Skip to content

Commit 6e60a1e

Browse files
committed
adding further flask code examples
1 parent 4774121 commit 6e60a1e

File tree

5 files changed

+361
-28
lines changed

5 files changed

+361
-28
lines changed

content/pages/examples/flask/flask-blueprints-blueprint.markdown

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,111 @@ def create_author():
323323
```
324324

325325

326-
## Example 5 from flask-debugtoolbar
326+
## Example 5 from Flask-Bootstrap
327+
[flask-bootstrap](https://github.com/mbr/flask-bootstrap)
328+
([PyPI package information](https://pypi.org/project/Flask-Bootstrap/))
329+
makes it easier to use the [Bootstrap CSS framework](/bootstrap-css.html)
330+
in your [Flask](/flask.html) applications with less boilerplate
331+
code. The project was primarily created by
332+
[Marc Brinkmann @mbr](https://github.com/mbr) and the source code is
333+
open sourced under the
334+
[Apache 2.0 license](https://github.com/mbr/flask-bootstrap/blob/master/LICENSE).
335+
336+
[**Flask-Bootstrap / flask_bootstrap / __init__.py**](https://github.com/mbr/flask-bootstrap/blob/master/flask_bootstrap/./__init__.py)
337+
338+
```python
339+
# __init__.py
340+
341+
import re
342+
343+
~~from flask import Blueprint, current_app, url_for
344+
345+
try:
346+
from wtforms.fields import HiddenField
347+
except ImportError:
348+
349+
def is_hidden_field_filter(field):
350+
raise RuntimeError('WTForms is not installed.')
351+
else:
352+
353+
def is_hidden_field_filter(field):
354+
return isinstance(field, HiddenField)
355+
356+
357+
from .forms import render_form
358+
359+
__version__ = '3.3.7.1.dev1'
360+
BOOTSTRAP_VERSION = re.sub(r'^(\d+\.\d+\.\d+).*', r'\1', __version__)
361+
JQUERY_VERSION = '1.12.4'
362+
HTML5SHIV_VERSION = '3.7.3'
363+
RESPONDJS_VERSION = '1.4.2'
364+
365+
366+
class CDN(object):
367+
368+
369+
370+
## ... source file abbreviated to get to Blueprint examples ...
371+
372+
373+
filename = '%s.min.%s' % tuple(filename.rsplit('.', 1))
374+
375+
cdns = current_app.extensions['bootstrap']['cdns']
376+
resource_url = cdns[cdn].get_resource_url(filename)
377+
378+
if resource_url.startswith('//') and config['BOOTSTRAP_CDN_FORCE_SSL']:
379+
resource_url = 'https:%s' % resource_url
380+
381+
return resource_url
382+
383+
384+
class Bootstrap(object):
385+
def __init__(self, app=None):
386+
if app is not None:
387+
self.init_app(app)
388+
389+
def init_app(self, app):
390+
app.config.setdefault('BOOTSTRAP_USE_MINIFIED', True)
391+
app.config.setdefault('BOOTSTRAP_CDN_FORCE_SSL', False)
392+
393+
app.config.setdefault('BOOTSTRAP_QUERYSTRING_REVVING', True)
394+
app.config.setdefault('BOOTSTRAP_SERVE_LOCAL', False)
395+
396+
app.config.setdefault('BOOTSTRAP_LOCAL_SUBDOMAIN', None)
397+
398+
~~ blueprint = Blueprint(
399+
'bootstrap',
400+
__name__,
401+
template_folder='templates',
402+
static_folder='static',
403+
static_url_path=app.static_url_path + '/bootstrap',
404+
subdomain=app.config['BOOTSTRAP_LOCAL_SUBDOMAIN'])
405+
406+
blueprint.add_app_template_filter(render_form)
407+
408+
app.register_blueprint(blueprint)
409+
410+
app.jinja_env.globals['bootstrap_is_hidden_field'] =\
411+
is_hidden_field_filter
412+
app.jinja_env.globals['bootstrap_find_resource'] =\
413+
bootstrap_find_resource
414+
app.jinja_env.add_extension('jinja2.ext.do')
415+
416+
if not hasattr(app, 'extensions'):
417+
app.extensions = {}
418+
419+
local = StaticCDN('bootstrap.static', rev=True)
420+
static = StaticCDN()
421+
422+
def lwrap(cdn, primary=static):
423+
424+
425+
## ... source file continues with no further Blueprint examples...
426+
427+
```
428+
429+
430+
## Example 6 from flask-debugtoolbar
327431
[Flask Debug-toolbar](https://github.com/flask-debugtoolbar/flask-debugtoolbar)
328432
([documentation](https://flask-debugtoolbar.readthedocs.io/en/latest/)
329433
and
@@ -393,7 +497,7 @@ class DebugToolbarExtension(object):
393497
```
394498

395499

396-
## Example 6 from flask-restx
500+
## Example 7 from flask-restx
397501
[Flask RESTX](https://github.com/python-restx/flask-restx) is an
398502
extension that makes it easier to build
399503
[RESTful APIs](/application-programming-interfaces.html) into
@@ -445,7 +549,7 @@ def swagger_static(filename):
445549
```
446550

447551

448-
## Example 7 from Flask-WTF
552+
## Example 8 from Flask-WTF
449553
[Flask-WTF](https://github.com/lepture/flask-wtf)
450554
([project documentation](https://flask-wtf.readthedocs.io/en/stable/)
451555
and
@@ -553,7 +657,7 @@ def generate_csrf(secret_key=None, token_key=None):
553657
```
554658

555659

556-
## Example 8 from Flask-User
660+
## Example 9 from Flask-User
557661
[Flask-User](https://github.com/lingthio/Flask-User)
558662
([PyPI information](https://pypi.org/project/Flask-User/)
559663
and
@@ -660,7 +764,7 @@ class UserManager(UserManager__Settings, UserManager__Utils, UserManager__Views)
660764
```
661765

662766

663-
## Example 9 from Flask-VueJs-Template
767+
## Example 10 from Flask-VueJs-Template
664768
[Flask-VueJs-Template](https://github.com/gtalarico/flask-vuejs-template)
665769
([demo site](https://flask-vuejs-template.herokuapp.com/))
666770
is a minimal [Flask](/flask.html) boilerplate starter project that
@@ -692,7 +796,7 @@ import os
692796
```
693797

694798

695-
## Example 10 from Datadog Flask Example App
799+
## Example 11 from Datadog Flask Example App
696800
The [Datadog Flask example app](https://github.com/DataDog/trace-examples/tree/master/python/flask)
697801
contains many examples of the [Flask](/flask.html) core functions
698802
available to a developer using the [web framework](/web-frameworks.html).
@@ -739,7 +843,7 @@ def bp_after_request(response):
739843
```
740844

741845

742-
## Example 11 from tedivms-flask
846+
## Example 12 from tedivms-flask
743847
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
744848
base of [Flask](/flask.html) code and related projects such as
745849
[Celery](/celery.html) which provides a template to start your own

content/pages/examples/flask/flask-cli-with-appcontext.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ sidebartitle: flask.cli with_appcontext
77
meta: Python example code for the with_appcontext callable from the flask.cli module of the Flask project.
88

99

10-
with_appcontext is a callable within the flask.cli module of the Flask project.
10+
[with_appcontext](https://github.com/pallets/flask/blob/master/src/flask/cli.py)
11+
is a [Flask](/flask.html) decorator in the flask.cli module that
12+
wraps a callback to guarantee it will be called with a script's
13+
application context. Any callbacks registered with `app.cli` are
14+
wrapped with this function by default.
1115

1216

1317
## Example 1 from FlaskBB

content/pages/examples/flask/flask-extensions-plug-ins.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ flask-base's code is open sourced
6565
[under the MIT license](https://github.com/hack4impact/flask-base/blob/master/LICENSE.md).
6666

6767

68+
### flask-bootstrap
69+
[flask-bootstrap](https://github.com/mbr/flask-bootstrap)
70+
([PyPI package information](https://pypi.org/project/Flask-Bootstrap/))
71+
makes it easier to use the [Bootstrap CSS framework](/bootstrap-css.html)
72+
in your [Flask](/flask.html) applications with less boilerplate
73+
code. The project was primarily created by
74+
[Marc Brinkmann @mbr](https://github.com/mbr) and the source code is
75+
open sourced under the
76+
[Apache 2.0 license](https://github.com/mbr/flask-bootstrap/blob/master/LICENSE).
77+
78+
6879
### Flask Debug-toolbar
6980
[Flask Debug-toolbar](https://github.com/flask-debugtoolbar/flask-debugtoolbar)
7081
([documentation](https://flask-debugtoolbar.readthedocs.io/en/latest/)

0 commit comments

Comments
 (0)