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
Built Distribution
File details
Details for the file starlette_flash-1.1.0.tar.gz
.
File metadata
- Download URL: starlette_flash-1.1.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8318eda7d60e69deba339d6be893b8e3b4d867820b79afea81ff502866a58ac |
|
MD5 | d39d6ddad352e0c9cf628649df966950 |
|
BLAKE2b-256 | 8b66c27e4a64517d5d510df9716f6b2f135db8e2f0bbaebc396125f23493b7b1 |
File details
Details for the file starlette_flash-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: starlette_flash-1.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7935cee2a7f4482a8a6ca733190011d0f2c0eb9aff6b7366b6afe6ceba06c151 |
|
MD5 | 949bc2430cc6285b1cc87926ca9a9556 |
|
BLAKE2b-256 | d74dcc4ecf34f3606c26052b9727cdb0b60dc7a723d464657b8207453ffb74b4 |