Running the Dev Server
During development you typically run:
flask run
This starts Werkzeug’s development server.
Typical output
Section titled “Typical output”You’ll usually see:
- the URL (like
http://127.0.0.1:5000) - that it’s a development server
Host and port
Section titled “Host and port”- Default host:
127.0.0.1(only accessible from your machine) - Default port:
5000
You can change them:
flask run --host 0.0.0.0 --port 8000When should you use 0.0.0.0?
Section titled “When should you use 0.0.0.0?”Use it when you want access from another device on your network.
Be careful: exposing dev servers is not recommended on public networks.
What happens when you visit the page?
Section titled “What happens when you visit the page?”- Browser connects to the host/port
- Sends
GET / - Dev server forwards to Flask app
- Flask finds a matching route and runs your view function
- Response is returned
If you get a 404, it usually means:
- you didn’t define the route, or
- your URL doesn’t match the route rule
What the routing table looks like before any request
Section titled “What the routing table looks like before any request”Flask builds a url_map at import time. Every rule you registered is in it — plus one
you did not write:
for rule in app.url_map.iter_rules():
print(rule, rule.endpoint, sorted(rule.methods - {"HEAD", "OPTIONS"}))/ index ['GET']
/api/items items ['GET', 'POST']
/static/<path:filename> static ['GET'] <- added automatically
/user/<name> user ['GET']HEAD and OPTIONS are attached to every GET rule for you, and a /static/ route
exists from the moment the app object is created.
How a request finds a view
Section titled “How a request finds a view” flowchart TD
R["incoming request"] --> M{"does a rule match the path?"}
M -->|"no"| E404["404 Not Found"]
M -->|"yes, but wrong method"| E405["405 Method Not Allowed
with an Allow header"]
M -->|"yes"| V["run the view function"]
V --> OK["200 and your return value"]
V -->|"raises"| E500["500, or the debugger if debug is on"]
Measured against the app above:
| request | status | note |
|---|---|---|
GET /nope | 404 | no rule matches |
DELETE /api/items | 405 | rule exists, method does not |
Allow header on that 405 | GET, POST, OPTIONS, HEAD |
A 405 rather than a 404 is a useful signal: the URL is right and the verb is wrong.
The trailing slash rule is asymmetric
Section titled “The trailing slash rule is asymmetric”@app.route("/dir/") # declared WITH a slash
def d(): ...
@app.route("/file") # declared WITHOUT one
def f(): ...| declared | requested | result |
|---|---|---|
/dir/ | /dir | 308 redirect to /dir/ |
/file | /file/ | 404 |
A rule ending in a slash behaves like a directory: ask without the slash and Flask
redirects you. A rule without one behaves like a file: the slashed form simply does not
exist. The redirect costs an extra round trip, and a POST that gets redirected can
lose its body in some clients — so declare the form you intend and link to it exactly.
See it move
Section titled “See it move”Check yourself
Section titled “Check yourself”-
A route is declared as @app.route('/dir/') and a client requests /dir. What happens?
A rule ending in a slash behaves like a directory and Flask redirects. The reverse is not symmetric: a rule declared '/file' requested as '/file/' gives 404.
pch.quizShowAnswer
C — 308 redirect to /dir/ — A rule ending in a slash behaves like a directory and Flask redirects. The reverse is not symmetric: a rule declared '/file' requested as '/file/' gives 404.
-
DELETE /api/items returns 405 rather than 404. What does that distinction tell you?
Flask matches the path first, then the method. A 405 means the URL is right and the verb is wrong, and the response carries an Allow header listing what is permitted.
pch.quizShowAnswer
B — the path matched a rule but the method is not allowed for it — Flask matches the path first, then the method. A 405 means the URL is right and the verb is wrong, and the response carries an Allow header listing what is permitted.
-
Which rule appears in app.url_map without you writing it?
Creating the Flask object registers the static route. Iterating url_map also shows HEAD and OPTIONS attached automatically to every GET rule.
pch.quizShowAnswer
B — /static/<path:filename> — Creating the Flask object registers the static route. Iterating url_map also shows HEAD and OPTIONS attached automatically to every GET rule.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading