Skip to content

Commit

Permalink
flask 0.11 cli support
Browse files Browse the repository at this point in the history
fixes #289
  • Loading branch information
miguelgrinberg committed Jul 24, 2016
1 parent b5cf4df commit 91bc084
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,19 @@ application::

The ``init_app()`` style of initialization is also supported. Note the way the
web server is started. The ``socketio.run()`` function encapsulates the start
up of the web server and replaces the standard ``app.run()`` standard Flask
up of the web server and replaces the ``app.run()`` standard Flask
development server start up. When the application is in debug mode the
Werkzeug development server is still used and configured properly inside
``socketio.run()``. In production mode the eventlet web server is used if
available, else the gevent web server is used. If eventlet and gevent are not
installed, the Werkzeug development web server is used.

The command line interface based on click introduced in Flask 0.11 is also
supported. This extension provides a new version of the ``flask run`` command
that is appropriate for starting the Socket.IO server. Example usage::

$ FLASK_APP=my_app.py flask run

The application must serve a page to the client that loads the Socket.IO
library and establishes a connection::

Expand Down
6 changes: 3 additions & 3 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def background_thread():

@app.route('/')
def index():
global thread
if thread is None:
thread = socketio.start_background_task(target=background_thread)
return render_template('index.html', async_mode=socketio.async_mode)


Expand Down Expand Up @@ -98,6 +95,9 @@ def ping_pong():

@socketio.on('connect', namespace='/test')
def test_connect():
global thread
if thread is None:
thread = socketio.start_background_task(target=background_thread)
emit('my response', {'data': 'Connected', 'count': 0})


Expand Down
58 changes: 58 additions & 0 deletions flask_socketio/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os

from flask.cli import pass_script_info, get_debug_flag
import click


@click.command()
@click.option('--host', '-h', default='127.0.0.1',
help='The interface to bind to.')
@click.option('--port', '-p', default=5000,
help='The port to bind to.')
@click.option('--reload/--no-reload', default=None,
help='Enable or disable the reloader. By default the reloader '
'is active if debug is enabled.')
@click.option('--debugger/--no-debugger', default=None,
help='Enable or disable the debugger. By default the debugger '
'is active if debug is enabled.')
@click.option('--eager-loading/--lazy-loader', default=None,
help='Enable or disable eager loading. By default eager '
'loading is enabled if the reloader is disabled.')
@pass_script_info
def run(info, host, port, reload, debugger, eager_loading):
"""Runs a local development server for the Flask-SocketIO application.
The reloader and debugger are by default enabled if the debug flag of
Flask is enabled and disabled otherwise.
"""
debug = get_debug_flag()
if reload is None:
reload = bool(debug)
if debugger is None:
debugger = bool(debug)
if eager_loading is None:
eager_loading = not reload

# Extra startup messages. This depends a bit on Werkzeug internals to
# not double execute when the reloader kicks in.
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
# If we have an import path we can print it out now which can help
# people understand what's being served. If we do not have an
# import path because the app was loaded through a callback then
# we won't print anything.
if info.app_import_path is not None:
print(' * Serving Flask-SocketIO app "%s"' % info.app_import_path)
if debug is not None:
print(' * Forcing debug mode %s' % (debug and 'on' or 'off'))

def run_server():
app = info.load_app()
socketio = app.extensions['socketio']
socketio.run(app, host=host, port=port, debug=debugger,
use_reloader=False, log_output=debugger)

if reload:
from werkzeug.serving import run_with_reloader
run_with_reloader(run_server)
else:
run_server()
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
zip_safe=False,
include_package_data=True,
platforms='any',
entry_points={
'flask.commands': [
'run=flask_socketio.cli:run'
],
},
install_requires=[
'Flask>=0.9',
'python-socketio>=1.4',
Expand Down

0 comments on commit 91bc084

Please sign in to comment.