Skip to content

Commit 51d10c3

Browse files
committed
add new flask example code
1 parent 301f48b commit 51d10c3

22 files changed

+963
-38
lines changed

content/pages/examples/flask/flask-app-flask.markdown

Lines changed: 157 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,118 @@ from app.models import User
457457
```
458458

459459

460-
## Example 8 from Flask-User
460+
## Example 8 from Flask-SocketIO
461+
[Flask-SocketIO](https://github.com/miguelgrinberg/Flask-SocketIO)
462+
([PyPI package information](https://pypi.org/project/Flask-SocketIO/),
463+
[official tutorial](https://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent)
464+
and
465+
[project documentation](https://flask-socketio.readthedocs.io/en/latest/))
466+
is a code library by [Miguel Grinberg](https://blog.miguelgrinberg.com/index)
467+
that provides Socket.IO integration for [Flask](/flask.html) applications.
468+
This extension makes it easier to add bi-directional communications on the
469+
web via the [WebSockets](/websockets.html) protocol.
470+
471+
The Flask-SocketIO project is open source under the
472+
[MIT license](https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/LICENSE).
473+
474+
[**Flask-SocketIO / test_socketio.py**](https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/././test_socketio.py)
475+
476+
```python
477+
# test_socketio.py
478+
import json
479+
import unittest
480+
import coverage
481+
482+
cov = coverage.coverage(branch=True)
483+
cov.start()
484+
485+
~~from flask import Flask, session, request, json as flask_json
486+
from flask_socketio import SocketIO, send, emit, join_room, leave_room, \
487+
Namespace, disconnect
488+
489+
~~app = Flask(__name__)
490+
app.config['SECRET_KEY'] = 'secret'
491+
socketio = SocketIO(app)
492+
disconnected = None
493+
494+
495+
@socketio.on('connect')
496+
def on_connect():
497+
if request.args.get('fail'):
498+
return False
499+
send('connected')
500+
send(json.dumps(request.args.to_dict(flat=False)))
501+
send(json.dumps({h: request.headers[h] for h in request.headers.keys()
502+
if h not in ['Host', 'Content-Type', 'Content-Length']}))
503+
504+
505+
@socketio.on('disconnect')
506+
def on_disconnect():
507+
global disconnected
508+
disconnected = '/'
509+
510+
511+
@socketio.on('connect', namespace='/test')
512+
def on_connect_test():
513+
send('connected-test')
514+
515+
516+
## ... source file abbreviated to get to Flask examples ...
517+
518+
519+
client.emit('exit', {}, namespace='/ns')
520+
self.assertFalse(client.is_connected('/ns'))
521+
with self.assertRaises(RuntimeError):
522+
client.emit('hello', {}, namespace='/ns')
523+
524+
def test_emit_class_based(self):
525+
client = socketio.test_client(app, namespace='/ns')
526+
client.get_received('/ns')
527+
client.emit('my_custom_event', {'a': 'b'}, namespace='/ns')
528+
received = client.get_received('/ns')
529+
self.assertEqual(len(received), 1)
530+
self.assertEqual(len(received[0]['args']), 1)
531+
self.assertEqual(received[0]['name'], 'my custom response')
532+
self.assertEqual(received[0]['args'][0]['a'], 'b')
533+
534+
def test_request_event_data_class_based(self):
535+
client = socketio.test_client(app, namespace='/ns')
536+
client.get_received('/ns')
537+
global request_event_data
538+
request_event_data = None
539+
client.emit('other_custom_event', 'foo', namespace='/ns')
540+
expected_data = {'message': 'other_custom_event', 'args': ('foo',)}
541+
self.assertEqual(request_event_data, expected_data)
542+
543+
def test_delayed_init(self):
544+
~~ app = Flask(__name__)
545+
socketio = SocketIO(allow_upgrades=False, json=flask_json)
546+
547+
@socketio.on('connect')
548+
def on_connect():
549+
send({'connected': 'foo'}, json=True)
550+
551+
socketio.init_app(app, cookie='foo')
552+
self.assertFalse(socketio.server.eio.allow_upgrades)
553+
self.assertEqual(socketio.server.eio.cookie, 'foo')
554+
555+
client = socketio.test_client(app)
556+
received = client.get_received()
557+
self.assertEqual(len(received), 1)
558+
self.assertEqual(received[0]['args'], {'connected': 'foo'})
559+
560+
561+
if __name__ == '__main__':
562+
unittest.main()
563+
564+
565+
566+
## ... source file continues with no further Flask examples...
567+
568+
```
569+
570+
571+
## Example 9 from Flask-User
461572
[Flask-User](https://github.com/lingthio/Flask-User)
462573
([PyPI information](https://pypi.org/project/Flask-User/)
463574
and
@@ -540,7 +651,48 @@ class UserManager(UserManager__Settings, UserManager__Utils, UserManager__Views)
540651
```
541652

542653

543-
## Example 9 from Flasky
654+
## Example 10 from Flask-VueJs-Template
655+
[Flask-VueJs-Template](https://github.com/gtalarico/flask-vuejs-template)
656+
([demo site](https://flask-vuejs-template.herokuapp.com/))
657+
is a minimal [Flask](/flask.html) boilerplate starter project that
658+
combines Flask, [Vue.js](https://www.fullstackpython.com/vuejs.html),
659+
and [Flask-RESTPlus](https://flask-restplus.readthedocs.io/en/stable/).
660+
The project provides some sensible defaults that are easy to continue
661+
building on, and the source code is open source under the
662+
[MIT license](https://github.com/gtalarico/flask-vuejs-template/blob/master/LICENSE.md).
663+
664+
[**Flask-VueJs-Template / app / __init__.py**](https://github.com/gtalarico/flask-vuejs-template/blob/master/app/./__init__.py)
665+
666+
```python
667+
# __init__.py
668+
import os
669+
~~from flask import Flask, current_app, send_file
670+
671+
from .api import api_bp
672+
from .client import client_bp
673+
674+
~~app = Flask(__name__, static_folder='../dist/static')
675+
app.register_blueprint(api_bp)
676+
677+
from .config import Config
678+
app.logger.info('>>> {}'.format(Config.FLASK_ENV))
679+
680+
@app.route('/')
681+
def index_client():
682+
dist_dir = current_app.config['DIST_DIR']
683+
entry = os.path.join(dist_dir, 'index.html')
684+
return send_file(entry)
685+
686+
687+
688+
689+
690+
## ... source file continues with no further Flask examples...
691+
692+
```
693+
694+
695+
## Example 11 from Flasky
544696
[Flasky](https://github.com/miguelgrinberg/flasky) is a wonderful
545697
example application by
546698
[Miguel Grinberg](https://github.com/miguelgrinberg) that he builds
@@ -604,7 +756,7 @@ def create_app(config_name):
604756
```
605757

606758

607-
## Example 10 from Datadog Flask Example App
759+
## Example 12 from Datadog Flask Example App
608760
The [Datadog Flask example app](https://github.com/DataDog/trace-examples/tree/master/python/flask)
609761
contains many examples of the [Flask](/flask.html) core functions
610762
available to a developer using the [web framework](/web-frameworks.html).
@@ -665,7 +817,7 @@ def before_request():
665817
```
666818

667819

668-
## Example 11 from sandman2
820+
## Example 13 from sandman2
669821
[sandman2](https://github.com/jeffknupp/sandman2)
670822
([project documentation](https://sandman2.readthedocs.io/en/latest/)
671823
and
@@ -746,7 +898,7 @@ def get_app(
746898
```
747899

748900

749-
## Example 12 from tedivms-flask
901+
## Example 14 from tedivms-flask
750902
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
751903
base of [Flask](/flask.html) code and related projects such as
752904
[Celery](/celery.html) which provides a template to start your own

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,39 @@ class UserManager(UserManager__Settings, UserManager__Utils, UserManager__Views)
656656
```
657657

658658

659-
## Example 9 from Datadog Flask Example App
659+
## Example 9 from Flask-VueJs-Template
660+
[Flask-VueJs-Template](https://github.com/gtalarico/flask-vuejs-template)
661+
([demo site](https://flask-vuejs-template.herokuapp.com/))
662+
is a minimal [Flask](/flask.html) boilerplate starter project that
663+
combines Flask, [Vue.js](https://www.fullstackpython.com/vuejs.html),
664+
and [Flask-RESTPlus](https://flask-restplus.readthedocs.io/en/stable/).
665+
The project provides some sensible defaults that are easy to continue
666+
building on, and the source code is open source under the
667+
[MIT license](https://github.com/gtalarico/flask-vuejs-template/blob/master/LICENSE.md).
668+
669+
[**Flask-VueJs-Template / app / client.py**](https://github.com/gtalarico/flask-vuejs-template/blob/master/app/./client.py)
670+
671+
```python
672+
# client.py
673+
674+
import os
675+
~~from flask import Blueprint, render_template
676+
677+
~~client_bp = Blueprint('client_app', __name__,
678+
url_prefix='',
679+
static_url_path='',
680+
static_folder='./dist/static/',
681+
template_folder='./dist/',
682+
)
683+
684+
685+
686+
## ... source file continues with no further Blueprint examples...
687+
688+
```
689+
690+
691+
## Example 10 from Datadog Flask Example App
660692
The [Datadog Flask example app](https://github.com/DataDog/trace-examples/tree/master/python/flask)
661693
contains many examples of the [Flask](/flask.html) core functions
662694
available to a developer using the [web framework](/web-frameworks.html).
@@ -703,7 +735,7 @@ def bp_after_request(response):
703735
```
704736

705737

706-
## Example 10 from tedivms-flask
738+
## Example 11 from tedivms-flask
707739
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
708740
base of [Flask](/flask.html) code and related projects such as
709741
[Celery](/celery.html) which provides a template to start your own

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ get basic APIs running for existing applications and it exposes
9999
endpoint documentation using [Swagger](https://swagger.io/).
100100

101101
Flask RESTX is provided as open source under the
102-
[BSD 3-Clause license](https://github.com/python-restx/flask-restx/blob/master/LICENSE).
102+
[BSD 3-Clause license](https://github.com/python-restx/flask-restx/blob/master/LICENSE).
103103

104104

105105
### Flask-Security-Too
@@ -118,6 +118,21 @@ The Flask-Security-Too project is provided as open source under the
118118
[MIT license](https://github.com/Flask-Middleware/flask-security/blob/master/LICENSE).
119119

120120

121+
### Flask-SocketIO
122+
[Flask-SocketIO](https://github.com/miguelgrinberg/Flask-SocketIO)
123+
([PyPI package information](https://pypi.org/project/Flask-SocketIO/),
124+
[official tutorial](https://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent)
125+
and
126+
[project documentation](https://flask-socketio.readthedocs.io/en/latest/))
127+
is a code library by [Miguel Grinberg](https://blog.miguelgrinberg.com/index)
128+
that provides Socket.IO integration for [Flask](/flask.html) applications.
129+
This extension makes it easier to add bi-directional communications on the
130+
web via the [WebSockets](/websockets.html) protocol.
131+
132+
The Flask-SocketIO project is open source under the
133+
[MIT license](https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/LICENSE).
134+
135+
121136
### Flask-User
122137
[Flask-User](https://github.com/lingthio/Flask-User)
123138
([PyPI information](https://pypi.org/project/Flask-User/)
@@ -131,6 +146,17 @@ through both [relational databases](/databases.html) and
131146
the [MIT license](https://github.com/lingthio/Flask-User/blob/master/LICENSE.txt).
132147

133148

149+
### Flask-VueJs-Template
150+
[Flask-VueJs-Template](https://github.com/gtalarico/flask-vuejs-template)
151+
([demo site](https://flask-vuejs-template.herokuapp.com/))
152+
is a minimal [Flask](/flask.html) boilerplate starter project that
153+
combines Flask, [Vue.js](https://www.fullstackpython.com/vuejs.html),
154+
and [Flask-RESTPlus](https://flask-restplus.readthedocs.io/en/stable/).
155+
The project provides some sensible defaults that are easy to continue
156+
building on, and the source code is open source under the
157+
[MIT license](https://github.com/gtalarico/flask-vuejs-template/blob/master/LICENSE.md).
158+
159+
134160
### Flask-WTF
135161
[Flask-WTF](https://github.com/lepture/flask-wtf)
136162
([project documentation](https://flask-wtf.readthedocs.io/en/stable/)

content/pages/examples/flask/flask-globals-current-app.markdown

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,48 @@ class UserManager(UserManager__Settings, UserManager__Utils, UserManager__Views)
19961996
```
19971997

19981998

1999-
## Example 13 from Flasky
1999+
## Example 13 from Flask-VueJs-Template
2000+
[Flask-VueJs-Template](https://github.com/gtalarico/flask-vuejs-template)
2001+
([demo site](https://flask-vuejs-template.herokuapp.com/))
2002+
is a minimal [Flask](/flask.html) boilerplate starter project that
2003+
combines Flask, [Vue.js](https://www.fullstackpython.com/vuejs.html),
2004+
and [Flask-RESTPlus](https://flask-restplus.readthedocs.io/en/stable/).
2005+
The project provides some sensible defaults that are easy to continue
2006+
building on, and the source code is open source under the
2007+
[MIT license](https://github.com/gtalarico/flask-vuejs-template/blob/master/LICENSE.md).
2008+
2009+
[**Flask-VueJs-Template / app / __init__.py**](https://github.com/gtalarico/flask-vuejs-template/blob/master/app/./__init__.py)
2010+
2011+
```python
2012+
# __init__.py
2013+
import os
2014+
~~from flask import Flask, current_app, send_file
2015+
2016+
from .api import api_bp
2017+
from .client import client_bp
2018+
2019+
app = Flask(__name__, static_folder='../dist/static')
2020+
app.register_blueprint(api_bp)
2021+
2022+
from .config import Config
2023+
app.logger.info('>>> {}'.format(Config.FLASK_ENV))
2024+
2025+
@app.route('/')
2026+
def index_client():
2027+
~~ dist_dir = current_app.config['DIST_DIR']
2028+
entry = os.path.join(dist_dir, 'index.html')
2029+
return send_file(entry)
2030+
2031+
2032+
2033+
2034+
2035+
## ... source file continues with no further current_app examples...
2036+
2037+
```
2038+
2039+
2040+
## Example 14 from Flasky
20002041
[Flasky](https://github.com/miguelgrinberg/flasky) is a wonderful
20012042
example application by
20022043
[Miguel Grinberg](https://github.com/miguelgrinberg) that he builds
@@ -2051,7 +2092,7 @@ def run_migrations_online():
20512092
```
20522093

20532094

2054-
## Example 14 from sandman2
2095+
## Example 15 from sandman2
20552096
[sandman2](https://github.com/jeffknupp/sandman2)
20562097
([project documentation](https://sandman2.readthedocs.io/en/latest/)
20572098
and
@@ -2176,7 +2217,7 @@ def register_model(cls, admin=None):
21762217
```
21772218

21782219

2179-
## Example 15 from tedivms-flask
2220+
## Example 16 from tedivms-flask
21802221
[tedivm's flask starter app](https://github.com/tedivm/tedivms-flask) is a
21812222
base of [Flask](/flask.html) code and related projects such as
21822223
[Celery](/celery.html) which provides a template to start your own

0 commit comments

Comments
 (0)