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: respect holoviews hv.output() settings when rendering in marimo (#6732)
Myles Scolnick17ea5ca3d644
diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.pyindex 10aad7c5d7..b1ca473315 100644--- a/marimo/_dependencies/dependencies.py+++ b/marimo/_dependencies/dependencies.py@@ -220,6 +220,7 @@ class DependencyManager:google_ai = Dependency("google.genai")groq = Dependency("groq")panel = Dependency("panel")+ holoviews = Dependency("holoviews")sqlalchemy = Dependency("sqlalchemy")pylsp = Dependency("pylsp")basedpyright = Dependency("basedpyright")diff --git a/marimo/_plugins/ui/_impl/from_panel.py b/marimo/_plugins/ui/_impl/from_panel.pyindex 7490512faf..c8b235d730 100644--- a/marimo/_plugins/ui/_impl/from_panel.py+++ b/marimo/_plugins/ui/_impl/from_panel.py@@ -172,6 +172,78 @@ def render_component(return ref, docs_json, render_json # type: ignore[return-value]+def _extract_holoviews_settings(obj: Any) -> dict[str, Any]:+ """+ Extract renderer settings from holoviews objects to pass to Panel.++ This ensures that settings like widget_location configured via hv.output()+ are respected when rendering holoviews objects through Panel.+ """+ try:+ import holoviews as hv # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501++ # Check if the object is a holoviews object+ if not isinstance(+ obj,+ (+ hv.core.ViewableElement,+ hv.core.Layout,+ hv.HoloMap,+ hv.DynamicMap,+ hv.core.spaces.HoloMap,+ hv.core.ndmapping.UniformNdMapping,+ hv.core.ndmapping.NdMapping,+ ),+ ):+ return {}++ # Get the current backend+ # Try to determine which backend is active+ backend = None+ if hasattr(hv.Store, "current_backend"):+ backend = hv.Store.current_backend+ elif hasattr(hv, "extension") and hasattr(+ hv.extension, "_loaded_backends"+ ):+ # Get the first loaded backend+ loaded_backends = list(hv.extension._loaded_backends.keys())+ if loaded_backends:+ backend = loaded_backends[0]++ if not backend:+ return {}++ # Get the renderer for the active backend+ try:+ renderer = hv.renderer(backend)+ except Exception:+ return {}++ # Extract relevant settings that Panel's HoloViews pane accepts+ panel_kwargs: dict[str, Any] = {}++ # widget_location is a common setting that affects widget placement+ if (+ hasattr(renderer, "widget_location")+ and renderer.widget_location is not None+ ):+ panel_kwargs["widget_location"] = renderer.widget_location++ # center is another setting that can be configured+ if hasattr(renderer, "center") and renderer.center is not None:+ panel_kwargs["center"] = renderer.center++ return panel_kwargs++ except ImportError:+ # holoviews is not installed+ return {}+ except Exception as e:+ # Log the error but don't fail - just don't apply settings+ LOGGER.debug(f"Failed to extract holoviews settings: {e}")+ return {}++@mddocclass panel(UIElement[T, T]):"""Create a UIElement from a Panel component.@@ -203,7 +275,10 @@ class panel(UIElement[T, T]):from panel.models.comm_manager import CommManager as PanelCommManagerfrom panel.pane import panel as panel_func- self.obj: Viewable = panel_func(obj) # type: ignore[assignment]+ # Extract holoviews renderer settings if the object is a holoviews object+ panel_kwargs = _extract_holoviews_settings(obj)++ self.obj: Viewable = panel_func(obj, **panel_kwargs) # type: ignore[assignment]# This gets set to True in super().__init__()self._initialized = Falsediff --git a/marimo/_smoke_tests/third_party/holoviews_output_options.py b/marimo/_smoke_tests/third_party/holoviews_output_options.pynew file mode 100644index 0000000000..78839214fb--- /dev/null+++ b/marimo/_smoke_tests/third_party/holoviews_output_options.py@@ -0,0 +1,49 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "holoviews==1.21.0",+# "hvplot==0.12.1",+# "netcdf4==1.7.2",+# "pooch==1.8.2",+# "xarray==2025.9.0",+# ]+# ///++import marimo++__generated_with = "0.15.2"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)+++def _():+ import xarray as xr+ import hvplot.xarray # noqa: F401+ import holoviews as hv++ hv.extension('bokeh')+ hv.output(widget_location='top')++ air_ds = xr.tutorial.open_dataset('air_temperature').load()+ air_ds.air.hvplot.image()+ return air_ds, hv, xr+++def _():+ return+++def _():+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_plugins/ui/_impl/test_panel.py b/tests/_plugins/ui/_impl/test_panel.pyindex 173a7e0761..668b2e22e0 100644--- a/tests/_plugins/ui/_impl/test_panel.py+++ b/tests/_plugins/ui/_impl/test_panel.py@@ -6,11 +6,15 @@ from unittest.mock import Mockimport pytestfrom marimo._dependencies.dependencies import DependencyManager-from marimo._plugins.ui._impl.from_panel import panel+from marimo._plugins.ui._impl.from_panel import (+ _extract_holoviews_settings,+ panel,+)from marimo._runtime.runtime import Kernelfrom tests.conftest import ExecReqProviderHAS_DEPS = DependencyManager.panel.has()+HAS_HOLOVIEWS = DependencyManager.holoviews.has()if HAS_DEPS:import panel as pn@@ -75,3 +79,100 @@ slider = mo.ui.panel(slider)assert panel(True) is not Noneassert panel(False) is not Noneassert panel(None) is not None++[email protected](not HAS_HOLOVIEWS, reason="holoviews not installed")+class TestHoloViewsSettings:+ @staticmethod+ def test_extract_holoviews_settings_with_widget_location() -> None:+ """Test that widget_location is extracted from holoviews renderer."""+ import holoviews as hv++ # Set up holoviews with bokeh backend+ hv.extension("bokeh")+ hv.output(widget_location="top")++ # Create a simple holoviews object+ curve = hv.Curve([1, 2, 3])++ # Extract settings+ settings = _extract_holoviews_settings(curve)++ # Verify widget_location is extracted+ assert "widget_location" in settings+ assert settings["widget_location"] == "top"++ @staticmethod+ def test_extract_holoviews_settings_with_center() -> None:+ """Test that center is extracted from holoviews renderer."""+ import holoviews as hv++ # Set up holoviews with bokeh backend+ hv.extension("bokeh")+ hv.output(center=True)++ # Create a simple holoviews object+ curve = hv.Curve([1, 2, 3])++ # Extract settings+ settings = _extract_holoviews_settings(curve)++ # Verify center is extracted+ assert "center" in settings+ assert settings["center"] is True++ @staticmethod+ def test_extract_holoviews_settings_non_holoviews_object() -> None:+ """Test that non-holoviews objects return empty dict."""+ # Test with regular objects+ assert _extract_holoviews_settings({}) == {}+ assert _extract_holoviews_settings([1, 2, 3]) == {}+ assert _extract_holoviews_settings("test") == {}++ @staticmethod+ def test_extract_holoviews_settings_no_widget_location() -> None:+ """Test extraction when widget_location is not set (None)."""+ import holoviews as hv++ # Set up holoviews with bokeh backend but no widget_location+ hv.extension("bokeh")++ # Get the renderer and ensure widget_location is None+ renderer = hv.renderer("bokeh")+ renderer.widget_location = None++ # Create a simple holoviews object+ curve = hv.Curve([1, 2, 3])++ # Extract settings+ settings = _extract_holoviews_settings(curve)++ # Verify that None values are not included+ assert "widget_location" not in settings+++ not (HAS_DEPS and HAS_HOLOVIEWS),+ reason="panel and holoviews not installed",+)+class TestPanelWithHoloViews:+ @staticmethod+ def test_panel_respects_holoviews_output_settings() -> None:+ """Test that panel() passes holoviews settings to Panel pane."""+ import holoviews as hv++ # Set up holoviews with specific settings+ hv.extension("bokeh")+ hv.output(widget_location="bottom")++ # Create a holoviews object+ curve = hv.Curve([1, 2, 3])++ # Wrap in panel+ wrapped = panel(curve)++ # The wrapped object should be a Panel pane+ # We can't directly check if widget_location was passed,+ # but we can verify the object was created successfully+ assert wrapped is not None+ assert isinstance(wrapped, panel)