If you're like me, you’ve probably seen a few different approaches to starting a Flask application and wondered which one is the best. Sometimes, you’ll encounter manage.py, other times, you'll see create_app. This can lead to confusion, especially if you're new to Flask development or transitioning from one project to another.
In this article, I will walk you through the most common methods used to start a Flask application, breaking them down with clear examples so you can decide what works best for your use case.
The simplest way to start a Flask application is by creating an app.py file. This is great for small applications or when you're just starting with Flask.
# app.py from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Welcome to my Flask app!" if __name__ == "__main__": app.run(debug=True)
In your terminal, navigate to the folder containing app.py and run:
python app.py
Flask will start on localhost:5000, and you can visit your app in a browser. This is the quickest method, but it has limitations for scaling.
As your application grows, the factory pattern with create_app() becomes more useful. This method provides a way to configure and initialize your app in a modular way, allowing you to better manage complex setups.
# app.py from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return "Hello from Factory Pattern!" return app
Since there’s no if __name__ == "__main__" block, you'll run it by setting the FLASK_APP environment variable.
export FLASK_APP=app:create_app export FLASK_ENV=development flask run
This method is more scalable because it allows for easier configuration management, making it suitable for larger applications or ones using extensions.
Although Flask-Script has been deprecated in favor of Flask's built-in command-line interface (CLI), some legacy applications still use the manage.py approach.
# manage.py from flask_script import Manager from app import create_app app = create_app() manager = Manager(app) if __name__ == "__main__": manager.run()
To run the application:
python manage.py runserver
Since this method is now considered outdated, it is better to rely on Flask's CLI for similar functionalities.
When deploying a Flask application to production, you'll want to use a WSGI server like Gunicorn instead of Flask’s built-in development server.
Here’s how you would run your create_app method with Gunicorn:
gunicorn 'app:create_app()'
This will launch your Flask app using Gunicorn. You can specify the number of worker processes, the host, and the port if needed:
gunicorn -w 3 -b 0.0.0.0:8000 'app:create_app()'
Flask’s CLI simplifies running the app and performing other commands like migrations. The default CLI uses the FLASK_APP and FLASK_ENV environment variables.
export FLASK_APP=app.py export FLASK_ENV=development flask run
This command runs your app in development mode with hot reloading and debug mode enabled. It’s great for development, but you shouldn’t use it in production.
Understanding these different methods gives you flexibility in how you start a Flask application. Whether you're building a small project or deploying a large-scale system, you’ll find the right approach to suit your needs. By grasping the essentials of each method, you'll be able to maintain and scale your application efficiently.
Have you used a different approach that works better for you? Let me know in the comments!
The above is the detailed content of How to Start a Flask Application: A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!