Lab Currently Closed Hours: 9am–8pm on Tuesday more »

Flask

Note: These instructions are primarily intended for individual user accounts. If you are using a group account, you may wish to consider apphosting instead.

Flask is a popular microframework for Python web development. Using it on the OCF servers requires only just a little extra configuration.

Starting a Flask project

  1. Make a directory in your ~/public_html directory:

    $ mkdir ~/public_html/flasky
    
  2. Go into the directory you just created, and create two files: .htaccess (note the dot), and run.fcgi.

  3. .htaccess:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    # Change "username" and "flasky" to your username and whatever directory name you made in public_html, respectively
    RewriteRule ^(.*)$ /~username/flasky/run.fcgi/$1 [QSA,L]
    
  4. run.fcgi:

    #!venv/bin/python
    from flup.server.fcgi import WSGIServer
    from flaskyapplication import app
    
    if __name__ == '__main__':
        WSGIServer(app).run()
    
  5. Make a virtualenv. This will allow you to pip install any python packages that you need that the OCF does not already have. The benefit of having a virtualenv is that you can decide the dependencies you want, without being tied to the OCF defaults:

    $ virtualenv -p python3.5 venv
    $ . venv/bin/activate
    $ pip install flask
    $ pip install flup6
    $ pip install <YOUR PACKAGE>
    
  6. Either add your application as a module in this folder or a subdirectory that will be treated as a python package:

  7. Case 1:

    /flaskyapplication.py
    /run.fcgi
    
  8. Case 2:

    /flaskyapplication
        /__init__.py
    /run.fcgi
    
  9. Write your application logic inside the module or package:

    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello World!'
    
    if __name__ == '__main__':
        app.run()
    
  10. Make the run.fcgi file you just created executable:

    $ chmod a+x ~/public_html/flasky/run.fcgi
    
  11. Once your app has started running, changes you make to the Python code or templates won't take effect for a few hours. To apply changes immediately, you can touch the run.fcgi file with the command:

    $ touch run.fcgi