bag.web.pyramid.flash_msg module

Advanced flash messages scheme for Pyramid.

This module integrates bag.web.flash_msg into Pyramid. (That module can be used with any web framework.)

Why?

It is natural to have a single class that knows:

  • the content of a flash message in either plain or rich form(s)
  • the level (color) of the message, such as info, danger, success etc.
  • different ways of rendering the message on the page
  • whatever else you want.

The queue problem

Some Pyramid applications have used flash message queues to separate them by level (danger, warning, info or success) in code such as this:

request.session.flash(str(e), 'danger')

The problem with this is that messages won’t appear in the order in which they were created. Because each queue is processed separately in the template, order is lost and messages are grouped by level. This is undesirable and confusing to the user.

Our solution stores the level with the message, so you can add all messages to the default queue and process only that queue in templates.

Installation

At web server startup time, add this simple line:

config.include('bag.web.pyramid.flash_msg')

Usage

Add messages to the queue like this:

request.add_flash(
    plain="Your password has been changed, thanks.",
    level='warning',
    close=False,  # user will NOT see an X button to close the alert
    allow_duplicate=False,  # do not bother the user with repeated text
    )

Then you can simply do, in templates:

${render_flash_messages()}

…to render the messages with Bootstrap styling. The Jinja2 version is {{ render_flash_messages() | safe }}.

If you don’t like the result in any way, just loop over the flash messages yourself and do what you want – no need to use this feature.

bag.web.pyramid.flash_msg.add_flash(request, allow_duplicate=False, **kw)[source]

Add a flash message to the user’s session. For convenience.

Return type:None
bag.web.pyramid.flash_msg.includeme(config)[source]

Integrate into Pyramid web apps.

Return type:None
bag.web.pyramid.flash_msg.make_templates_able_to_render_flash_msgs(config)[source]

Make a render_flash_messages() function available to every template.

Also make available in templates a function flash_msgs_as_dicts() which returns a list of the flash messages, each one represented as a dictionary. This is useful to turn the flash messages into JSON.

If you want to use the queues feature (not recommended), add this configuration setting:

bag.flash.use_queues = true
Return type:None
bag.web.pyramid.flash_msg.render_flash_messages(request)[source]
Return type:str
bag.web.pyramid.flash_msg.render_flash_messages_from_queues(request)[source]

Avoid. This method is for compatibility with other systems only.

Some developers are using queues named after bootstrap message flavours. I think my system (using only the default queue ‘’) is better, because FlashMessage already supports a level attribute, but this function provides a way to display their flash messages, too.

You can set QUEUES to something else if you like, from user code.

Return type:str