0
0
Flaskframework~10 mins

Parameter type converters (int, float, path) in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parameter type converters (int, float, path)
Define route with converter
Request URL received
Extract parameter from URL
Apply type converter
Pass converted value to view function
Return response with typed parameter
The web server reads the URL, extracts parameters, converts them to the specified type (int, float, or path), then passes them to the function.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/item/<int:id>')
def item(id):
    return f"Item ID is {id}"
Defines a route that accepts an integer parameter from the URL and returns it in the response.
Execution Table
StepURL PartConverter AppliedConverted ValueFunction ParameterResponse
1/item/42int42id=42"Item ID is 42"
2/item/3.14intErrorNo call404 Not Found (conversion fails)
3/item/abcintErrorNo call404 Not Found (conversion fails)
4/price/3.14float3.14price=3.14"Price is 3.14"
5/price/42float42.0price=42.0"Price is 42.0"
6/files/docs/readme.txtpathdocs/readme.txtfilepath='docs/readme.txt'"File path is docs/readme.txt"
💡 Execution stops when URL matches route and parameter converts successfully; otherwise 404 error.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 6
idNone42NoneNone
priceNoneNone3.14None
filepathNoneNoneNonedocs/readme.txt
Key Moments - 2 Insights
Why does accessing /item/3.14 cause a 404 error when using <int:id>?
Because the int converter only accepts whole numbers. The execution_table row 2 shows conversion fails for '3.14', so the function is not called.
What is the difference between <path:filepath> and a normal string parameter?
The path converter accepts slashes inside the parameter, allowing multi-level paths. Normal string stops at the first slash. See execution_table row 6 for example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the function parameter 'price' at step 5?
A3.14
B42.0
C42
DError
💡 Hint
Check the 'Converted Value' and 'Function Parameter' columns at step 5 in execution_table.
At which step does the path converter handle a parameter with slashes?
AStep 1
BStep 4
CStep 6
DStep 2
💡 Hint
Look for the 'path' converter in the 'Converter Applied' column in execution_table.
If you change <int:id> to <float:id> in the route, what happens at step 2 (/item/3.14)?
Aid becomes 3.14 as float
Bid becomes 3 as int
CConversion fails, 404 error
DFunction not called
💡 Hint
Refer to how float converter works in execution_table steps 4 and 5.
Concept Snapshot
Flask URL converters:
- <int:name>: accepts integers only
- <float:name>: accepts floating numbers
- <path:name>: accepts strings including slashes
Use converters in route to auto-convert URL parts to Python types.
If conversion fails, Flask returns 404 error.
Pass converted values as function parameters.
Full Transcript
In Flask, you can specify parameter type converters in your route URLs to automatically convert parts of the URL to Python types. Common converters are int for integers, float for decimal numbers, and path for strings that can include slashes. When a request URL matches a route, Flask extracts the parameter and tries to convert it using the specified converter. If conversion succeeds, the converted value is passed to the view function as an argument. If conversion fails, Flask returns a 404 error. For example, a route '/item/<int:id>' only accepts integer IDs. If you visit '/item/42', the id parameter is 42 as an integer. But '/item/3.14' fails because 3.14 is not an integer. The path converter allows slashes inside the parameter, useful for file paths like '/files/docs/readme.txt'. This way, Flask helps you get typed parameters easily from URLs.