Before building web applications with Flask, establishing an isolated virtual environment is critical. Virtual environments guarantee that your project's dependencies never conflict with system-wide Python packages or other projects on the same machine.
Python 3 provides the built-in venv module. Follow these commands in your project terminal:
When activated, your terminal prompt will display (.venv).
Install the latest version of Flask using pip:
To verify the installation and view installed dependencies:
You will notice that installing flask automatically installs its key dependencies:
Werkzeug: WSGI utilities and routingJinja2: Template engineMarkupSafe: HTML string escapingclick: Command Line Interface frameworkitsdangerous: Cryptographic signing for session cookiesblinker: Signal dispatching (in modern Flask 3.x)Create a file named app.py in your project root:
from flask import Flask: Imports the central application class.app = Flask(__name__):Flask class is created.__name__ is a Python special variable representing the current module's name ("__main__" when run directly).__name__ to locate templates, static assets, and resource folders relative to this file.@app.route("/"):"/", Flask invokes the decorated home() function.return "<h1>...</h1>":200 OK response with Content-Type: text/html; charset=utf-8.return {"status": "success", ...}:Content-Type: application/json.app.run(debug=True, port=5000):http://127.0.0.1:5000.Execute app.py directly:
Console Output:
Open http://127.0.0.1:5000 in your web browser to view your live web application!
__name__ passed into app = Flask(__name__)?debug=True flag in app.run() during development?app.run() development server explicitly not recommended for production deployments?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| What is Flask & WSGI Architecture | Flask CLI & Environment Configuration |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.