tam203
tam203

Reputation: 339

Define prefix for static resource paths in bokeh with autoload.js

I have a service that spins up Bokeh server instances like so:

server = Server({"/": app}, io_loop=loop, port=port, 
allow_websocket_origin=['localhost:%d'%port, origin, self.request.host])

This service runs behind a proxy (nbserverproxy) such that the client can only access localhost:<port>/<path> via example.com/proxy/<port>/<path>

I am trying to return a script tag that will load my bokeh app ensuring that resources are requested from example.com/proxy/<port>/<path>

Currently I generate this tag like so:

script = """<script
        src="{protocol}://{origin}{prefix}autoload.js?bokeh-autoload-element={ele_id}&bokeh-absolute-url={protocol}://{origin}&bokeh-app-path={prefix}"
        id="{ele_id}"
        data-bokeh-model-id=""
        data-bokeh-doc-id=""
    ></script>""".format(protocol=protocol, origin=origin, prefix=prefix, ele_id=ele_id) 

which (for bokeh running on port 45740 renders something like:

<script
        src="https://example.com/proxy/45740/autoload.js?bokeh-autoload-element=0535b6ca-6af2-4fcf-aa51-66d642fd3b08&bokeh-absolute-url=http://localhost:8888&bokeh-app-path=/proxy/45740/"
        id="0535b6ca-6af2-4fcf-aa51-66d642fd3b08"
        data-bokeh-model-id=""
        data-bokeh-doc-id=""
    ></script>

This tag will correctly return some Javascript unfortunately in this JS it will try to load the JS / CSS resources at a location like:

https://example.com/static/js/bokeh.min.js

rather than

https://example.com/proxy/45740/static/js/bokeh.min.js

I can't figure a way to tell bokeh that it's behind a proxy such that all paths start '/proxy/45740/' from the client's perspective.

I have tried starting the server with the prefix arg like:

Server({"/": app}, prefix="/proxy/%s"%port, io_loop=loop, port=port, allow_websocket_origin=['localhost:%d'%port, origin, self.request.host])

This doesn't work because the proxy strips the /proxy/<port> prefix before it reaches bokeh server so this isn't passed to bokeh server and bokeh server gives a 404 as it expects this prefix for all it's paths.

Any solutions?

Upvotes: 0

Views: 481

Answers (1)

bigreddot
bigreddot

Reputation: 34618

That is an intended purpose of prefix, the automatic behavior of nbserverproxy seems unfortunate.

One possible solution might be to let your proxy serve BokehJS resources directly, instead of having the Bokeh server do it. Many proxies are optimized for serving static resources, and this also reduces the load on the Bokeh server which may be relevant depending on your situation.

I don't know anything about nbserverproxy but you can see how the old version of https://demo.bokeh.org site did it with nginx:

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

The gist is this: copy the BokehJS files to some local directory, and configure your proxy to serve that directory as https://example.com/static/

Upvotes: 1

Related Questions