Notes on using Python Flask in 2019

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.

Learning

Miguel Grinberg’s Flask Mega-Tutorial is the undisputable place to start and reference. I also strongly recommend his book, Flask Web Development. Be sure to get version 2.

Project Template

I maintain a reasonably well-documented skeleton of a Flask app built with the application factory and multiple configs.

Visual Studio Code

I don’t need to extole the virtues of VS Code.

Useful plugins: - Jinja highlighting: https://github.com/wholroyd/vscode-jinja

Debugging:

Initial configuration:

  1. Enable the python interpreter by opening any Python file *.py. In the bottom left corner of the screen you should see which interpreter you’re using
  2. Switch to the debug module
  3. Select “Add Configuration” from the configuration dropdown
  4. Select “Flask” from the menu that appears
  5. Set the application path. If you’re using my project template the enterance file is myapp.py
  6. VSCode will now show you the file launch.json that it generated. If you want to set or change any environment variables you can do so here.

To run your code switch to the debug module and press the green arrow!

Bootstrap

Use Bootstrap-Flask, not Flask Bootstrap which hasn’t been updated since May, 2017 and doesn’t support Bootstrap 4.

The render_field() and render_form() macros are particurarly useful.

OpenID Connect (oidc)

I have had the most luck with Flask PYOIDC

Background tasks

RQ with Redis

Supervisord is an easy way to run the RQ workers. It’s written in Python2, but can run Python3 applications / scripts without any issues.

Deploying to Azure

Deploying a Flask app to Azure is remarkably easy–props to the team responsible for the wizardry that happens under the hood.

  1. Log in to
  2. Select “Create a Resource” from the top left.
  3. Search for, select, and create a “Web App”.
  4. Fill in the web app details
    • Choose your subscription and resource group.
    • Give your application a name
    • Choose “Code” as your publish method
    • Choose the proper version of python for your runtime stack
    • Linux as your operating system
    • Choose the location most applicapable to you
  5. When the deployment is done open your new resource. The deployment process usually takes 20-30 seconds.
  6. Select the “Deployment Center” blade and the “Local Git” option.
  7. Select “App Service build service” and complete the process.
  8. The contents of the Deployment Center section has now changed. Azure has set up and empty git repository that we can push code to which it will build, host, and run. Copy the “Git Clone Uri” and add it to your local repository with the command

    git add remote [remote name] [remote uri]
    

    I like to call my deployment hosts “deploy” so for example:

    git add remote deploy https://daniel-flask.scm.azurewebsites.net:443/daniel-flask.git
    
  9. To actually deploy your code to this repository run:

    git push deploy master
    

    You will be prompted for a username and password which can be found by clicking on the key icon labled “Deployment Credentials” at the top of the Deployment Center.

  10. Almost done! If the startup file isn’t named “application.py” and/or isn’t located in the repository’s root folder Azure won’t find it. Open the “Configuration” blade and then to the “General Settings” tab. Fill in the “Startup Command” field as follows:

    gunicorn --bind=0.0.0.0 --timeout 600 [startup file minus extension]:[app object in that file]
    

    For example, using my my project template

    gunicorn --bind=0.0.0.0 --timeout 600 myapp:app
    

    You can find the full reference here

  11. To set environment variables navigate to the “Configuration” blade and add the “Application Settings” tab. You can reference them in your application with os.environ.get('Environment Variable Name')

  12. To restart your application to make the changes take effect navigate to the “Overview” blade and press the “Restart” button. Or push new code!

And that’s it! You can find the URL of you application on the “Overview” tab.