Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.
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
- 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
- Switch to the debug module
- Select “Add Configuration” from the configuration dropdown
- Select “Flask” from the menu that appears
- Set the application path. If you’re using my project template the enterance file is
- VSCode will now show you the file
launch.jsonthat 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!
The render_field() and render_form() macros are particurarly useful.
OpenID Connect (oidc)
I have had the most luck with Flask PYOIDC
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.
- Log in to
- Select “Create a Resource” from the top left.
- Search for, select, and create a “Web App”.
- 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
- When the deployment is done open your new resource. The deployment process usually takes 20-30 seconds.
- Select the “Deployment Center” blade and the “Local Git” option.
- Select “App Service build service” and complete the process.
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
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.
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
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')
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.