Important configs to remember
Flask
The app.run() method in Flask is used to start the development server for a Flask application. While it can be called directly within your application script, it is not typically part of the application's configuration itself. Instead, configuration settings for the development server are often passed as arguments to app.run() or managed through environment variables and the Flask CLI.
Common app.run() arguments for configuration:
-
debug=True/False:Enables or disables debug mode. When
True, Flask provides detailed error messages and automatically reloads the application on code changes. It should always beFalsein production. -
host='0.0.0.0':Specifies the IP address the server should listen on.
0.0.0.0makes the server accessible from any IP address (useful for development across multiple devices), while127.0.0.1(localhost) restricts it to the local machine. -
port=5000:Specifies the port number the server should listen on. The default is 5000.
Example of using app.run() with configuration arguments:
Python
from flask import Flaskapp = Flask(__name__)# ... define routes and other application logic ...if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=8000)
Alternative and Recommended Approach (Flask CLI):
For running a Flask application, especially in development, the recommended approach is to use the Flask Command Line Interface (CLI) and environment variables. This separates the running of the application from the application code itself.
- Set the
FLASK_APPenvironment variable: This tells Flask where to find your application.
Code
export FLASK_APP=your_app_file.py
(On Windows, use set FLASK_APP=your_app_file.py)
- Optionally, set
FLASK_DEBUGfor debug mode:
Code
export FLASK_DEBUG=1
(On Windows, use set FLASK_DEBUG=1)
- Run the application using the Flask CLI:
Code
flask run
This approach allows for more flexible configuration and is better aligned with how Flask applications are typically deployed.