ChesuCR
ChesuCR

Reputation: 9670

How to << require("core/util/projections") >> with Bokeh Server?

If I run this example on a jupyter lab works fine

from bokeh.io import output_notebook, show, push_notebook
from bokeh.models import HoverTool, CustomJSHover
from bokeh.tile_providers import CARTODBPOSITRON
from bokeh.plotting import figure
output_notebook()

# range bounds supplied in web mercator coordinates
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
        x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(CARTODBPOSITRON)

p.circle(x=[0, 2000000, 4000000], y=[4000000, 2000000, 0], size=30)

code = """
    var projections = require("core/util/projections");
    var x = special_vars.x
    var y = special_vars.y
    var coords = projections.wgs84_mercator.inverse([x, y])
    return coords[%d].toFixed(2)
"""

p.add_tools(HoverTool(
    tooltips=[
        ( 'lon',   '@x{custom}' ),
        ( 'lat',   '@y{custom}' ),
    ],

    formatters={
        'x' : CustomJSHover(code=code % 0),
        'y' : CustomJSHover(code=code % 1),
    }
))

show(p)

enter image description here

But if I run the example with the bokeh serve I get the error Uncaught Error: Cannot find module 'core/util/projections'

from bokeh.io import curdoc
from bokeh.models import HoverTool, CustomJSHover
from bokeh.plotting import figure
from bokeh.tile_providers import CARTODBPOSITRON

# range bounds supplied in web mercator coordinates
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
        x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(CARTODBPOSITRON)

p.circle(x=[0, 2000000, 4000000], y=[4000000, 2000000, 0], size=30)

code = """
    var projections = require("core/util/projections");
    var x = special_vars.x
    var y = special_vars.y
    var coords = projections.wgs84_mercator.inverse([x, y])
    return coords[%d].toFixed(2)
"""

p.add_tools(HoverTool(
    tooltips=[
        ( 'lon',   '@x{custom}' ),
        ( 'lat',   '@y{custom}' ),
    ],

    formatters={
        'x' : CustomJSHover(code=code % 0),
        'y' : CustomJSHover(code=code % 1),
    }
))

curdoc().add_root(p)

Am I missing anythin? Do I need to adapt the require call when I run the script with bokeh serve? I do not see any example with bokeh serve on the examples folder

My versions

Python version      :  3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
IPython version     :  6.5.0
Tornado version     :  5.1
Bokeh version       :  1.0.0dev8
node.js version     :  v8.11.3

Update October, 9th

Actually I have compiled and installed Bokeh from the zip release. I am getting a 404 error because the url to the js file is a concatenation of the url with the local path

http://localhost:5006/static/js/C:/path_to_bokeh_folder/bokeh/bokehjs/node_modules/tslib/tslib.js

I am afraid I did something wrong during the bokeh installation. I have followed this guide

I am using Windows 10

Update October 10th

Thanks @Torus, I have set BOKEH_RESOURCES=server-dev environment variable, but the module cannot be found neither.

For aditional information: I have installed bokeh with:

python setup.py install --build-js

And I have set up this environment variables:

BOKEH_BROWSER=none
BOKEH_LOG_LEVEL=debug
BOKEH_MINIFIED=false
BOKEH_PRETTY=true
BOKEH_PY_LOG_LEVEL=debug
BOKEH_RESOURCES=server-dev              # this is the most important as you pointed, to get the right paths
BOKEH_SIMPLE_IDS=true

But I am still getting the same error, the module cannot be found:

Cannot find module 'core/util/projections'

Conclusion

I think there is something wrong when I build bokeh manually, because if I install it in the usual way: pip install bokeh, when I do require("core/util/projections") works fine.

Is there another way to convert the coordinates to the "mercator projection"?. Should I create another CDS column and make the conversion within python?

Upvotes: 0

Views: 162

Answers (1)

Torus
Torus

Reputation: 188

You are using BOKEH_RESOURCES=absolute-dev or BOKEH_DEV=true. It doesn't work with bokeh server.

Run BOKEH_RESOURCES=server-dev bokeh serve your_script.py to get Bokeh to fetch resources in the correct way.

Upvotes: 1

Related Questions