Flash messages for Starlette framework.
Project description
Starlette-Flash
Flash messages for Starlette framework.
Installation
Install starlette_flash
using PIP or poetry:
pip install starlette_flash
# or
poetry add starlette_flash
Quick start
See example application in examples/ directory of this repository.
Setup
You must install SessionMiddleware to use flash messages.
Flashing messages
To flash a message use flash
helper.
from starlette_flash import flash
def index_view(request):
flash(request).add('This is a message.', 'success')
Using helpers
There are several predefined helpers exists which automatically set the category:
- success
- error
- info
- debug
from starlette_flash import flash
def index_view(request):
flash(request).success('This is a message.')
flash(request).error('This is a message.')
flash(request).info('This is a message.')
flash(request).debug('This is a message.')
Reading messages
To get current flash messages without removing them from session, use all
method:
from starlette_flash import flash
def index_view(request):
flash(request).success('This is a message.')
messages = flash(request).all()
print(messages) # {'category': 'success', 'message': 'This is a message.'}
Consuming messages
You can read messages one by one and then clear the storage by using consume
method.
from starlette_flash import flash
def index_view(request):
flash(request).success('This is a message.')
messages = []
for message in flash(request).consume():
messages.append(message)
print(messages) # {'category': 'success', 'message': 'This is a message.'}
print(flash(request).all()) # empty, messages has been consumed
You can iterate the flash bag to consume messages as well:
from starlette_flash import flash
def index_view(request):
flash(request).success('This is a message.')
messages = []
for message in flash(request):
messages.append(message)
print(messages) # {'category': 'success', 'message': 'This is a message.'}
print(flash(request).all()) # empty, messages has been consumed
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
starlette_flash-1.0.0.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for starlette_flash-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04e321754fa25ae7efca6f27ca03bd90d6bb662925474878b7acd37e3b47c0a1 |
|
MD5 | baa5f7cc62b0f6201535bd39a9a1a1b9 |
|
BLAKE2b-256 | edd32258527d33188cf0f17ba9212f88cee7bb7222334d0e0456491d5fb4bcf5 |