3

I cannot get a bokeh plot to work on a deployed server because of cross-domain issues. I have asked this question in a few forms and am not really getting anywhere.

I always get the error

XMLHttpRequest cannot load http://127.0.0.1:5006/bokeh/objinfo/0257493b-cce5-450d-8036-2bc57233b1dc/bd1791f4-4d28-4faa-8c9d-a6fe5a1721c1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my_ip_address' is therefore not allowed access. The response had HTTP status code 500.

Whether I am running an applet or trying to embed a single plot.

Here I am trying to fetch a plot script from within a Flask view

@perf.route('/_fetch_heatmap', methods=['POST'])
@login_required
def fetch_sd_heatmap():

    document = Document()
    session = Session(root_url='http://127.0.0.1:5006', configdir=current_app.config['BASE_DIRECTORY'])
    session.use_doc('sd_viz')
    session.load_document(document)
    ...
    plots = VBox(hm_duration, hm_frequency)

    document.add(plots)
    push(session, document)

    script = autoload_server(plots, session)

return jsonify({'script': script})

This script is returned to an ajax call within my javascript. This script is then appended to the corresponding <div>

This runs fine on my development machine.

Below is my nginx configuration for production

server {


    listen my_ip default_server;
    charset     utf-8;
    client_max_body_size 30M;

    location ~ ^/(app_config.py|.git) {
        deny all;
        return 404;
    }

    location / {
        index index.html index.htm;
        root /home/myuser/app_directory;
        try_files $uri @app;
    }

    location /static {
    alias /home/myuser/app_directory/webapp/static;
    }


    location @app {
        include uwsgi_params;
        uwsgi_pass unix:/home/myuser/app_directory/uwsgi.sock;
        uwsgi_connect_timeout 18000;
...
}

Has anyone successfully made a flask application with embedded bokeh plots from the bokeh server that runs in a production environment?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Brian Leach
  • 3,974
  • 8
  • 36
  • 75
  • "The response had HTTP status code 500" suggests you haven't got your server set up correctly – Benjamin Hodgson Oct 26 '15 at 22:48
  • @BenjaminHodgson can you expand on that please? – Brian Leach Oct 26 '15 at 22:53
  • 1
    Well, it's returning a 500 Internal Server Error, which usually signals an uncaught exception in the server processing the request. It _looks_ like a CORS problem because the 500 has resulted in the Access-Control-Allow-Origin header going missing. – Benjamin Hodgson Oct 26 '15 at 22:56
  • @BenjaminHodgson I do not see any errors coming from my server. The only clues I get to this error are coming from Chrome's devtools (as identified above) – Brian Leach Oct 26 '15 at 23:41
  • Actually, that may be a bug in the Bokeh server (notice that it's the one returning the 500, not his Flask application). – Aron Ahmadia Oct 27 '15 at 03:21
  • @BrianLeach - The Bokeh developers run the [crossfilter demonstration application](https://github.com/bokeh/bokeh/blob/master/examples/app/crossfilter/crossfilter_app.py) here: http://bokeh.pydata.org/en/latest/docs/server_gallery/crossfilter_server.html. If you inspect the source, you'll see that the application is embedded using an iframe, which should get around your CORS issue. There are a couple of different tricks for communicating between iframes using Javascript. – Aron Ahmadia Oct 27 '15 at 03:22
  • Another option would be to set up a reverse proxy in nginx, but I think you would also need to do a string substitution on the generated JavaScript from the `autoload_server` function to make it properly use the proxy. – Aron Ahmadia Oct 27 '15 at 03:25

1 Answers1

1

Hi just to update this discussion, as of the new Bokeh server in 0.11 there is much more extensive documentation about deployments:

http://docs.bokeh.org/en/0.11.1/docs/user_guide/server.html

Including information about running behind reverse proxies, using load-balancers and process manager and automating with tools like Salt. The never server is much more robust, scalable, and easy to use. You can see a gallery of live Bokeh server examples that have been "production" deployed continuously since January 2016 here:

http://demo.bokeh.org

As a reference, the full automated deployment is available for study on GitHub:

https://github.com/bokeh/demo.bokeh.org

Additionally, a fairly sophisticated example of embedding a session-specific Bokeh server app is demonstrated in the "Happiness" example here:

https://github.com/bokeh/bokeh/tree/master/examples/embed/server_session

But lastly I should say that the upcoiming 0.12 release will have the ability to set a custom Jinja template for Bokeh apps, meaning that things like Single Page Apps that build heavily around Bokeh documents can be served directly from the Bokeh server, without needing to embed in another web-server (if that is desired).

bigreddot
  • 33,642
  • 5
  • 69
  • 122