Introduction to Python Web development with Flask


Hi all, belated xmas and new year salutations! Had a busy time over the period and have only just gotten things settled to carry on my tinkering ways!
So, todays post is going to be about Python and the Flask framework. I was reading someone’s comments the other day about how easy, straight forward and clean Flask is.

After reading this I stopped and pondered a bit.......
then muttered “I like easy and clean……”. So I decided to play around with it. ^_ ^

I haven’t really gotten far, spent about an hour and a half, but decided to stop and write this up, I've come across some points I really like already that I want to write down before I forget!


To start off I’ll talk about the installation. I’ll begin with the virtual environment install first. Those of you new to Python may not know about the python tool Virtualenv. This tool is very handy for python developers. It allows you to have isolated pockets for your various different python projects. It is not a full blown virtualisation technology alongside the likes of VMware or VirtualBox. It is simply a clever way wrapping up your projects, all you get are independent lib directories, and project local binaries for python. The great thing about it is its really fast to get up and running and comes bolted together with Python3.

All you need to do to set up a env (with Python3) is:

python3 –m venv <environment-name>

This command would create a folder in your current dir with the title of the env-name you give. A list of this dir will show you something like:

bin   include  lib   lib64    pyvenv.cfg

All you’ll find is a python binary in bin and something like 3 lines of config in pyvenv.cfg which speaks for itself. Simple and clear...nice eh?


The lib folders will contain any modules you add to your environment as not to get mixed in with your global modules thus keeping it neatly tucked away.
So at this stage, you may of gotten an error already!?! Don’t panic, it happens to the best of us ^_^. After running the venv module you may seen an error message like:

Error: Command ‘[‘<your-pwd>/bin/python3’, ‘-Im’, ‘ensurepip’, ‘—upgrade’, ‘—default-pip’]’       return non-zero exit status 1

You may be wondering what pip has to do with anything?!?!

Well, when you create your venv by default the –default-pip argument is passed, this installs pip into your venv. For reasons I have not yet looked into, you may get this message indicating that pip was not installed ( believe it may be a bug with a particular version of python3). I'll leave a comment at the bottom once I've looked into this.

To fix this, delete your environment, then create it again using the above command, but use the "with_pip=False" option. This will create you env without any errors. However you will now have to install pip ( or any other package manager) into you new env manually.
To do this we first have to start up our environment. All you need to do is:

Source /<env-name>/bin/activate

Your prompt should change to look like:

(<env-name>) [b@localhost]

This is how you can tell you are now working within your new environment. You can also tell by using the which command on python:

which python

This will show you that your using the python binary under your project as oppose to your global.
Now you have your environment up and running you can install pip via:

curl | python

and once you have install pip into your project you can start by installing flask:

pip install flask

……..installation complete. Now let’s use flask!
As is custom I have started off by creating a simple hello word page. I’ve create a file under my environment, which looks like this:

From flask import Flask
app = Flask(__name__)

def index():
    return “<h1>Hello, world!</h1>”
def testing():
    return “<h1>This is another testing page</h1>”
If __name__ == ‘__main__’:

Lets run through this code. We've imported the Flask framework and initialised it under the 'app' parameter. The routing that Flask gives us can be utilised via the '@app.route()' method. Our first route demonstrates creating our index page. Flask will implement the method when the route is accessed. In this case the index method simply returns a string to the client accessing the '/' route, and of course your browser will interpret this as a headline tag and render it accordingly. The last line runs the simple app in debug mode.

Until next time######

Flask is a small compact framework. When you install Flask, all your getting really is a routing system - Werkzeug and a templating engine - Jinja2. Today I only touched on the routing system, My next post will build on this and show you how Jinja2 can be used to help you build you application.

The ideology that the Flask framework promotes is flexibility. The framework doesn't believe in force feeding you, Flask embraces that everyone has different needs, ideas and ways of working that is specific to you. It encourage you to take charge and decide your own way of doing things.
There is no database layer incorporated into the framework. This may seem odd at first glance, this is because Flask looks after the little man first ^_^, not all applications will need a database layer, especially small applications. So if you decide you need one, it’s up to you to pick the one you need and plug it in.
If I had to sum up Flask, I would use a quote by a wise man we all know:

“Be like water my friend…”