Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/christabor/flask_extras
Assorted generic flask views, blueprints, Jinja2 filters, macros, forms and more.
https://github.com/christabor/flask_extras
filter flask-views html jinja2 jinja2-filters jinja2-templates macros
Last synced: 4 days ago
JSON representation
Assorted generic flask views, blueprints, Jinja2 filters, macros, forms and more.
- Host: GitHub
- URL: https://github.com/christabor/flask_extras
- Owner: christabor
- License: mit
- Created: 2016-02-18T21:54:05.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-04T01:48:58.000Z (about 5 years ago)
- Last Synced: 2024-10-10T22:53:18.518Z (about 1 month ago)
- Topics: filter, flask-views, html, jinja2, jinja2-filters, jinja2-templates, macros
- Language: Python
- Homepage:
- Size: 849 KB
- Stars: 24
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - christabor/flask_extras - Assorted generic flask views, blueprints, Jinja2 filters, macros, forms and more. (Python)
README
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1f8f45e92a9b4ed1ab5029ee7a0e5534)](https://www.codacy.com/app/dxdstudio/flask_extras?utm_source=github.com&utm_medium=referral&utm_content=christabor/flask_extras&utm_campaign=badger)
[![Build Status](https://travis-ci.org/christabor/flask_extras.svg?branch=master)](https://travis-ci.org/christabor/flask_extras)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/christabor/flask_extras/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/christabor/flask_extras/?branch=master)
[![Code Climate](https://codeclimate.com/github/christabor/flask_extras/badges/gpa.svg)](https://codeclimate.com/github/christabor/flask_extras)
[![Coverage Status](https://coveralls.io/repos/github/christabor/flask_extras/badge.svg?branch=master)](https://coveralls.io/github/christabor/flask_extras?branch=master)
[![Code Health](https://landscape.io/github/christabor/flask_extras/master/landscape.svg?style=flat)](https://landscape.io/github/christabor/flask_extras/master)# Flask Extras
Assorted useful flask views, blueprints, Jinja2 template filters, and templates/macros.## Overall setup
As of `3.4.0`, filters and templates will automatically be registered and available through the following simple command:
```python
from flask_extras import FlaskExtras
app = Flask('myapp')
FlaskExtras(app)
```For the old way, check out [this page](wiki/old_setup.md)
## Available features
### Views
Import them like usual:
```python
from flask_extras.views import (
statuses,
)
```*Note:* each view must have a valid template in your apps templates dir. See each view for the required names and locations.
*Note:* each view has configuration helpers to inject or configure your app. See source for details.
### Macros
All macro DEMOS are available by cloning this repo and firing up the flask test server, e.g:
```shell
git clone https://github.com/christabor/flask_extras
cd flask_extras
virtualenv env
source env/bin/active
python setup.py install
cd test_app
python app.py
open http://localhost:5014
```#### Philosophy
Many macros leverage the structure of data to find a common mapping. For example, a dictionary looks a lot like a definition list (dl), and a list looks really like a ...list, in html terms. But stepping even further into things, more complex data structures can have fairly elegant mappings when using macros to hide away a lot of html cruft.
This makes rendering complex server side data easy, without having to do lots of transformation. This won't work for cases where every last DOM element need to be stylized, but many users will find incredibly powerful tools available to make UI development much easier.
**Many more macros** are available. You can use them like so:
```html
{% from 'macros.html' import list_group, objects2table %}
```For the most comprehensive docs, check out each [macro](flask_extras/macros/). Comment "docstrings" are inline using jinja2 comments (these are not rendered in your html).
Also, check the source and/or output to see what classes are available for style overrides.
### Statuses
Provides views for common status codes. Usage:
```python
app = statuses.inject_error_views(app)
```See source for more.
### Decorators
See the source for more. Usage example:
```python
from flask_extras.decorators import require_headersapp.route('/')
@require_headers(['X-Foo'])
def foo():
pass
```### Forms
#### WTForm Multi-step wizard
A WTForm extension for handling an arbitrary number of separate forms as a single, multi-step, multi-POST wizard. All state and data are handled by apps' session backend. Building forms is just like you're used to -- simple and intuitive. Just inherit the `MultiStepWizard` class and put a `__forms__` key on it, which is just a list of all the forms you want to use. *Note*: list order matters for your form steps.
Usage example:
```python
from flask.ext.wtf import FlaskFormfrom flask_extras.forms.wizard import MultiStepWizard
class MultiStepTest1(FlaskForm):
field1 = StringField(validators=[validators.DataRequired()],)
field2 = IntegerField(validators=[validators.DataRequired()],)class MultiStepTest2(FlaskForm):
field3 = StringField(validators=[validators.DataRequired()],)
field4 = IntegerField(validators=[validators.DataRequired()],)class MyCoolForm(MultiStepWizard):
__forms__ = [
MultiStepTest1,
MultiStepTest2,
]
```and an example route:
```python
from forms import MyCoolForm@app.route('/', methods=['GET', 'POST'])
def index():
curr_step = request.args.get('curr_step')
form_kwargs = dict(session_key='mycustomkey')
if curr_step is not None:
form_kwargs.update(curr_step=curr_step)
form = forms.MyCoolForm(**form_kwargs)
kwargs = dict(form=form)
if request.method == 'POST':
if form.validate_on_submit():
if form.is_complete():
data = form.alldata(combine_fields=True, flush_after=True)
flash('Form validated and complete! data = {}'.format(data),
'success')
return jsonify(data)
else:
flash('Great job, but not done yet ({} steps remain!).'.format(form.remaining))
else:
flash('Invalid form data.', 'error')
return render_template('index.html', **kwargs)
```and an example html page (using the [wtform_form](flask_extras/macros/macros.html) macro also available):
```html
{% if form.is_complete() %}
Complete!
{% else %}
-
{% if step == form.curr_step %}
current {{ step }}
{% else %}
{{ step }}
{% endif %}
{% if not loop.last %}
/
{% endif %}
{% for step in form.steps %}
{% endfor %}
{{ wtform_form(form,
classes=['form', 'form-horizontal'],
btn_classes=['btn btn-primary', 'btn-lg'],
align='right',
action=url_for('app.index'),
method='POST',
reset_btn=False,
horizontal=True,
) }}
{% endif %}
```