Your First Flask App with uv: From an Empty Folder to a Page in Debug Mode
uv init, uv add flask, a five-line main.py and flask --app main run --debug. What each of the seven packages does, what the lock file pins, and what the 200 and 404 log lines tell you.
What you will learn
- Create a Flask project with uv init and uv add
- Explain what pyproject.toml and uv.lock each record
- Run an app with flask --app main run --debug and read every line it prints
- Recognise the reloader, the debugger and why debug mode stays on your own machine
Before you start
- Getting started with Flask
- uv basics
This is the project the whole Flask blog grows out of. We start with an empty folder, install Flask with uv, write one small file and see a page in the browser. Two names stay fixed from here on: the file is main.py, and the object inside it is called app. That way every later lesson carries on with the same project.
If you haven't met Flask yet, read Getting started with Flask first. For uv itself, see Python project setup with uv.
A uv project
In an empty folder called S01L02_FirstApp, open the terminal and run:
uv init --bare
Initialized project `s01l02-firstapp`
--bare creates just one file, pyproject.toml. uv took the folder name, made it lowercase and swapped the underscore for a dash.
Older tutorials do three steps here: create a virtual environment, activate it, then pip install. uv handles all of that for you. (If you want to see what the old way does under the hood, Python virtual environments walks through it.)
Installing Flask
uv add flask
Using CPython 3.12.14
Creating virtual environment at: .venv
Resolved 8 packages in 12ms
Installed 7 packages in 77ms
+ blinker==1.9.0
+ click==8.5.1
+ flask==3.1.3
+ itsdangerous==2.2.0
+ jinja2==3.1.6
+ markupsafe==3.0.4
+ werkzeug==3.1.9
uv picked Python 3.12, created .venv and installed Flask 3.1.3. Flask brings six helper packages along:
| Package | What it does |
|---|---|
| Werkzeug | requests, responses and the development server |
| Jinja2 | renders templates |
| Click | gives you the flask command |
| ItsDangerous | signs data, like the session cookie |
| MarkupSafe | keeps text safe inside templates |
| Blinker | sends signals (you won't need it for a while) |
Seven packages in total. That short list is why Flask feels light.
pyproject.toml and uv.lock
uv add wrote Flask into the dependencies list:
[project]
name = "s01l02-firstapp"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"flask>=3.1.3",
]
Next to it, uv.lock appeared. It pins the exact version of every package, so anyone who opens the folder gets the same setup. That's the job a requirements file used to do with pip freeze. To check what's installed:
uv pip show flask
Name: flask
Version: 3.1.3
Location: S01L02_FirstApp\.venv\Lib\site-packages
Requires: blinker, click, itsdangerous, jinja2, markupsafe, werkzeug
Required-by:
So pyproject.toml says what you want, and uv.lock records exactly what you got.
The app in main.py
Add a new file at the top of the folder, main.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello blog, my first app."
Line by line: import the Flask class. Create the application object; __name__ tells Flask where the app lives so it can find templates and static files next to it later. @app.route("/") registers the function below it for the home page. home returns one plain line of text. That's a complete Flask app.
Running it
uv run flask --app main run --debug --port 8000
* Serving Flask app 'main'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 123-456-789
127.0.0.1 - - "GET / HTTP/1.1" 200 -
127.0.0.1 - - "GET /nothing HTTP/1.1" 404 -
uv runstarts the command inside the virtual environment, so there's nothing to activate.--app maintells Flask which file to load: the file name without.py. Flask finds theappobject inside it. If the name doesn't match your file, Flask says it could not import it, so that's the first thing to check.- The warning says this is a development server. Fine on your machine; a deployed site uses a production server.
127.0.0.1is your own computer, followed by the port.
Two requests: 200 and 404
Open the address in the browser. It sends GET /, Flask matches / to home, and the response comes back with status 200 and a content type of text/html; charset=utf-8. The page shows Hello blog, my first app., and the terminal logs one line for that request. That log line is how you'll check your requests from now on.
Ask for /nothing and there's no rule to match, so Flask answers 404 on its own.
What --debug gives you
- Restarting with stat: the reloader is on. Save
main.pyand the server restarts by itself, so you just refresh the browser. - Debugger is active: if your code raises an error, the browser shows the full traceback with the line that failed.
- Debugger PIN: protects that page. The debugger can run code on the server, so keep debug mode for your own machine and never switch it on for a deployed site.
Press Ctrl+C in the terminal to stop the server.
The project folder
| Item | Job |
|---|---|
main.py | the app |
pyproject.toml | what the project needs |
uv.lock | the exact versions |
.venv | Python and the seven packages; never edit it or share it, uv sync rebuilds it from the lock file |
Common mistakes
--appdoesn't match the file.--app mainneedsmain.py. Otherwise Flask can't import the app.- Debug mode on a public server. The debugger can run code on the machine.
- Committing
.venv. Commitpyproject.tomlanduv.lock; the environment is rebuilt from them.
Try it yourself
Change the text that home returns, save the file and refresh the browser: the reloader picks it up. Then add a second route for /about and open it. The next step is routes, templates and url_for.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Regular Expressions in Python: search, findall, Groups and Greedy vs Lazy
A regex is a stencil for a shape of text. Watch it slide and back up, build patterns with \d, \w, quantifiers and anchors, use search, findall, sub and split, pull parts out with named groups, and test AI-written patterns on edge cases.
Testing Python Code with pytest: assert, parametrize, fixtures and pytest.raises
A test is a line on a checklist your code must pass every time. Catch a discount bug with assert, read the E lines in the pytest report, cover cases with parametrize, share a cart with a fixture, test errors with pytest.raises, and see why a passing test from an AI tool can prove nothing.
Async Python: async def, await, asyncio.gather and the Blocking Trap
One cook, several pots: async lets one program wait on many things at once. See what async def and await really do, why three awaits in a row are no faster, how gather and TaskGroup bring 0.6 seconds down to 0.2, and the two mistakes that catch almost everyone.
Django, FastAPI or Flask? The Same Blog Feature in Three Shapes, and How to Choose
One small project lists two blog posts three times: as a Django view, a Flask route and a FastAPI endpoint. See each shape with its real output, what each framework includes, and a three-question way to choose.
