Flask is a micro web framework written in Python. It’s called “micro” not because it’s limited, but because it keeps the core simple and extensible. You can build anything from a small prototype to a full-featured web application. Flask it doesn’t impose strict requirements on project structure or dependencies. Here is a list of some features.
- Routing: Easily map URLs to Python functions using the @app.route() decorator.
- Templates: Use Jinja2 templating to dynamically generate HTML (render_template()).
- Request Handling: Access query parameters, form inputs, JSON data with request.
- Modular: Add only what you need โ like SQLAlchemy for database handling or Flask-Login for authentication.
- Debug Mode: Built-in development server with real-time reloading and error reporting.
๐น Part 1: Setup Python and Flask
โ Step 1: Install Python (only once)
- Go to the official Python website: ๐ https://www.python.org/downloads
- Click the yellow download button that says “Download Python 3.x.x”.
- IMPORTANT: Check the box that says Add Python to PATH before clicking Install Now.
- When it’s done, restart your computer (just in case!).
โ Step 2: Open the Terminal
- Press Windows key, type cmd, and hit Enter.
- Youโll see a black window โ this is the Command Prompt. Youโre ready to talk to your computer ๐ฌ
โ Step 3: Install Flask
In the Command Prompt window, type this:
pip install flask
Wait a moment โ it will install Flask. Youโll see a bunch of lines. If it ends without errors, you’re good!
๐น Part 2: Create Your Web App
โ Step 4: Create a New Folder
- On your desktop, right-click > New > Folder โ Name it flask_hello
- Open it, and right-click inside โ New > Text Document
- Rename it to: app.py (make sure itโs not app.py.txt)
โ Step 5: Write Your Python Web App
- Right-click app.py โ Open with โ Notepad (or Notepad++)
- Copy & paste this code:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World! This is my first web app!' if __name__ == '__main__': app.run(debug=True)
Click File > Save
๐น Part 3: Run Your Webpage
โ Step 6: Run Your Flask App
Back in your Command Prompt, type:
cd Desktop\flask_hello
Then type:
python app.py
You will see in the command prompt
Running on http://127.0.0.1:5000/
โ Step 7: View It in the Browser
- Open your browser (Edge, Chrome, etc.)
- Type in the address bar: ๐ http://127.0.0.1:5000/
- ๐ Youโll see: โHello, World! This is my first web app!โ
You just ran a tiny server on your own computer. Flask gave you a webpage when you went to a URL (localhost). You can now create more routes (pages), or templates (HTML files), step by step!