On_init in js callback

Hi - I have been trying to find out if can inject some JS code that can run on initialization of a specific component or on initialization of the bokeh app or something…basically something that triggers when a component or all components have loaded?

Not yet, it seems: https://github.com/bokeh/bokeh/issues/4272#issuecomment-679487409
But maybe my example from that issue will work for you.

1 Like

Is your work around for bokeh serve or can it also be used for customJS?

I am using customJS

The answer is right there at the end of my post at https://github.com/bokeh/bokeh/issues/4272#issuecomment-615592757

aha ok nice…will it also work when using json_item instead of save/show ?

I don’t know.

I get this error when i try to run your full example with the only change that i use json_item instead of save:

ValueError                                Traceback (most recent call last)
<ipython-input-107-4f70c56c7c2f> in <module>
     83 layout = column(slider, plot)
---> 84 json_item([idle_doc_observer, layout])

~/anaconda3/lib/python3.7/site-packages/bokeh/embed/standalone.py in json_item(model, target, theme)
    367     '''
--> 368     with OutputDocumentFor([model], apply_theme=theme) as doc:
    369         doc.title = ""
    370         docs_json = standalone_docs_json([model])

~/anaconda3/lib/python3.7/contextlib.py in __enter__(self)
    110         del self.args, self.kwds, self.func
    111         try:
--> 112             return next(self.gen)
    113         except StopIteration:
    114             raise RuntimeError("generator didn't yield") from None

~/anaconda3/lib/python3.7/site-packages/bokeh/embed/util.py in OutputDocumentFor(objs, apply_theme, always_new)
    117     if not isinstance(objs, Sequence) or len(objs) == 0 or not all(isinstance(x, Model) for x in objs):
--> 118         raise ValueError("OutputDocumentFor expects a sequence of Models")
    120     def finish(): pass

ValueError: OutputDocumentFor expects a sequence of Models

Do you know how this could be fixed?

According to the docstring of json_item, it expects a single model and not a list of models.

Given that the implementation of json_item is just 6 lines, I would just copy it and modify it to my needs in my own code so it can receive a list of models.

ok yes…that makes sense…i did that, and I am getting this javascript error now:

Error: Model 'IdleDocObserver' does not exist. This could be due to a widget or a custom model not being registered before first usage.
    at Object.s.Models (bokeh-2.0.0.min.js:149)
    at Function._instantiate_object (bokeh-2.0.0.min.js:148)
    at Function._instantiate_references_json (bokeh-2.0.0.min.js:148)
    at Function.from_json (bokeh-2.0.0.min.js:148)
    at g (bokeh-2.0.0.min.js:146)
    at bokeh-2.0.0.min.js:146
    at bokeh-2.0.0.min.js:171

Any idea why i am getting that?

My code looks like this:

        all_plots = column(row(...)) //truncated
        models = [idle_doc_observer, all_plots]
        with OutputDocumentFor(models) as doc:
            doc.title = ""
            docs_json = standalone_docs_json(models)

        doc = list(docs_json.values())[0]
        root_id = doc['roots']['root_ids'][0]

        return {
            'target_id' : 'my_plot',
            'root_id'   : root_id,
            'doc'       : doc,

is that not correct?

Here my experience ends. You may want to look at how file_html works, especially the call to bundle_for_objs_and_resources since that’s the function that compiles all custom models.
Feel free to also open an feature request on GitHub for this.

Custom models are not currently supported with json_item