Using the Flask framework with Python

This page is showing a generic answer.
To see a more detailed answer customized for you, type your domain name here:

You can use the Flask framework for Python on our servers.

To get this to work, you need to create two files in a directory of your website. For the sake of this example, we’ll assume you create them in a directory named /flask.

An example using Python 3

To get started, first run this from the command-line shell to install the Python “flup” module:

pip3 install flup

You can also create the flask directory from the shell if you haven’t already done so:

mkdir ~/html/flask

Next, create a file name “index.fcgi” to handle FastCGI requests, using a editor like vi ~/html/flask/index.fcgi:

#!/usr/bin/python3
import sys
from flup.server.fcgi import WSGIServer
from myapp import app

class ScriptNameStripper(object):
  def __init__(self, app):
    self.app = app
  def __call__(self, environ, start_response):
    environ['SCRIPT_NAME'] = ''
    return self.app(environ, start_response)

app = ScriptNameStripper(app)
if __name__ == '__main__':
  WSGIServer(app).run()

After saving that file, make it executable, which you can do with this command:

chmod 0755 ~/html/flask/index.fcgi

Finally, create and save a second file named “myapp.py” using vi ~/html/flask/myapp.py:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
  return 'Hello, World!'

After doing this, the script should work if you load it as https://example.com/flask/index.fcgi/.

Can I hide “/index.fcgi/” at the end of the URL?

You can make the script work when loaded as just https://example.com/flask/ if you add this .htaccess file to the directory:

RewriteEngine On
RewriteBase /flask/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]

Note that the “/flask/” in the “RewriteBase” line needs to match the name of the directory the files are in.

How can I force Flask to quickly reload when I make a change to a script?

One of the reasons Flask is so efficient is that it reads the application’s Python files once when it starts, but doesn’t read them again for each web request it handles.

This can be frustrating if you’re trying to make small live changes when you’re developing a script from the command-line shell — you want to see the changes instantly, without waiting until it decides to read your files again.

The simplest way to solve this is to terminate the running copy of Flask after you make any change. Run this:

killall /usr/bin/python3

After you do that, the next web request will read the changed copy of the files.

How can I debug “internal server error” messages?

As the Flask FCGI documentation mentions, FastCGI deployments tend to be hard to debug. Executing the application by hand is usually the best way to see the true error. For example, you can simply run this from the shell:

python3 ~/html/flask/index.fcgi

If the script is working properly, you’ll see something like “OSError: [Errno 88] Socket operation on non-socket”, which is okay (it shows that the script is successfully trying to communicate with the web server). If there’s an error in the script, it will show a useful error message like:

NameError: name 'does not exist' is not defined