Skip to main content

Custom flash system allowing custom keyword arguments for Flask.

Project description

flask-flashy

Custom flash system allowing custom keyword arguments for Flask.

How it works

Flask's default flash() function simply takes two strings and passes them into the session (the message and category, eg flash('Username Is In Use', 'warning')), which is then obtained and cleared by get_flashed_messages() in jinja.

flask-flashy has a class-based flashing system, allowing you to pass in as many keyword arguments as you'd like, while still keeping the default message and category to align with flask and allow for minimal edits to convert (eg flash('Username Is In Use. Login Instead?', 'warning', url=url_for('login'), timestamp=datetime.now() )).

Installation & Basic Usage

Install via pip:

pip install flask-flashy

After installing, wrap your Flask app with flashy...

from flask import Flask
from flask_flashy import Flashy, flash

app = Flask(__name__)
flashy = Flashy(app)

@app.route("/")
def index():
  flash('Hello flashy!')
  return render_template('index.html')

...or initialize later for use with blueprints and app factories.

from flask import Flask
from flask_flashy import Flashy, flash

flashy = Flashy()

app = Flask(__name__)

flashy.init_app(app)

@app.route("/")
def index():
  flash('Hello flashy!')
  return render_template('index.html')

NOTE: Flashy must be initialized in order to create the context processor which allows get_flashy_messages() to be used in jinja templates.

Examples

After initializing the app, simply call for a flash in a route, passing in the message, optionally a second string as a category (will default to 'message' if not provided), and as many custom keyword arguments as you'd like.

@app.route('/')
def index():
  flash('Woah there partner!', 'danger', url='https://example.com/')
  return render_template('index.html')

Then get the flashes in the template using jinja:

{% with messages = get_flashy_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
{% if message.url %}
<a href="{{ message.url }}"><li class="{{ message.category }}">{{ message.message }}</li></a>
{% else %}
<li class="{{ message.category }}">{{ message.message }}</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endwith %}

NOTE: Yes, gone are the days of having to specify with_categories=True, it passes everything automatically.

And style and use to your hearts content.

Don't like flashy taking over the flash() function? Want to use both the flash and flashy systems in the same project (for whatever reason)? Simply import it with a custom name, such as...

from flask-flashy import flash as flashyFlash

TODO

  • Category sorting like vanilla flask
  • Fun flask-like logo
  • ???

Release History

  • 0.2.0 - Made flash() and get_flashed_messages() their own functions rather than methods within Flashy, and more verbose docstrings.
  • 0.1.0 - Initial release on pypi.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_flashy-0.2.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file flask_flashy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: flask_flashy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for flask_flashy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04e975ac4517eadbd444bc61c5d97576555064e2f24903c5feb281f060f42fc1
MD5 53e4ee28b16f85ecc08d008bc10fdd9e
BLAKE2b-256 3517d19f53dbdada114b8d6a9b3352007c4f0424a219e03853ab0b0583adf54f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page