Flash Message Management in Python, Flask

Flash Message

You will see here how to do flash message management using Python, Flask. Ideally almost every application needs to provide some meaningful message to end user after some activities performed by end users. Such activities may be registration on a site, login, payment, etc. So after each operation whether success or failure most of the time we need to provide meaningful message so that end user will understand what happens or what would be the next step.

Flask provides a really simple way to give feedback to a user with the flashing system. The flashing system basically makes it possible to record a message at the end of a request and access it next request and only next request. This is usually combined with a layout template that does this.

Prerequisites

Python 3.7.4/3.11.5, Flask 1.1.1/2.3.3, Windows 10/11 64 bit

Configuring Flask

Create below configuration before you use session or flash from Flask framework.

The secret key ideally should be the encrypted one.

from flask import Flask

app = Flask(__name__)
app.secret_key = "secret key"

Putting Message into flash

Simple Flash Message

I will put simple message into flash. I will render a simple template.

@app.route('/')
def app_session():
	flash('This is a flash message')
	return render_template('template.html')

The corresponding template.html file is given below with the code. To retrieve message or feedback from flash simply use similar to below code into the template file and display:

{% with messages = get_flashed_messages() %}
  {% if messages %}
	<ul>
	{% for message in messages %}
	  <li>{{ message }}</li>
	{% endfor %}
	</ul>
  {% endif %}
{% endwith %}

The browser displays the output as shown in the below image:

flask flash

Flash with Category

It is possible to provide category when flashing a message. The default category if nothing is provided is message.

For example, error messages could be displayed with a red background and success messages could be displayed with a green background .

@app.route('/')
def app_session():
	flash('This is a flash error message', 'error')
	flash('This is a flash success message', 'success')
	return render_template('template.html')

The corresponding template.html file with the below code is given. The categorical flash messages could be retrieved in the following way:

<div>
	{% with messages = get_flashed_messages(with_categories=true) %}
	  {% if messages %}
		<ul>
		{% for category, message in messages %}
		  <li class="{{ category }}">{{ message }}</li>
		{% endfor %}
		</ul>
	  {% endif %}
	{% endwith %}
</div>

The output in the browser is shown similar to the following image:

flask flash

Filter Message

Optionally you can pass a list of categories which filters the results of get_flashed_messages(). This is useful if you wish to render each category in a separate block.

@app.route('/')
def app_session():
	flash('This is a flash error message', 'error')
	flash('This is a flash success message', 'success')
	return render_template('template.html')

The template.html file is written with the following code. For example, I am filtereing messages which have category error.

<div>
	{% with errors = get_flashed_messages(category_filter=["error"]) %}
	  {% if errors %}
		<ul>
		{%- for msg in errors %}
			<li>{{ msg }}</li>
		{% endfor -%}
		</ul>
	  {% endif %}
	{% endwith %}
</div>

The following image shows what would be the output in the browser:

flask flash

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *