Flask Autodeploy

Website deployment and server configuration can be a rather unrewarding task. To save you the trouble of configuring your Python applications to run on Apache, we put together a script to do the dirty work for you. If you follow the instructions below, you should have your Flask application up and running in no time

Overview

  1. Take a look at the example we included in the handoutcode repository. If you haven't pulled the handoutcode yet, cd to your project directory and run:
    git pull handoutcode master
    There should be a folder called "Flask_example" containing a single file called "main.py." Notice that the "main.py" file defines a variable called "app" which is assigned to a Flask application object. This is the entry point to the web application.
  2. Now, let's try deploying the application to the 6.170 server. All you have to do is push your repository to the remote branch called "deploy_only"
    git add .
    git commit -a -m "first deploy"
    git push origin dev:deploy_only
  3. Now, browse to your deploy url at https://courses.csail.mit.edu/6.170/sp12/students/ and navigate to the folder called Flask_example. Click on the file called "index.fcgi" to launch the web application.
  4. If all goes well, you should see the line "Hello World! I'm a Flask example."

Using databases

Extra Tips

Behind the scenes

What is happening behind the scenes? When you run your web application from your laptop (or desktop), Flask automatically creates a lightweight "test server" on your computer which listens for requests on a specific port on your computer. On a large shared server such as courses.csail.mit.edu, however, we don't have convenience of spawning new test servers on different ports.

In one way or another, Apache must associate a specific URL with your Python application. A naive solution would simply invoke your Python application every time a client requests that specific url. However, this would mean that the server would have to initialize a Python environment and load your code on every single web request. This is certainly not a very scalable solution.

Instead, Apache only loads the Python environment during the very first web request by essentially spawning a new, separate process to house your web application. Though the first request may be slow, subsequent requests are quick because the application has already been loaded into RAM.

The "index.fcgi" file which the autodeploy script added to your project tells Apache where to find the entry point to your application. It additionally wraps your application in a middleware which prints pretty error messages whenever your application crashes.

However, what exactly is the Flask application, and how does Apache know how to use it? In fact, Python developers have settled on a convention called the "WSGI" application which provides a simple interface for communication between the web server (Apache) and your web application. This modular design makes it easy, for example, to wrap web applications in extra functionality very much the same way that Python function decorators add functionality to regular Python functions. If you want to learn more, you can read about it here.