Sharing js code between callbacks

Hi, i am writing a lot of JS code which is used via eg.

callback=CustomJS(...)
fxymap.x_range.js_on_change('start', callback)

first of all, its getting quite horrible to write javascript code inside ‘’’ jscode ‘’’ in python (so i made a manual solution where i am loading js code from a js file and injecting that) and second of all, what do i do if i want to share functions and data between different chunks of callbacks?

Can i somehow just write a reqular js module of sorts and then access that from bokeh - or something similar?

Sure, that’s easy enough. You can even make the JavaScript from your own module manipulate bokeh objects and callbacks that you listen to server-side will fire automatically.

Firstly, you should use a template, the how-to is described here: Running a Bokeh server — Bokeh 2.4.2 Documentation

Inside the template you can declare any (local) resources that you want to have loaded into your application. Now you can simply evoke a custom function using a JSCallback, e.g.:
CustomJS(code=“myfunc();”). If you pass a reference of the bokeh-object that you want to manipulate to your own code you can ofcoure manipulate it there, as well. (I did something similar recently)

If you want to do this from a Notebook, it’d be slightly harder to embed your custom code I believe.

Something handy I came across is:
You can execude abritrary JS from the bokeh server by creating a PreText-Widget. And abusing it a bit, like so: python 3.x - How to trigger execution of arbitrary JavaScript code in browser from Bokeh Server? - Stack Overflow .

That way you can access your custom modules a bit more easily and customizably.

If you want to just share data from non-connected callbacks you could temporarily save data in the browser by putting it in e.g., window.callback2_data and access it from the other callback

first of all, its getting quite horrible to write javascript code inside ‘’’ jscode ‘’’ in python (so i made a manual solution where i am loading js code from a js file and injecting that)

As far as I can see, the options are

code = """ manual code string """

and

code = open("some_jscode.js").read()

Are you proposing some new maintained API just to spell the second version some different way? I don’t think a dedicated PI is warranted for replacing what can already be done with built-in Python functions trivially.

If you are asking about being able to use require, import, etc. to load modules, then I’ll note that that is simply out of scope for CustomJS. The motivation behind CustomJS etc (and one of their requirements) is that they work without a compilation step, or needing to have node installed. That is to keep things as simple as possible for the majority of basic use-cases. Once compilation/packing is involved, things are in the realm of custom extensions.

It’s possible there is some way to allow compiled code to be used for CustomJS and friends but any solution would need to be explicitly enabled and off by default so that the common case still requires no heavy dependencies be installed. But that would be new developenmt. so aGitHub development discussion would be appropriate