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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: rendering with panel when not a native panel object (#3361)
Myles Scolnick484316a9400f
diff --git a/marimo/_output/formatters/holoviews_formatters.py b/marimo/_output/formatters/holoviews_formatters.pyindex 7581c72996..d1dcedd0a9 100644--- a/marimo/_output/formatters/holoviews_formatters.py+++ b/marimo/_output/formatters/holoviews_formatters.py@@ -1,12 +1,16 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+from marimo import _loggersfrom marimo._config.config import Themefrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.mimetypes import KnownMimeTypefrom marimo._output.formatters.formatter_factory import FormatterFactory+from marimo._output.formatting import as_htmlfrom marimo._plugins.ui._impl.from_panel import panel as from_panel+LOGGER = _loggers.get_logger(__name__)+class HoloViewsFormatter(FormatterFactory):@staticmethod@@ -36,7 +40,15 @@ class HoloViewsFormatter(FormatterFactory):| hv.core.ndmapping.NdMapping),) -> tuple[KnownMimeType, str]:- return from_panel(plot)._mime_()+ try:+ return from_panel(plot)._mime_()+ except Exception as e:+ LOGGER.exception("Failed to render holoviews plot", exc_info=e)+ backend_output = hv.render(plot)+ # Call as_html to recurse back into the formatter+ # this may be bokeh, matplotlib, or plotly+ html = as_html(backend_output)+ return ("text/html", html.text)def apply_theme(self, theme: Theme) -> None:import holoviews as hv # type: ignorediff --git a/marimo/_plugins/ui/_impl/from_panel.py b/marimo/_plugins/ui/_impl/from_panel.pyindex 245cf1e292..dfc8f97cf4 100644--- a/marimo/_plugins/ui/_impl/from_panel.py+++ b/marimo/_plugins/ui/_impl/from_panel.py@@ -218,7 +218,7 @@ class panel(UIElement[T, T]):# This gets set to True in super().__init__()self._initialized = False- ref, docs_json, render_json = render_component(obj)+ ref, docs_json, render_json = render_component(self.obj)self._ref = refself._manager = PanelCommManager(plot_id=ref) # type: ignore[no-untyped-call]diff --git a/marimo/_smoke_tests/third_party/holoviews_scatter.py b/marimo/_smoke_tests/third_party/holoviews_scatter.pynew file mode 100644index 0000000000..25197de855--- /dev/null+++ b/marimo/_smoke_tests/third_party/holoviews_scatter.py@@ -0,0 +1,60 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "bokeh==3.6.2",+# "hvplot==0.11.2",+# "marimo",+# "panel==1.5.5",+# ]+# ///++import marimo++__generated_with = "0.10.9"+app = marimo.App(width="medium")+++def _():+ import hvplot.pandas+ import panel++ from bokeh.sampledata.iris import flowers as df++ df.sample(n=5)++ df.hvplot.scatter(+ x="sepal_length",+ y="sepal_width",+ by="species",+ legend="top",+ height=400,+ width=400,+ )+ return df, hvplot, panel+++def _():+ import marimo as mo+ return (mo,)+++def _(mo):+ mo.ui.panel({1: 2})+ return+++def _(mo):+ import panel as pn++ slider = pn.widgets.IntSlider(start=0, end=10, value=5)+ rx_stars = mo.ui.panel(slider.rx() * "*")+ rx_stars+ return pn, rx_stars, slider+++if __name__ == "__main__":+ app.run()diff --git a/pyproject.toml b/pyproject.tomlindex f15dc7809b..bbfb272d8f 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -231,6 +231,7 @@ extra-dependencies = ["altair>=5.4.0","polars~=1.9.0","pandas>=1.5.3",+ "hvplot~=0.11.2","geopandas~=0.14.4; python_version > '3.9'",# FOr testing sql"ibis-framework[duckdb]~=9.5.0; python_version > '3.9'",diff --git a/tests/_plugins/ui/_impl/test_panel.py b/tests/_plugins/ui/_impl/test_panel.pynew file mode 100644index 0000000000..173a7e0761--- /dev/null+++ b/tests/_plugins/ui/_impl/test_panel.py@@ -0,0 +1,77 @@+# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations++from unittest.mock import Mock++import pytest++from marimo._dependencies.dependencies import DependencyManager+from marimo._plugins.ui._impl.from_panel import panel+from marimo._runtime.runtime import Kernel+from tests.conftest import ExecReqProvider++HAS_DEPS = DependencyManager.panel.has()++if HAS_DEPS:+ import panel as pn+else:+ pn = Mock()++[email protected](not HAS_DEPS, reason="optional dependencies not installed")+class TestPanel:+ @staticmethod+ async def test_instances(k: Kernel, exec_req: ExecReqProvider) -> None:+ await k.run(+ [+ exec_req.get(+ """+import panel as pn+import marimo as mo++slider = pn.widgets.IntSlider(start=0, end=10, value=5)+slider = mo.ui.panel(slider)+"""+ )+ ]+ )++ @staticmethod+ def test_proxy_attributes() -> None:+ slider = pn.widgets.IntSlider(start=0, end=10, value=5)+ wrapped = panel(slider)++ # Test attribute access+ assert (+ wrapped.value == {}+ ) # slider gets wrapped in a row, so it's value is empty+ assert wrapped.start == 0+ assert wrapped.end == 10++ # Test attribute modification+ wrapped.value = 7+ assert (+ wrapped.value == {}+ ) # slider gets wrapped in a row, so it's value is empty+ assert slider.value == 7++ @staticmethod+ def test_layout_components() -> None:+ row = panel(pn.Row("Test"))+ col = panel(pn.Column("Test"))+ tabs = panel(pn.Tabs(("Tab1", "Content")))++ assert isinstance(row, panel)+ assert isinstance(col, panel)+ assert isinstance(tabs, panel)++ @staticmethod+ def test_any_object() -> None:+ assert panel({}) is not None+ assert panel([]) is not None+ assert panel(()) is not None+ assert panel(1) is not None+ assert panel("test") is not None+ assert panel(True) is not None+ assert panel(False) is not None+ assert panel(None) is not None