0
0
Flaskframework~10 mins

Flash message categories in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Flash message categories
Trigger flash message
Store message with category
Redirect or render template
Retrieve flashed messages
Display messages grouped by category
User sees categorized messages
Flash messages are stored with categories, retrieved later, and displayed grouped by those categories.
Execution Sample
Flask
from flask import flash, get_flashed_messages

flash('Login successful', 'success')
flash('Invalid password', 'error')
messages = get_flashed_messages(with_categories=true)
This code flashes two messages with categories and retrieves them grouped by category.
Execution Table
StepActionMessage StoredCategoryMessages Retrieved
1flash('Login successful', 'success')Login successfulsuccess[]
2flash('Invalid password', 'error')Invalid passworderror[]
3get_flashed_messages(with_categories=true)--[('success', 'Login successful'), ('error', 'Invalid password')]
4Display messages grouped by category--Show 'Login successful' as success, 'Invalid password' as error
💡 All flashed messages retrieved and ready for display grouped by their categories.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
messages[][][][('success', 'Login successful'), ('error', 'Invalid password')][('success', 'Login successful'), ('error', 'Invalid password')]
Key Moments - 2 Insights
Why do we use categories with flash messages?
Categories help group messages by type (like success or error) so the display can style or organize them differently, as shown in step 3 and 4 of the execution_table.
What happens if we call get_flashed_messages without with_categories=true?
Only message texts are returned without categories, so you lose the ability to group or style messages by type. This is why step 3 uses with_categories=true.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does get_flashed_messages(with_categories=true) return?
A[]
B['Login successful', 'Invalid password']
C[('success', 'Login successful'), ('error', 'Invalid password')]
D[('Login successful', 'success'), ('Invalid password', 'error')]
💡 Hint
Check the Messages Retrieved column at step 3 in the execution_table.
At which step are the flash messages actually stored with their categories?
AStep 1 and Step 2
BStep 3
CStep 4
DStep 0
💡 Hint
Look at the Message Stored and Category columns in steps 1 and 2.
If we call get_flashed_messages() without with_categories=true, how would the messages variable change after step 3?
AIt would be empty
BIt would contain only message texts without categories
CIt would contain categories but no messages
DIt would cause an error
💡 Hint
Refer to key_moments about get_flashed_messages without with_categories=true.
Concept Snapshot
Flash messages in Flask store short messages for the next request.
Use flash(message, category) to add a message with a category.
Retrieve with get_flashed_messages(with_categories=true) to get (category, message) pairs.
Categories help display messages differently (e.g., success, error).
Common categories: 'success', 'error', 'warning', 'info'.
Full Transcript
In Flask, flash messages let you send short messages to users that appear after a page reload or redirect. You add a message with flash(message, category), where category is a label like 'success' or 'error'. These categories help you show messages in different styles or places. When you want to show the messages, you call get_flashed_messages(with_categories=true) to get a list of pairs with category and message. Then you can loop over them in your template and display each message with its style. This flow helps keep user feedback clear and organized.