Flask 101: Getting Started

The Flask 101 series is my attempt at learning the Flask microframework for Python. For those who haven’t heard of it, Flask is micro web framework for creating web applications in Python. According to their website, Flask is based on Werkzeug, Jinja 2 and good intentions. For this series of articles, I wanted to create a web application that would do something useful without being too complicated. So for my learning sanity, I decided to create a simple web application that I can use to store information about my music library.

Over the course of multiple articles, you will see how this journey unfolded.


Getting Setup

To get started using Flask, you will need to install it. We will create a virtual environment for this series of tutorials as there will be a number of other Flask dependencies that we will need to add and most people probably don’t want to pollute their main Python installation with a lot of cruft they may not end up using. So before we install Flask, let’s create a virtual environment using virtualenv. If you want to use virtualenv, then we will need to install that with pip:

pip install virtualenv

Now that we have that installed, we can create our virtual environment. Find a location on your local system where you want to store your web application. Then open up a terminal and run the following command:

virtualenv musicdb

On Windows, you might have to give the full path to virtualenv, which is usually something like C:\Python36\Scripts\virtualenv.exe.

Note that starting in Python 3.3, you can also use Python’s built-in venv module to create a virtual environment instead of using virtualenv. Of course, the virtualenv package can be installed in Python 3, so it’s up to you which you want to use. They work in pretty much the same way.

Once you have your virtual environment set up, you will need to activate it. To do that, you will need to change your directory in your terminal to the folder you just created using the “cd” command:

cd musicdb

If you are on a Linux or Mac OS, you should run the following:

source bin/activate

Windows is a bit different. You still need to “cd” into your folder, but the command to run is this:

Scripts/activate

For more details on activating and deactivating your virtual environment, check out the user guide.

You may have noticed that when you created your virtual environment, it copied in your Python executable as well as pip. This means that you can now install packages to your virtual environment using pip, which is the reason so many people like virtual environments. Once the virtual environment is activated, you should see that your terminal has changed to prepend the name of the virtual environment to the terminal’s prompt. Here’s an example screenshot using Python 2.7:

Now we’re ready to install Flask!


Getting Started with Flask

Flask is easy to install using the pip installer. Here’s how you can do it:

pip install flask

This command will install Flask and any of the dependencies that it needs. This is the output I received:

Collecting flask
Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
100% |████████████████████████████████| 92kB 185kB/s
Collecting itsdangerous>=0.21 (from flask)
Downloading itsdangerous-0.24.tar.gz (46kB)
100% |████████████████████████████████| 51kB 638kB/s
Collecting Jinja2>=2.4 (from flask)
Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
100% |████████████████████████████████| 133kB 277kB/s
Collecting Werkzeug>=0.7 (from flask)
Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
100% |████████████████████████████████| 317kB 307kB/s
Collecting click>=2.0 (from flask)
Downloading click-6.7-py2.py3-none-any.whl (71kB)
100% |████████████████████████████████| 71kB 414kB/s
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask)
Building wheels for collected packages: itsdangerous
Running setup.py bdist_wheel for itsdangerous ... done
Stored in directory: /home/mdriscoll/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
Successfully built itsdangerous
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 flask-0.12.2 itsdangerous-0.24

Now let’s write something simple to prove that Flask is working correctly. Save the following code in the musicdb folder that we created earlier.

# test.py
 
from flask import Flask
 
app = Flask(__name__)
 
@app.route('/')
def test():
    return "Welcome to Flask!"

All this code does is import the Flask class and create an instance of it which we call app. Then we set up the default route for the home page (AKA root or index) of our website. This is done via the following decorator: @app.route(‘/’). Finally we create a function that just returns a string.

When this code is run in Flask, you will be able to navigate to your new web app’s home page and see that text. That brings us to how we run this code. In your terminal, make sure you are in your musicdb folder. Then run the following command in the terminal:

FLASK_APP=test.py flask run

When you run this command, you should see something like this in your terminal:

* Serving Flask app "test"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Now you just need to open up a browser, such as Chrome or Firefox, and go to the URL mentioned above: http://127.0.0.1:5000/. Here is what I got when I went to that URL in Chrome:


Wrapping Up

At this point you should be able to get a working version of Flask up and running. You can do some really basic web applications with just what you have right now. In the next article in this series, we will look at how to add database support to our web application.


Other Articles in the Series

Print Friendly, PDF & Email