Explain Codes LogoExplain Codes Logo

Auto reloading python Flask app upon code changes

python
auto-reload
flask
development
Nikita BarsukovbyNikita Barsukov·Dec 17, 2024
TLDR

Experience real-time code updates in Flask by enabling debug setting in app.run(). This results in an automatic server refresh whenever you save your code:

app.run(debug=True)

This setup provides an instantly updated preview of your changes, without the manual hassle of restarting the Flask server.

Detailed set-up guide

Environment variables to kick-start the auto-reload

In Flask, development-friendly features can be toggled ON by setting certain environment variables:

  • FLASK_APP: Set to the name of your module entry point:
    export FLASK_APP=yourapplication.py
  • FLASK_ENV: Set to 'development' to activate development environment features, including auto-reloading:
    export FLASK_ENV=development

For Flask 2.2+, make sure the FLASK_DEBUG environment variable is set to 1. It's the secret key to unlock debugging and auto-reloading:

export FLASK_DEBUG=1

Command line magic with flask run

You can also harness the power of flask run command-line utility to start your application in debug mode, thereby enabling auto-reloading:

flask run --app yourapplication --debug

Want to explore all options flask run provides? Run the below command:

flask run --help

Unleash the beast: Werkzeug debugger

The Werkzeug debugger can also serve your debugging and auto-reloading needs. How to enable it:

from werkzeug.debug import DebuggedApplication app = DebuggedApplication(app, evalex=True)

Automatic .env loading and uWSGI integration

Auotomating environment loading using python-dotenv

Congure Flask seamlessly by using python-dotenv to automatically read environment variables from a .env file:

from dotenv import load_dotenv load_dotenv()

This reads in your .env configurations, including FLASK_APP and FLASK_ENV, easing Flask's auto-reload functionality.

Nginx + uWSGI: The Super Duo

As your Flask application grows, you may consider integrating Nginx and uWSGI for better performance and congruency with production environments. uWSGI's touch-reload can be used for auto-reloading triggered by filesystem changes:

touch-reload = '/path/to/reload.trigger'

Simply touch the specified file to reload:

touch /path/to/reload.trigger # And Voila! The server dances to your touch 🕺

Automate uWSGI reloads

Consider making a simple script to automate touching the file and trigger uWSGI's reload:

#!/bin/bash touch /path/to/reload.trigger # Easy peasy lemon squeezy! 🍋

A word of caution for production usage

Remember: Debug mode and auto-reloading are superheroes for local development, but often morph into villains in a production environment due to potential security risks and performance overhead. Ensure these features are not enabled in a live, production environment.

Extending Your Development Workflow

Auto-reloading in front-end development

In addition to app code, template changes are also auto-reloaded. This enhances front-end development by instantaneously reflecting HTML/CSS updates:

# Flask auto-reloads the templates when debug is enabled app = Flask(__name__) app.config['TEMPLATES_AUTO_RELOAD'] = True

Keeping your configurations tidy

Maintain a dedicated settings file to keep track of and organize multiple configurations:

# Reference settings from configurations in settings.py DEBUG = True # The "open sesame" for enabling debug mode and auto-reload

Then, just pump up your app with these settings!

Visual Studio Code: Your debugging companion

You can also integrate Flask with Visual Studio Code for a smoother debugging experience:

  1. Get the Python extension running.
  2. Use the integrated terminal to set environment variables.
  3. Start the auto-reloading Flask server using the terminal.

Now you have your IDE's debugging tools and Flask's auto-reloading working in harmony. Code and debug in a breeze!

References