Skip to main content

Flask-Toastr

Showing Flask's flash non-blocking notifications in templates using toastr.

Donate to help keep this project maintained. Donate with PayPal button

Quick Start

Step 1: Initialize the extension:

from flask_toastr import Toastr
toastr = Toastr(app)

Step 2: In your <head> and bottom of <body>sections of your base template add the following code:

<html>
  <head>
    {{ toastr.include_jquery() }}
    {{ toastr.include_toastr_css() }}
    {{ toastr.message() }}
  </head>
  <body>
    {{ toastr.include_toastr_js() }}
  </body>
</html>

This extension also supports the Flask application factory pattern by allowing you to create a Toastr object and then separately initialize it for an app:

    toastr = Toastr()

    def create_app(config):
        app = Flask(__name__)
        app.config.from_object(config)
        # initialize toastr on the app within create_app()
        toastr.init_app(app)

    app = create_app(prod_config)

Note that jQuery is required. If you are already including it on your own then you can remove the include_jquery() line. Secure HTTP is used if the request under which these are executed is secure.

The include_jquery(), include_toastr_js() and include_toastr_css() methods take some optional arguments. If you pass a version argument to any of these calls, then the requested version will be loaded from the default CDN. In addition, it is also possible to pass js_filename and css_filename to include_toastr_js() and include_toastr_css(), respectively.

Step 3: Use the flash() method with or without category in your views. For example:

@app.route('/')
def index():
    flash("All OK")
    flash("All OK", 'success')
    flash("All Normal", 'info')
    flash("Not So OK", 'error')
    flash("So So", 'warning')
    return render_template('index.html')

Or you could also use flash() method with custom titles:

@app.route('/')
def index():
    flash("Message", 'Custom Title')
    flash({'title': "Custom Title", 'message': "Error Message"}, 'error')
    return render_template('index.html')

Step 4: Enjoy

Parameters

Config Default Possible Values Description Reference
TOASTR_VERSION '2.1.4' Releases from here Version of Toastr https://github.com/CodeSeven/toastr/tags
TOASTR_JQUERY_VERSION '2.1.0' Releases from here Version of JQuery https://releases.jquery.com/jquery/
TOASTR_CSS_FILENAME 'toastr.min.css' CDN file name CSS Filename used in CDN Toastr's link https://github.com/CodeSeven/toastr#cdns
TOASTR_JS_FILENAME 'toastr.min.js' CDN file name JS Filename used in CDN Toastr's link https://github.com/CodeSeven/toastr#cdns
TOASTR_CLOSE_BUTTON 'true' 'true' or 'false' Enable or Disable close button https://github.com/CodeSeven/toastr#close-button
TOASTR_SHOW_EASING 'swing' 'swing' or 'linear' Override the animation easing to show the toasts https://github.com/CodeSeven/toastr#animation-options
TOASTR_HIDE_EASING 'linear' 'swing' or 'linear' Override the animation easing to hide the toasts https://github.com/CodeSeven/toastr#animation-options
TOASTR_CLOSE_EASING 'linear' 'swing' or 'linear' Override the animation easing to close the toasts https://github.com/CodeSeven/toastr#animation-options
TOASTR_SHOW_METHOD 'fadeIn' 'fadeIn', 'fadeOut', 'slideDown', 'slideUp, 'show', 'hide' Override the animation method to show the toasts https://github.com/CodeSeven/toastr#animation-options
TOASTR_HIDE_METHOD 'fadeOut' 'fadeIn', 'fadeOut', 'slideDown', 'slideUp, 'show', 'hide' Override the animation method to hide the toasts https://github.com/CodeSeven/toastr#animation-options
TOASTR_CLOSE_METHOD 'fadeOut' 'fadeIn', 'fadeOut', 'slideDown', 'slideUp, 'show', 'hide' Override the animation method to close the toasts https://github.com/CodeSeven/toastr#animation-options
TOASTR_TIMEOUT 15000 any int value (in ms) Time to notification close https://github.com/CodeSeven/toastr#timeouts
TOASTR_EXTENDED_TIMEOUT 1000 any int value (in ms) Time to notification close after hover mouse or click https://github.com/CodeSeven/toastr#timeouts
TOASTR_POSITION_CLASS 'toast-top-right' 'toast-top-right', 'toast-bottom-right', 'toast-bottom-left', 'toast-top-left', 'toast-top-full-width', 'toast-bottom-full-width', 'toast-top-center', 'toast-bottom-center' Notification Positon https://codeseven.github.io/toastr/demo.html
TOASTR_PREVENT_DUPLICATES 'false' 'true' or 'false' Doesn't show same notification https://github.com/CodeSeven/toastr#prevent-duplicates
TOASTR_NEWS_ON_TOP 'false' 'true' or 'false' Notification's aparition order https://github.com/CodeSeven/toastr#display-sequence
TOASTR_PROGRESS_BAR 'true' 'true' or 'false' Enable or Disable progress bar https://github.com/CodeSeven/toastr#progress-bar
TOASTR_OPACITY True True or False Enable or Disable notification's opacity https://stackoverflow.com/a/17640150/7041939
Note: The values `true` and `false` are passed to JS, that is Case Sensitive.
Therefore they must be passed in lowercase.

Examples

To run the example in your local environment::

  1. Clone the repository::

        git clone https://github.com/wiltonsr/Flask-Toastr.git
        cd Flask-Toastr

  2. Create and activate a virtual environment::

        virtualenv env
        source env/bin/activate

  3. Install requirements::

        pip install -r 'example/requirements.txt'

  4. Run the application::

        python example/app.py

Function Reference

Consult the toastr documentation for more details.

Development

This extension is just a project to improve my python and flask skills. Any suggestions or tips are welcome.

Release files for Flask-Toastr 0.5.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Toastr 0.5.8
File Size Uploaded
Flask-Toastr-0.5.8.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Toastr 0.5.8
File Interpreter ABI Platform
Flask_Toastr-0.5.8-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / Flask-Toastr-0.5.8.tar.gz

Download URL Flask-Toastr-0.5.8.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
133228fea7f9da9a68ca87c7b3ce51e20e747c7c7eb73c56e52e58f4231a53a1
BLAKE2b-256 checksum
How to use checksums
cce933adc2d17855b6b78670c4b523882ee68c80287d7176ef1b1bb3bd2b4750
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / Flask_Toastr-0.5.8-py3-none-any.whl

Download URL Flask_Toastr-0.5.8-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a0446009c3855bfc717c80c61753afe752ebb1263215ebe1050c9ebdccf949f2
BLAKE2b-256 checksum
How to use checksums
67ede081cd0746e3aaeb8fe360d7e9fca70cb6bd5c86f3d58eb64336abb0775f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.5.8 This release

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

1 release file

0.5.3

1 release file

0.5.2

2 release files

0.5.1

2 release files

0.5

2 release files

0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page