Embed dash plot into web page

I’ve been asked if I can add Dash to Django-based website without iframe, so I did this:

  1. added dash-index view, which renders html page, copied from Dash main page (will be styled as website)
  2. added view for all requests, started with ‘_dash’, with code:
    return HttpResponse(dispatcher(request), content_type='application/json')
  1. where dispatcher looks like this:
app = _create_app() # create usual Dash app
# request is Django HttpRequest
params = {
    'data': request.body,
    'method': request.method,
    'content_type': request.content_type
}
with app.server.test_request_context(request.path, **params):
    app.server.preprocess_request()
    try:
        response = app.server.full_dispatch_request()
    except Exception as e:
        response = app.server.make_response(app.server.handle_exception(e))
    return response.get_data()

I know this is not an ideal solution, but it works and does the job for now.

12 Likes