Bird
Raised Fist0
Flaskframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the int converter do in a Flask route parameter like <int:id>?
easy
A. It matches any text including slashes.
B. It ensures the parameter is an integer and converts it automatically.
C. It converts the parameter to a string.
D. It converts the parameter to a floating-point number.

Solution

  1. Step 1: Understand the role of int converter

    The int converter tells Flask to accept only integer values for that URL part.
  2. Step 2: Check what happens with the parameter

    Flask automatically converts the matched URL part to an integer type before passing it to the view function.
  3. Final Answer:

    It ensures the parameter is an integer and converts it automatically. -> Option B
  4. Quick Check:

    int converter = integer parameter [OK]
Hint: int converter means only whole numbers allowed [OK]
Common Mistakes:
  • Thinking int converts to string
  • Confusing int with float converter
  • Assuming int matches any text
2. Which of the following is the correct syntax to define a Flask route that accepts a floating-point number parameter named price?
easy
A. @app.route('/item/<path:price>')
B. @app.route('/item/<int:price>')
C. @app.route('/item/<float:price>')
D. @app.route('/item/<str:price>')

Solution

  1. Step 1: Identify the correct converter for floating-point numbers

    The float converter is used to accept decimal numbers in the URL.
  2. Step 2: Match the syntax for route parameter

    The correct syntax is <float:price> inside the route decorator.
  3. Final Answer:

    @app.route('/item/<float:price>') -> Option C
  4. Quick Check:

    float converter = decimal number parameter [OK]
Hint: Use for decimal numbers in route parameters [OK]
Common Mistakes:
  • Using int instead of float for decimals
  • Using str which is not a Flask converter
  • Using path which allows slashes, not numbers
3. Given this Flask route:
@app.route('/files/<path:filename>')
def show_file(filename):
    return f"File requested: {filename}"

What will be the output if the URL is /files/docs/manual.pdf?
medium
A. File requested: docs/manual.pdf
B. File requested: manual.pdf
C. 404 Not Found error
D. File requested: docs

Solution

  1. Step 1: Understand the path converter behavior

    The path converter matches the entire remaining path including slashes.
  2. Step 2: Analyze the URL and parameter value

    The URL /files/docs/manual.pdf matches filename as docs/manual.pdf.
  3. Final Answer:

    File requested: docs/manual.pdf -> Option A
  4. Quick Check:

    path converter = matches full path with slashes [OK]
Hint: path converter captures slashes in URL parts [OK]
Common Mistakes:
  • Assuming path stops at first slash
  • Expecting 404 error for slashes in parameter
  • Confusing path with string converter
4. What is wrong with this Flask route definition?
@app.route('/product/<int:product_id>/<float:price>')
def show_product(product_id, price):
    return f"Product {product_id} costs {price}"

When accessing /product/abc/19.99, what error occurs?
medium
A. ValueError because 'abc' is not an integer
B. TypeError because price is not a float
C. No error, it works fine
D. 404 Not Found error due to URL mismatch

Solution

  1. Step 1: Check the int converter requirement

    The first parameter expects an integer, but 'abc' is not an integer.
  2. Step 2: Understand Flask routing behavior on type mismatch

    Flask does not raise ValueError but returns 404 Not Found if the URL does not match the route pattern.
  3. Final Answer:

    404 Not Found error due to URL mismatch -> Option D
  4. Quick Check:

    Type mismatch in route = 404 error [OK]
Hint: Wrong type in URL parameter causes 404, not exception [OK]
Common Mistakes:
  • Expecting Python exceptions instead of 404
  • Confusing ValueError with Flask routing errors
  • Assuming route matches any string
5. You want to create a Flask route that accepts a file path parameter which may include slashes, and an integer version number after it, like /download/docs/manual/2. Which route definition correctly uses converters to achieve this?
hard
A. @app.route('/download/<path:filepath>/<int:version>')
B. @app.route('/download/<int:version>/<path:filepath>')
C. @app.route('/download/<path:filepath><int:version>')
D. @app.route('/download/<path:filepath>/<float:version>')

Solution

  1. Step 1: Identify the order of parameters in the URL

    The URL pattern is /download/ followed by a path (which may include slashes), then a slash, then an integer version.
  2. Step 2: Use path for filepath and int for version in correct order

    The route must be /download/<path:filepath>/<int:version> to match the URL structure.
  3. Final Answer:

    @app.route('/download/<path:filepath>/<int:version>') -> Option A
  4. Quick Check:

    path then int in route matches URL structure [OK]
Hint: Use path converter before int for multi-part paths [OK]
Common Mistakes:
  • Swapping parameter order
  • Missing slash between parameters
  • Using float instead of int for version
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard