@@ -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/ )
463574and
@@ -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
545697example 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
608760The [ Datadog Flask example app] ( https://github.com/DataDog/trace-examples/tree/master/python/flask )
609761contains many examples of the [ Flask] ( /flask.html ) core functions
610762available 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/ )
671823and
@@ -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
751903base of [ Flask] ( /flask.html ) code and related projects such as
752904[ Celery] ( /celery.html ) which provides a template to start your own
0 commit comments