A new custom tool that doesn't require compilation

Hi,
I created a custom tool (following the example here).
It works just fine and does whatever I want it to. The problem I have is this - it is recompiled on every computer that runs that code, and that requires everyone to install Node.js just to be able to run this code.

My question divides into two:

  1. Is there a way to generate the custom tool’s code using JavaScript instead of TypeScript, and whether that would obviate the need for compilation?
  2. How do I pre-build the tool such that it could be loaded without the need for compilation on the remote copmuter?

Thanks

Is there a way to generate the custom tool’s code using JavaScript instead of TypeScript

Just use bokeh.util.compiler.JavaScript instead of bokeh.util.compiler.TypeScript.

whether that would obviate the need for compilation?

Alas, the compilation is still required in that case.

How do I pre-build the tool such that it could be loaded without the need for compilation on the remote copmuter?

It’s not a very straightforward process, but it’s possible:

  1. Run the example as is
  2. Open the saved file and find the <script> tag in <head> that contains all the compiled code. In the case of the custom tool it will contain "custom/draw_tool": function ...
  3. In the original example code, remove TS_CODE and __implementation__ completely
  4. Create a custom template:
from bokeh.core.templates import get_env

template = get_env().from_string("""
{% extends "file.html" %}

{% block postamble %}
[...]
{% endblock %}
  1. Replace the [...] line with the <script> tag you found earlier, including the tag itself
  2. Now, we need to use the custom template but show doesn’t support it - we will have to use save from bokeh.io:
save(plot, template=template)

save works just like show but it doesn’t open the browser window - you and the users of the resulting Python script will have to open the generated HTML file by hand or by writing some code that just opens the HTML file in a browser.

Support for pre-compilation of extensions via bokeh build was added late last year for Holoviews but AFAIK is it not well documented yet. cc @Philipp_Rudiger @mateusz

See the pre-built extensions section towards the bottom of this page in the Bokeh docs for info on pre-built extensions.

https://docs.bokeh.org/en/latest/docs/user_guide/extensions.html

And here for an illustrative example …

https://awesome-panel.readthedocs.io/en/latest/guides/awesome-panel-extensions-guide/bokeh-extensions.html#prebuilt-bokeh-extensions

2 Likes