Bokeh: update zoom plot (or axis rescaling) when hide series on legend

Since 0.12.13, all of BokehJS was ported to TypeScript and the entire layout subsystem was rewritten from scratch. Here is an updated version below. Note that it does not use CustomJS.from_py_func which has been deprecated for quite some time and will 100% be removed in the upcoming 2.0 release.

callback = """
    y_range = fig.y_range
    y_range.have_updated_interactively = false
    y_range.renderers = []
    for (let it of legend.items) {
        for (let r of it.renderers) {
            if (r.visible)
                y_range.renderers.push(r)
        }
    }
    Bokeh.index[fig.id].update_dataranges()
"""

for item in legend.items:
    item.renderers[0].js_on_change("visible",
         CustomJS(args=dict(fig=fig, legend=fig.legend[0]), code=callback))

FYI this is pretty esoteric usage, so I’m not implicitly making any guarantees about this long-term, either. We are not yet at a point where the BokehJS API can be considered completely stable. A better path is to make an issue for adding supported events around legend interactions that can be maintained under test.