Flask is a great Python framework for getting ideas up and running quickly on a lightweight app environment. In this tutorial we are going to get Python 3.6 configured with development system packages to start a new Flask web application project and run it with Green Unicorn (Gunicorn).
Green Unicorn (from here on out, we will refer to it as Gunicorn) is “…a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.”
You can find out more about Gunicorn, from their website, which is also where I pulled that quote. But for the sake of brevity, Gunicorn is an easy to use HTTP server for Python web apps.
To begin, we’re going to install three packages. python-dev python-pip & python-virtualenv. You should also check which version of Python you have installed and it’s location, then run the following command below:
sudo apt-get install python3-dev python3-pip python3-virtualenv
If run correctly, the package manager should spit a bunch of output and report when everything is set up. From there, we’ll use pip and virtualenv, both packages we just installed, to form a directory and handle our app dependencies.
###CREATE A NEW DIRECTORY TO HOUSE YOUR VIRTUAL ENVS cd ~ mkdir -p ~/venvs ###SPECIFY PYTHON3 FOR THE INSTALLTION AND THEN ACTIVATE YOUR VIRTUAL ENV python3 -m venv venvs/flaskproject source ~/venvs/flaskproject/bin/activate
If all goes well, you should see your terminal prompt changes with the name of your virtual environment that was activated. In our case, we have (flaskproject) now in our terminal prompt.
We’re going to use pip to install a few dependencies and packages. Let’s start by updating our packages list and then installing wheel. Then we will install Flask and Gunicorn as well.
###UPDATING AND INSTALLING WHEEL pip install --upgrade && pip install wheel ###INSTALLING FLASK AND GUNICORN pip install flask gunicorn
You should see a bunch of output expressing successful installations. If all goes well, you shouldn’t see any errors.
We need to create a new directory to hold our app files. This directory will live within our home folder, not within our virtualenvs folder.
###CREATE A DIRECTORY AND MOVE INTO IT mkdir ~/flaskproject cd ~/flaskproject ###CREATE FILE FOR TEST APP CODE nano app.py
Within the app.py file, write the following code:
from flask import Flask, Response app = Flask(\_\_name\_\_) @app.route("/") def index(): return Response("Your Python Flask Project is Working!"), 200 if \_\_name\_\_ == "\_\_main\_\_": app.run(debug=True)
Now move to the directory above the flaskproject folder, which in our case is the user home folder. You can also just typecd ~ to get there.
From your home folder, we can use Gunicorn to run your app with the following command:
###RUN THIS FROM YOUR HOME FOLDER ~/ gunicorn flaskproject.app:app
If successful, we’ll get output of Gunicorn starting and listening on localhost:8000 or 127.0.0.0:8000 and going to that address should show your app.
That’s it! You basically set up a quick Python test environment using PIP, Flask, and Gunicorn. From here, you can build more dynamic web apps using Flask and test them the same way.