Flask is a (micro) web development Framework for Python. It is fairly simple to get started. All you need to do is to pip install Flask
into your virtualenv, give the FLASK_APP environment variable your file and run flask run
(described in detail in installation and quick start). This will launch the development server and you can instantly start hacking around.
In this video I start with a simple Python Flask application and show how to 1) Write a Dockerfile 2) Build docker image 3) Start running a docker container based off the image In the end we have. Create a Python Flask application locally. First let us create the flask app locally and migrate it to the database. After that we will see how to do the same thing on Docker. Create the directory structure as below. Leave the venv folder for now as this will be automatically created when we create the virtual environment which is the next step.
When you want to use your Apache webserver, however, you need to install and configure a WSGI module. When I first wanted to do this I tried to install mod_wsgi via brew (brew install mod_wsgi
from the homebrew/apache tap), but quickly ran into some (apparently common) issues with the XCode toolchain. Then I discovered that there is a much easier way of installing mod_wsgi as a Python package.
On the PyPI page it says…
[it] will compile not only the Apache module for mod_wsgi, but will also install a Python module and admin script for starting up a standalone instance of Apache directly from the command line with an auto generated configuration.
Let’s try it out
At this point I assume you have virtualenv and the XCode cli tools (xcode-select --install
) installed (and of course the standard Apache from macOS). Everything else we will do together in the following steps.
Setting up virtualenv
Let’s start by creating the directory for the application and setting up our virtual environment:
Now we activate our environment:
Installing mod_wsgi
Run Flask App Locally
Installing mod_wsgi is now easily done via pip:
Let’s see if it worked by launching the server:
By pointing your browser to http://localhost:8000/, you should be greeted by this page:
Looks good. Let’s stop the server with ctrl-c
.
Installing Flask and creating the web app
If you already have your Flask app, you can skip the next few commands, but for the sake of completeness, let’s install Flask and create a small sample web app (make sure you are still in your virtualenv):
Let’s create another directory to put the code of our web application in, and fire up an editor for creating the code file:
Paste this code (from the Quick Start tutorial) into your file:
Creating the wsgi script
To let the server know about our application, let’s create the wsgi script which we will later point to when starting the server.
Paste this code into the new file:
UPDATE: I initially set sys.path.insert(0,'/your/path/my_app/')
in the snippet above. This is not needed, as the directory you run mod_wsgi-express setup-server
or mod_wsgi-express start-server
in is added to sys.path automatically. See thesethreetweets.
Launching the server with the wsgi script
By going to http://localhost:8000/ you should now see the “Hello, World!” from our Flask app.
Flask App In Macbook
Doesn’t work for you? Didn’t work for me at first either as I had a typo in the code 🙂. Check out the error logs and you will see a hint of what might be wrong: /tmp/mod_wsgi-localhost:8000:501/error_log
.
Running the server in the background
If you want to continue with your shell session, but leave the server running, you can use nohup
.
This will leave the server running, while you can continue working in the session (output will go to the file nohup.out). To bring it back to the foreground, use fg
.
UPDATE:Graham Dumpleton reached out to me and noted that the better way of running it in the background is by generating scripts via setup-server
and then use apachectl to start/stop. So you might want to execute the following (as root) instead of using nohup
:
(feel free to change the port)
And then control the server state via:
Watching for changes
When you develop your app and don’t want to reboot the server for each change, there is a convenient way of starting the server with the --reload-on-changes
option. Now you can change your files and have the changes immediately served.
More info
You can find more info about mod_wsgi (e.g. running it on a privileged port as root) on the PyPI page: mod_wsgi.
Flask App Mac
The command documentation is available here: