Revealing the structure of a bokeh model

I’ve been trying to learn about how Bokeh is put together, and in the course of that wrote this (relatively) small piece of code that draws the structure of a bokeh model. For example, here
is a bokeh plot of a sin wave with some added noise:

and here is the structure of that model:

Although not visible in this static image, if you hover over the nodes you can see which attribute of the parent the submodel is attached to.

The trickiest part of this is that there isn’t a natural way to iterate over the submodel structure. I got around this by using model.references() to construct, for each submodel, the subset of all models that
are children of that submodel, constructing the graph of the partial order for the inclusion relation, and then finding the transitive reduction of that graph using a networkx algorithm.

The code for this is here.

9 Likes

This is really fantastic! Thinking out loud if it might make sense to add this as a utility function to Bokeh itself, so that anyone could interrogate their object graph. What do you think?

2 Likes

I’d be honored! What would you need for that?

You can either create a proper PR with a [FEATURE] issue (a preferable approach) or just post the code here, and the issue and PR will be created by someone else.

I created an issue and a related PR.

1 Like

In working with this further, I found that a basic assumption I had made – that the models form a directed acyclic graph - -doesn’t hold for complicated models involving selections and callbacks. So until I figure out a general implementation I’m going to take this code down.