Performance: Flash messages for user feedback
Flash messages impact page load speed and interaction responsiveness by adding small DOM updates and rendering changes after user actions.
from flask import flash, redirect, url_for @app.route('/submit', methods=['POST']) def submit(): flash('Form submitted successfully!') return redirect(url_for('form'))
from flask import flash, redirect, render_template @app.route('/submit', methods=['POST']) def submit(): flash('Form submitted successfully!') return render_template('form.html')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload with flash | High (full DOM rebuild) | Multiple (full page) | High (full repaint) | [X] Bad |
| Redirect after flash message | Medium (new page load) | Few (initial load) | Medium | [!] OK |
| innerHTML append for flash | Medium (subtree rebuild) | Multiple (subtree) | Medium | [X] Bad |
| DOM appendChild for flash | Low (single node) | Single | Low | [OK] Good |
flash() in a Flask application?flash() doesflash() function stores these messages to be displayed once, then removed.flash in a Flask app?flash are imported directly from the flask package using from flask import flash.flash('Message') is the correct way to flash a message.from flask import Flask, flash, render_template_string, get_flashed_messages
app = Flask(__name__)
app.secret_key = 'secret'
@app.route('/')
def index():
flash('Welcome back!', 'info')
messages = get_flashed_messages(with_categories=True)
return render_template_string('''
{% for category, message in messages %}
<div class="{{ category }}">{{ message }}</div>
{% endfor %}
''', messages=messages)get_flashed_messages(with_categories=True) returns a list of (category, message) tuples.from flask import Flask, flash
app = Flask(__name__)
@app.route('/')
def index():
flash('Hello user!')
return 'Done'app.secret_key to be set.app.secret_key, so flashing will not work properly and may raise an error.flash('message', 'category') -- category as the second positional argument.get_flashed_messages(with_categories=True) directly in the template to unpack (category, message) and apply class="{{ category }}" for distinct styles.type='success' raises TypeError; retrieving without with_categories=True returns strings causing unpack error in for category, msg in messages.{{ msg }}
\n{% endfor %} [OK]Pass the test and this pattern moves unknown