How can I add a standalone CustomJS callback without attaching it to any model?

Hello everyone,

I want to call a CustomJS callback from the html document, but when I initialize it in python, it doesn’t show up in the html file it’s creating.
For example, I initialize the callback in Python, it gives me the id in the command line, but does not include it in the html

CustomJS(args=dict(source1=source1, source2=source2), name="foo", code="""
    //do something
""")

CustomJS(id = ‘1344’, …)

Is there any solution or workaround to this?

Thank you
Sandra

Not really, what is it exactly that you are trying to accomplish, that makes you want to do this specifically?

I work with bokeh and flask and want to automatically load my csv data (ColumnDataSource) when the webpage is loaded

<script>
  window.onload = function(){
    window.Bokeh.documents[0].get_model_by_name(“foo”).execute();
  }
</script>

This CustomJS callback would call a python function via ajax to fetch the data and update the ColumnDataSource in the CustomJS callback.

If this isn’t possible I may have an idea for a workaround.
I thought it might be possible to assign the callback to a model and then make it invisible or something like that. But I dont know if there exists a model that can be set like this.

Althought it’s unusual, you might be able to add the CustomJS as a root itself with curdoc().add_root(...) (even in standalone content). But I can’t guarantee that will work, you will just have to try. Otherwise, you can add it to an some other model as a workaround, as you suggest. (A DataModel might be a good choice)

1 Like