Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: basic support for bokeh.plotting.show (#3796)

Akshay Agrawalc1e41195e57f
diff --git a/marimo/_output/formatters/bokeh_formatters.py b/marimo/_output/formatters/bokeh_formatters.py
index 2054fbb752..fc19e05047 100644
--- a/marimo/_output/formatters/bokeh_formatters.py
+++ b/marimo/_output/formatters/bokeh_formatters.py
@@ -1,7 +1,8 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import Optional
+import functools
+from typing import Any, Callable, Optional
from marimo._config.config import Theme
from marimo._messaging.mimetypes import KnownMimeType
@@ -16,10 +17,42 @@ class BokehFormatter(FormatterFactory):
def package_name() -> str:
return "bokeh"
- def register(self) -> None:
+ def register(self) -> Callable[[], None]:
import bokeh.models # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
+ import bokeh.plotting # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
from marimo._output import formatting
+ from marimo._runtime.output import _output
+
+ old_show = bokeh.plotting.show
+ # bokeh always starts with output_notebook() in Jupyter, but this
+ # brings in a dependency on IPython, which we don't need.
+ old_output_notebook = bokeh.plotting.output_notebook
+
+ @functools.wraps(old_show)
+ def show(*args: Any, **kwargs: Any) -> None:
+ # Imperatively display the Bokeh object in the marimo output
+ # area.
+ if args:
+ obj = args[0]
+ else:
+ obj = kwargs.get("obj", None)
+ if obj is not None:
+ _output.append(obj)
+
+ @functools.wraps(old_output_notebook)
+ def output_notebook(*args: Any, **kwargs: Any) -> None:
+ # Noop
+ del args
+ del kwargs
+ pass
+
+ bokeh.plotting.show = show
+ bokeh.plotting.output_notebook = output_notebook
+
+ def unpatch() -> None:
+ bokeh.plotting.show = old_show
+ bokeh.plotting.output_notebook = old_output_notebook
@formatting.formatter(bokeh.models.Plot)
def _show_plot(
@@ -69,6 +102,8 @@ class BokehFormatter(FormatterFactory):
),
)
+ return unpatch
+
def apply_theme(self, theme: Theme) -> None:
from bokeh.io import curdoc # type: ignore
diff --git a/marimo/_smoke_tests/bokeh_figure.py b/marimo/_smoke_tests/bokeh_figure.py
new file mode 100644
index 0000000000..a257abddcc
--- /dev/null
+++ b/marimo/_smoke_tests/bokeh_figure.py
@@ -0,0 +1,51 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "bokeh==3.6.3",
+# "marimo",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.11.4"
+app = marimo.App()
+
+
+def _():
+ import bokeh
+ return (bokeh,)
+
+
+def _():
+ from bokeh.plotting import figure, show, output_notebook
+ # NOOP in marimo
+ output_notebook()
+ return figure, output_notebook, show
+
+
+def _(figure):
+ p = figure()
+ # Shows GlyphRenderer
+ p.scatter([1, 2], [3, 4])
+ return (p,)
+
+
+def _(p, show):
+ show(p)
+ return
+
+
+def _(p):
+ # Shows plot
+ p
+ return
+
+
+if __name__ == "__main__":
+ app.run()