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.

Allow numpy steps (#2958)

vincent d warmerdam994f71d7fb7c
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 952e3d2f8a..940b663600 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -29,6 +29,7 @@ from marimo._data.series import (
get_category_series_info,
get_number_series_info,
)
+from marimo._dependencies.dependencies import DependencyManager
from marimo._output.rich_help import mddoc
from marimo._plugins.core.web_component import JSONType
from marimo._plugins.ui._core.ui_element import S as JSONTypeBound, UIElement
@@ -232,6 +233,12 @@ class slider(UIElement[Numeric, Numeric]):
)
# If steps are provided
if steps is not None:
+ # Cast to a list in case user passes a numpy array
+ if not isinstance(steps, list):
+ if DependencyManager.numpy.has():
+ import numpy as np
+ if isinstance(steps, np.ndarray):
+ steps = steps.tolist()
self._dtype = _infer_dtype(steps)
self._mapping = dict(enumerate(steps))
try:
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 08b3da96d4..2fb2c4c123 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -9,6 +9,7 @@ from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins import ui
HAS_PANDAS = DependencyManager.pandas.has()
+HAS_NUMPY = DependencyManager.numpy.has()
def test_number_init() -> None:
@@ -280,6 +281,15 @@ def test_range_slider_from_dataframe() -> None:
assert slider._args.label == "Custom label"
[email protected](not HAS_NUMPY, reason="numpy not installed")
+def test_range_slider_from_np_array() -> None:
+ import numpy as np
+
+ slider = ui.slider(steps=np.array([1, 2, 3]))
+ assert slider.start == 1
+ assert slider.stop == 3
+
+
def test_text() -> None:
assert ui.text().value == ""
assert ui.text(value="hello world").value == "hello world"