Setting Up a Flask Project
Every Flask project should start with an isolated virtual environment (python -m venv venv) to avoid dependency conflicts, followed by pip install flask, which pulls in Flask and its core dependencies (Werkzeug, Jinja2, Click, itsdangerous, blinker); this keeps project dependencies reproducible via a requirements.txt or pyproject.toml.
Cricket analogy: Creating a virtual environment before installing Flask is like a franchise like Chennai Super Kings keeping a separate net-practice pitch for trialing new players, so experiments never affect the main squad's form.
The Application Object
The Flask application object, created with app = Flask(__name__), is the central registry for routes, configuration, and error handlers; passing __name__ lets Flask locate resources like templates and static files relative to the module, and the object exposes config as a dict-like structure (app.config["SECRET_KEY"] = ...) used across the whole app.
Cricket analogy: The Flask app object is like the team captain such as Rohit Sharma, the single point who holds the batting order, field placements, and strategy that every other decision on the pitch refers back to.
Running the Development Server
You can start Flask's built-in server either by calling app.run(debug=True) in the __main__ block or, preferably, by setting the FLASK_APP environment variable and running flask run, which enables the reloader and interactive debugger when FLASK_DEBUG=1 (or debug=True) is set, auto-restarting the server on code changes and showing tracebacks in the browser.
Cricket analogy: Running flask run with the reloader active is like a bowling machine at nets that automatically resets and reloads after every delivery, letting Virat Kohli keep facing balls without manual intervention.
python -m venv venv
source venv/bin/activate
pip install flask
export FLASK_APP=app.py
export FLASK_DEBUG=1
flask runNever run app.run(debug=True) or FLASK_DEBUG=1 in production — the interactive debugger executes arbitrary Python code from the browser, which is a severe remote code execution risk if exposed publicly.
- Use python -m venv and pip install flask to create an isolated, reproducible environment.
- app = Flask(__name__) creates the central application object used for routing and config.
- __name__ helps Flask locate templates and static files relative to your module.
- flask run (with FLASK_APP set) is the preferred way to start the dev server.
- debug=True enables the auto-reloader and interactive in-browser debugger.
- Never enable debug mode in a production deployment.
Practice what you learned
1. What command creates an isolated Python environment before installing Flask?
2. What is passed as the argument to Flask() when creating the app object?
3. Which environment variable tells the flask run command which file to load?
4. What serious risk does leaving debug mode on in production create?
5. What does Flask's debug reloader do?
Was this page helpful?
You May Also Like
What Is Flask?
Flask is a lightweight WSGI micro web framework for Python that provides routing, request handling, and templating while leaving choices like ORM and auth up to the developer.
Routing in Flask
Understand how @app.route binds URLs to view functions, how dynamic URL converters validate input, and how to handle HTTP methods and generate URLs with url_for.
Flask Application Structure
Learn how to organize a growing Flask project using the application factory pattern, Blueprints for modularity, and conventional templates/static/config layouts.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics
Web DevelopmentNext.js Study Notes
JavaScript · 30 topics