Created
December 12, 2025 14:51
-
-
Save adivekar-utexas/374c3a37c81aecc8202e3e5d54d87326 to your computer and use it in GitHub Desktop.
Make bokeh work on older Jupyter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import * | |
| import hvplot | |
| def plotsum( | |
| plots_list: Union[List[Tuple[str, Any]], List[Any]], | |
| *, | |
| order: Optional[List[str]] = None, | |
| how: Literal["overlay", "grid"] = "grid", | |
| legend: Literal["first", "last", "none"] = "none", | |
| update_layout: Optional[Dict] = None, | |
| ): | |
| if order is not None: | |
| assert len(plots_list) > 0 | |
| assert len(order) == len(plots_list) | |
| assert len(set(p[0] for p in plots_list)) == len(order) | |
| ordered_plots_list: List[Any] = [] | |
| for order_item in order: | |
| plot_str: Optional = None | |
| for plot_str, plot in plots_list: | |
| if plot_str == order_item: | |
| break | |
| plot_str = None | |
| if plot_str is None: | |
| raise ValueError(f'No plot found with name: "{order_item}"') | |
| ordered_plots_list.append(plot) | |
| plots_list = ordered_plots_list | |
| plots = None | |
| for plot in plots_list: | |
| if isinstance(plot, tuple): | |
| assert len(plot) == 2 | |
| plot = plot[1] | |
| if plots is None: | |
| plots = plot | |
| else: | |
| if how == "grid": | |
| plots += plot | |
| elif how == "overlay": | |
| plots *= plot | |
| else: | |
| raise not_impl("how", how) | |
| return plots | |
| from bokeh.embed import components | |
| from bokeh.resources import INLINE | |
| from IPython.display import HTML, display | |
| import numpy as np | |
| import hvplot.pandas | |
| import holoviews as hv | |
| hv.extension('bokeh') | |
| def show_plt(plot): | |
| """ | |
| Display an hvplot/HoloViews plot using inline HTML embedding. | |
| Works around JupyterLab Bokeh extension issues. | |
| Args: | |
| plot: An hvplot or HoloViews object | |
| """ | |
| # Convert HoloViews object to Bokeh figure | |
| renderer = hv.renderer('bokeh') | |
| bokeh_plot = renderer.get_plot(plot).state | |
| # Get script and div components | |
| script, div = components(bokeh_plot) | |
| # Get inline resources | |
| js_resources = INLINE.render_js() | |
| css_resources = INLINE.render_css() | |
| # Combine into HTML | |
| html = f""" | |
| {css_resources} | |
| {js_resources} | |
| {div} | |
| {script} | |
| """ | |
| display(HTML(html)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment