So the question is: what if we want to run both apps in parallel?
Package werkzeug.wsgi provides DispatcherMiddleware class to let multiple apps running concurrently under a parent app. All things work at Flask's middleware level and it basically shares the same port to the children apps.
Here's the code snippet:
# Assuming Flask object "app" is created for the original app
#
import dash
from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = Flask(__name__)
# Assuming source code for original Flask "app" is already in place
# ...
dash_app = dash.Dash(__name__)
dash_app.config.supress_callback_exceptions = True
dash_app.css.config.serve_locally = True
dash_app.scripts.config.serve_locally = True
dash_app.config.update({
'routes_pathname_prefix':'/app2/',
'requests_pathname_prefix': '/app2/'
})
dash_app.layout = html.Div([html.H1('Hello world')])
# Wrapping original "app" with DispatcherMiddleware
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {'/app2': dash_app.server})
# Entry point
if __name__ == '__main__':
run_simple('0.0.0.0', 80, app, use_debugger=True, use_reloader=True, threaded=True)
To link up both apps, we need to make sure "app.wsgi_app" and "dash_app.server" are called correctly within DispatcherMiddleware.
Also, run_simple() method plays an important role here to ease the pain of complicated configurations and avoid unnecessary error messages. You might not want to use app.run() again after all.
The beauty is that the original Flask app remains the same while app2 or even app3 can load in for testing, of course, under the same hostname and port number.
Let's imagine the URLs can be accessed like these:
# Original Flask app http://localhost # app2 using Dash http://localhost/app2/ # app3 and so on... http://localhost/app3/
No comments:
Post a Comment