As I mentioned in my previous post I’m looking into installing a WordPress plugin to make it easier for me to share computer code snippets here on the blog. However, when I looked in to doing this on WordPress.com, Automattic (WordPress.com’s parent company) wanted to increase my monthly payment from $4 to $25 a month! $25 seemed pretty steep to me, so I decided to move the whole site over from WordPress.com to my favorite IaaS provider, DigitalOcean. The move has actually been quite a bit of work, but I’m going to end up saving a lot of money. Instead of $25 a month I’m going to end up paying something like ~$8 per month for a cheap virtual server, a .blog domain extension, and an SSL certificate. Originally I planned on just dumping the whole SSL/HTTPS thing in favor of simple HTTP, but I don’t want to break all of my old https links that have already been indexed by Google. So… in conclusion… Goodbye WordPress.com, and hello self hosting with the power of open source software 🙂
New WordPress Theme; Broken Posts :(
A lot of changes going on here with the blog behind the scenes (not that anyone really cares). I found a WordPress theme that I really like called Casper, but unfortunately the new theme has sort of broken a lot of my old posts. The content is all still there, but there are just little goofy doodads around my various computer programming code snippets. The whole point of this blog is to publish programming related content so I’m working hard to get setup to where I can easily post little programming tutorials with snippets of code and whatnot. I suppose done is better than perfect so I’ll be sticking with the new theme for now despite the small bugs related to my old posts. Also, I plan on checking out some cool WordPress plugins to make it easier to share code here on the blog. Coming Soon!
# TODO: Learn to Use MySQL with Python & Flask
This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io
Just wanted to bookmark a great tutorial on: Creating a Web App From Scratch Using Python Flask and MySQL. Coming from a LAMP Stack background, I’d really like to keep using MySQL, so that’s what I plan on playing around with over the next couple days. Learning Python/Flask is coming along fairly well, and I think I’m almost to the point where I can actually start building some stuff.
UPDATE (3/20/2019): Made some good progress getting MySQL all setup on my local Windows 10 development machine tonight. Note to self– pick back up with the tutorial tomorrow @ Step 1: Setting Up the Database
UPDATE (3/20/2019): Pick back up @ Step 3: Implement a Signup method
UPDATE (3/21/2019): Resume tutorial @ Step 5: Call the MySQL Stored Procedure
CONCLUSION (3/21/2019): So I just finished the above tutorial, but unfortunately received an error when attempting to actually insert data into my MySQL database. I think the problem likely has something to do with “stored procedures.” I’ve never actually used a “stored procedure” before and honestly don’t really even know what it is or why I would want one, but… I think the tutorial was helpful. My main takeaway from the whole thing was that I should look more into this library called Flask-MySQL so I can really get this figured out. Right now I have two big things I need to learn:
- Using MySQL with Python & Flask
- Deploying my Flask Project Code to a Live Server Running Apache Web Server Software Instead of the Built-In Development Flask Server
FURTHERMORE (3/21/2019): There appear to be multiple python/mysql libraries, so I might check some of those out as well.
YET ANOTHER UPDATE (3/22/2019): I figured out how to INSERT data into my MySQL database using the mysql.connector Python library. Here is a link to my example webapp: Flask-Plus-MySQL
Right now the demo app only shows how to INSERT data into the database. However, I will make sure to improve the demo tomorrow so that it also shows how to SELECT data from the database and then display the data using a Jinja2 template.
LAST UPDATE (3/22/2019): Finished adding some code to the example app which demonstrates how to SELECT data from a MySQL database using Python (and Flask) and then present that data back to the user using the Jinja2 templating engine.
Receiving HTTP-POST Requests in Python using Flask
Over the past few previous posts here on the blog I’ve been talking about Python & Flask and how I sort of got stuck today on Chapter 4 of an O’Reilly book on Flask when the author decided to throw in a 3rd party library for handling basic networking that ended up throwing me off (along with a few other choices the author made). Well… I’ve finally figured out what I’m doing and figured out how to handle basic networking in Python using Flask the way *I want to do things*.
In my opinion, you shouldn’t need a third party library to handle networking in a web-app. The whole point of learning Flask over something like Django was the simplicity of everything. So here I present: Simple Networking in Python with Flask
How to Setup a New Flask App on a Local Windows Machine
Another quick little blog post here for my own future reference. I’m just getting started learning Flask and it’s quite different from doing web development in PHP like I’m used to. So here are a few quick steps to get started (assumes virtualenv and python 3 have already been installed):
Create a Blank Flask App
$ cd Desktop $ mkdir my_new_flask_app $ cd my_new_flask_app $ virtualenv venv $ venv\Scripts\activate (venv) $ pip install flask
Hello, World (helloflask.py)
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'hello, world'
Start Local Development Server
$ set FLASK_APP=helloflask.py $ flask run
Visit Your New App With Your Web Browser: http://localhost:5000
Keeping it Simple, Stupid: Handling HTTP GET & POST Requests in Python with Flask
TLDR: Skip To The Bottom of This Post/Rant for the Answer to Handling HTTP POST & GET Requests in Python with Flask (the Easy Way)
Just sort of wanted to vent here for a minute… I’m working out of Miguel Grinberg’s Flask Web Development (2nd Edition) trying to teach myself Flask and I’m really frustrated: I’m only on Chapter 4 and this guy has already thrown in Bootstrap and another 3rd party library (Flask-WTF) and the examples are quickly growing in complexity to the point where I’m having trouble following along. I wish authors would just keep things as simple as possible. There’s no need to use every feature, and every library, when writing a programming book or tutorial. Programming is complicated enough.
O well I suppose. I got a lot out of the first three chapters, but it looks like I’m going to have to move on to another book to keep learning. Tossing in this Flask-WTF library and using the ‘extends’ and ‘block’ features in the Jinja2 templates just makes it too confusing as a complete beginner. I’m sure that the ‘extends’ and ‘block’ features are nice to have, but they aren’t appropriate this early in a programming tutorial. Just demonstrate how to do something in its most simplest form.
TAKEAWAY: Read all the programming books on a given subject. Take what you can from each book, and move on to the next when necessary.
UPDATE: Here are the three other books on Flask which I’m planning on diving into tonight–
- Building Web Applications with Flask by Italo Maia
- Flask by Example by Gareth Dwyer
- Learning Flask Framework by Matt Copperwaite & Charles Leifer
UPDATE: Okay, so two of the three new books also jump straight into handling HTTP GET & POST Requests using Flask-WTF, but luckily Flask By Example kept things simple and demonstrated the simplest way to handle networking requests. Need to grag some GET or POST data? Simply call:
# Retrieve Data from an HTTP GET Request from flask import request username = request.args.get("username")
# Retrieve Data from an HTTP POST Request # Additional Reference: https://bit.ly/2HsJtD1 # Note, we are using HTTP POST, However the # Method Name is Still .get() # A Bit confusing eh? from flask import request username = request.form.get("username") password = request.form.get("password")
Jinja2 Control Structures
Another quick little post here for my own personal reference. Working out of Miguel Grinberg’s Flask Web Development (2nd Edition) trying to teach myself a little Flask for a new project on which I’m working, so here is a little code straight from Grinberg’s book demonstrating the basics of using control structures in Flask’s Jinja2 templating language:
How to Restart Your Flask Development Server on Windows
Quick little post here for my own personal reference on how to restart your Flask development server on Windows:
1) $ cd Desktop/YourFlaskProjectDirectory
2) $ venv\Scripts\activate
3) $ set FLASK_APP=your_main_flask_script.py
4) $ flask run
5) Visit http://localhost:5000/ in your browser
Dumping Django for Flask
Spent my day off today diving into Django and have found myself quite frustrated with the whole thing. Working through the official Django quick-start tutorial I keep finding myself thinking: How complicated does “hello, world” have to be?
I’ve often heard neckbeards on the internet proclaim you “can’t judge something by it’s hello, world.” I call B.S. Judge languages and frameworks by how difficult it is to go from zero to hello, world. If the authors of a language or framework make your life miserable getting to hello, world, you can count on the fact that they aren’t going to make anything else any easier.
Coming from PHP I know that I am spoiled. Nothing is easier than going from zero to hello, world with PHP! Unfortunately for my latest project I need to either use Python, Ruby, or Node.js. So it looks like I’m going to go ahead and give Flask a try. I will make sure to write up another post about Flask if I end up liking it, or if I find something else wonderful I’ll go ahead and post about that instead. Stay tuned!
UPDATE: I got the Flask “hello, world” all up and running following along with Miguel Grinberg’s Flask Web Development (2nd Edition) from O’Reilly and found it much easier to wrap my head around how everything works compared to Django. Simplicity is of great importance to me, so I think I’ve made the right choice. If you happened to stumble upon this blog post by searching for “Django vs. Flask” or something along those lines, I think you’ll find Flask the better option if you prefer keeping things as simple as possible.
SyntaxError: Generator expression must be parenthesized
This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io
I’ve been trying to teach myself a little Django here over the past few days for a new side project I’m working on that requires Python instead of my usual goto language PHP. I was attempting to run Django 1.11.11 on my Windows machine running Python 3.7.1 and ran into the following error (SyntaxError: Generator expression must be parenthesized) when attempting to run the built-in Django development server. According to one StackOverflow post the problem is that Django 1.11.11 isn’t compatible with Python 3.7.1. If you happened to have stumbled upon this blog post searching for an answer to this error message, I suggest uninstalling Django and reinstalling the latest version of Django 1 which happens to be Django 1.11.20 (NOT 1.11.11), or simply upgrade to Django 2.
Personally, I was attempting to run Django 1.11.11 because that is the version of Django that comes with the DigitalOcean Django server droplet image. Going forward I’m going to try running 1.11.20 on my Windows machine and see how that goes, as I would prefer to stick with using DigitalOcean’s pre-configured Django server droplet images as opposed to setting everything up from scratch myself.