0
0
Flaskframework~10 mins

Template filters in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Template filters
Define filter function
Register filter with Flask
Render template
Template engine finds filter
Apply filter to variable
Display filtered output
This flow shows how a custom filter is defined, registered, and then used in a Flask template to transform data before display.
Execution Sample
Flask
from flask import Flask, render_template_string
app = Flask(__name__)

@app.template_filter('reverse')
def reverse_filter(s):
    return s[::-1]

@app.route('/')
def home():
    return render_template_string("{{ 'hello'|reverse }}")
This code defines a custom filter 'reverse' that reverses a string, registers it with Flask, and uses it in a template to display 'olleh'.
Execution Table
StepActionInputFilter AppliedOutput
1Define filter function'hello'reverseFunction ready
2Register filter with Flaskreverse_filterreverseFilter registered
3Render template'hello'reverseTemplate engine starts
4Find filter in template'hello'reverseFilter found
5Apply filter to variable'hello'reverse'olleh'
6Display filtered output'olleh'none'olleh' shown on page
7ExitN/AN/ARendering complete
💡 Rendering completes after filter application and output display.
Variable Tracker
VariableStartAfter Step 5Final
s'hello''hello''hello'
filtered_sN/A'olleh''olleh'
Key Moments - 3 Insights
Why do we need to register the filter with Flask?
Because Flask needs to know about the filter function to use it in templates, as shown in step 2 of the execution_table.
What happens if the filter name used in the template does not match the registered filter?
The template engine will not find the filter (step 4), causing an error or no transformation.
Does the filter change the original variable?
No, the filter returns a new transformed value (step 5), leaving the original variable unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after applying the filter at step 5?
A'HELLO'
B'hello'
C'olleh'
D'error'
💡 Hint
Check the 'Output' column at step 5 in the execution_table.
At which step does Flask register the custom filter?
AStep 2
BStep 1
CStep 4
DStep 6
💡 Hint
Look for the 'Register filter with Flask' action in the execution_table.
If the filter function returned s.upper() instead of s[::-1], what would be the output at step 5?
A'olleh'
B'HELLO'
C'hello'
D'Error'
💡 Hint
Consider how the filter transforms the input string in the execution_table.
Concept Snapshot
Template filters in Flask:
- Define a Python function to transform data.
- Register it with @app.template_filter('name').
- Use in templates with {{ variable|name }}.
- Filters transform data before display.
- Original data stays unchanged.
Full Transcript
In Flask, template filters let you change how data looks in your web pages. You write a small function that changes the data, like reversing a string. Then you tell Flask about this function using @app.template_filter with a name. When Flask shows a page, it looks for that name in the template and runs your function on the data. The changed data is shown on the page, but the original data stays the same. This helps keep your code clean and your pages nice.