Skip to main content

Showing non-blocking notifications in Flask templates using toastr.

Project description

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.

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

Flask-Toastr-0.5.8.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

Flask_Toastr-0.5.8-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Toastr-0.5.8.tar.gz.

File metadata

  • Download URL: Flask-Toastr-0.5.8.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for Flask-Toastr-0.5.8.tar.gz
Algorithm Hash digest
SHA256 133228fea7f9da9a68ca87c7b3ce51e20e747c7c7eb73c56e52e58f4231a53a1
MD5 2b8ea5b41507dcec2fee7237b53f12cd
BLAKE2b-256 cce933adc2d17855b6b78670c4b523882ee68c80287d7176ef1b1bb3bd2b4750

See more details on using hashes here.

File details

Details for the file Flask_Toastr-0.5.8-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Toastr-0.5.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a0446009c3855bfc717c80c61753afe752ebb1263215ebe1050c9ebdccf949f2
MD5 b0243cc238ccb16bc16157da4703486e
BLAKE2b-256 67ede081cd0746e3aaeb8fe360d7e9fca70cb6bd5c86f3d58eb64336abb0775f

See more details on using hashes here.

Supported by

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