Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wiltonsr/Flask-Toastr
Showing non-blocking notifications with Flask's flash and toastr.
https://github.com/wiltonsr/Flask-Toastr
flask flask-extension flask-extensions hacktoberfest jinja2 python27 python3 toastr
Last synced: 3 days ago
JSON representation
Showing non-blocking notifications with Flask's flash and toastr.
- Host: GitHub
- URL: https://github.com/wiltonsr/Flask-Toastr
- Owner: wiltonsr
- License: mit
- Created: 2019-02-22T15:47:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-23T18:01:16.000Z (about 2 years ago)
- Last Synced: 2024-10-13T09:21:22.617Z (22 days ago)
- Topics: flask, flask-extension, flask-extensions, hacktoberfest, jinja2, python27, python3, toastr
- Language: Python
- Homepage: https://pypi.org/project/Flask-Toastr/
- Size: 36.1 KB
- Stars: 52
- Watchers: 2
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Flask-Toastr
============[![](https://img.shields.io/badge/python-3.4+-blue.svg)](https://www.python.org/download/releases/3.4.0/) [![](https://img.shields.io/badge/python-2.7+-blue.svg)](https://www.python.org/download/releases/2.7.2/) [![](https://img.shields.io/github/license/ResidentMario/missingno.svg)](https://github.com/wiltonsr/Flask-Toastr/blob/master/README.md)
Showing Flask's flash non-blocking notifications in templates using [toastr](https://github.com/CodeSeven/toastr).
Donate to help keep this project maintained.
Quick Start
-----------Step 1: Initialize the extension:
from flask_toastr import Toastr
toastr = Toastr(app)Step 2: In your `` and bottom of ``sections of your base template add the following code:
```html
{{ toastr.include_jquery() }}
{{ toastr.include_toastr_css() }}
{{ toastr.message() }}
{{ toastr.include_toastr_js() }}
```
This extension also supports the [Flask application factory pattern](http://flask.pocoo.org/docs/latest/patterns/appfactories/) by allowing you to create a Toastr object and then separately initialize it for an app:
```python
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:
```python
@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, see [#14](https://github.com/wiltonsr/Flask-Toastr/issues/14) for details:
```python
@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](https://github.com/CodeSeven/toastr/releases) | Version of Toastr | https://github.com/CodeSeven/toastr/tags |
TOASTR_JQUERY_VERSION | '2.1.0' | [Releases from here](https://github.com/jquery/jquery/releases) | 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-Toastr2. Create and activate a virtual environment:
virtualenv env
source env/bin/activate3. Install requirements:
pip install -r 'example/requirements.txt'
4. Run the application:
python example/app.py
```Function Reference
------------------Consult the [toastr documentation](https://github.com/CodeSeven/toastr#toastr) for more details.
Development
-----------This extension is just a project to improve my python and flask skills. Any suggestions or tips are welcome.