Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bug fixes

* An ipyvolume scatter or mesh given a per-point array of color strings (e.g., `ipv.scatter(x, y, z, color=['tomato', 'seagreen'])`) no longer hangs the entire Shiny app with no error: the invalid value is reset to `None` and a clear `TypeError` points to the working alternatives (a single color, or per-point numeric data with a colormap). (#493)
* Widget-to-server messages larger than the websocket limit are now dropped client-side instead of disconnecting the whole session: the user sees a sticky error notification (once per widget), and the console error names the widget and the `--ws-max-size` workaround. Set `SHINYWIDGETS_WS_MAX_SIZE` if you run with a raised limit. (#459)
* A widget that fails after its render function returns (e.g., an Altair/VegaFusion chart without `vl-convert-python` installed) now shows the user an error notification instead of leaving the output blank. (#320)
* Fixed many issues with widgets nested inside other widgets and with widget outputs inserted dynamically (e.g., `output_widget()` inside `@render.ui`). With ipywidgets 8, these either didn't render at all (e.g., a Plotly `FigureWidget` or a styled `HTML` widget inside an ipyleaflet popup) or rendered while logging errors like `Class HTMLStyleModel not found` (e.g., ipyleaflet popups, `WidgetControl` children, and leafmap legends). (#75, #101, #137, #157, #194, #237)
Expand Down
55 changes: 54 additions & 1 deletion shinywidgets/_as_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from bokeh.models import LayoutDOM
from ipympl.backend_nbagg import Canvas
from ipysigma import Sigma
from ipyvolume.widgets import Figure as IpyvolumeFigure
from jupyter_bokeh import BokehModel
from matplotlib.figure import Figure
from panel.viewable import Viewable
Expand Down Expand Up @@ -204,11 +205,12 @@ def as_widget_ipyvolume(x: object) -> Widget:
# ipyvolume is guaranteed to be installed here since this function is only
# called when x comes from the ipyvolume package
from ipyvolume.ui import Container
from ipyvolume.widgets import Figure

# quickscatter() and friends return the Container (figure plus control
# widgets); the Figure is the renderable part
if isinstance(x, Container):
return x.figure
x = x.figure

if not isinstance(x, DOMWidget):
raise TypeError(
Expand All @@ -217,9 +219,60 @@ def as_widget_ipyvolume(x: object) -> Widget:
"`ipv.gcf()` after drawing, or `ipv.figure()`)."
)

if isinstance(x, Figure):
_reject_ipyvolume_string_color_arrays(x)

return x


def _reject_ipyvolume_string_color_arrays(fig: IpyvolumeFigure) -> None:
import numpy as np

# Scatter.color/color_selected and Mesh.color serialize through ipyvolume's
# color_to_binary_or_json(), which walks value[0][0]... until indexing
# raises; indexing into a string never raises ("tomato"[0] == "t", then
# "t"[0] == "t", ...), so a per-point string array infinite-loops and hangs
# the whole process the first time the widget's state is serialized
# (https://github.com/widgetti/ipyvolume/issues/371)
color_traits: list[Tuple[Widget, str]] = [
*[(scatter, "color") for scatter in fig.scatters],
*[(scatter, "color_selected") for scatter in fig.scatters],
*[(mesh, "color") for mesh in fig.meshes],
]
first_bad: Optional[Tuple[Widget, str, np.ndarray]] = None
for mark, trait in color_traits:
value: object = getattr(mark, trait)
if not (isinstance(value, np.ndarray) and value.ndim > 0):
continue
is_string_array = value.dtype.kind in ("U", "S") or (
value.dtype.kind == "O"
and value.size > 0
and isinstance(value.flat[0], str)
)
if not is_string_array:
continue
if first_bad is None:
first_bad = (mark, trait, value)
# Reset every offending trait, not just the one reported below: the
# marks' comm opens were deferred at construction and still serialize
# state at the next reactive flush (nothing closes widgets created
# during a failed render), so a string array left in place would hang
# the process despite the error
setattr(mark, trait, None)

if first_bad is not None:
mark, trait, value = first_bad
raise TypeError(
f"ipyvolume `{type(mark).__name__}.{trait}` was given an array "
f"of strings (dtype={value.dtype}, shape={value.shape}), which "
"ipyvolume's serializer infinite-loops on, hanging the entire "
"Shiny app. Pass a single color (e.g., color='red') or "
"per-point numeric data (e.g., values to map through a "
"colormap, or an (N, 3) RGB float array) instead. "
"The invalid value was reset to None to keep the app running."
)


def as_widget_lonboard(x: object) -> Widget:
# lonboard is guaranteed to be installed here since this function is only
# called when x comes from the lonboard package
Expand Down
145 changes: 145 additions & 0 deletions tests/unit/test_ipyvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,151 @@ def test_as_widget_errors_helpfully_for_a_bare_scene_object(
as_widget(scatter)


def test_as_widget_rejects_per_point_string_colors(widget_session: Session) -> None:
ipv = pytest.importorskip("ipyvolume")

with session_context(widget_session):
fig = ipv.figure()
scatter = ipv.scatter(
[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color=["tomato", "seagreen"]
)
with pytest.raises(TypeError, match=r"Scatter\.color.*array of strings"):
as_widget(fig)
# The invalid value is sanitized away: a string array left in place
# would hang the next flush in ipyvolume's serializer
assert scatter.color is None


def test_as_widget_rejects_string_colors_from_the_quickscatter_container(
widget_session: Session,
) -> None:
ipv = pytest.importorskip("ipyvolume")

with session_context(widget_session):
container = ipv.quickscatter(
[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color=["tomato", "seagreen"]
)
with pytest.raises(TypeError, match=r"Scatter\.color.*array of strings"):
as_widget(container)
assert container.figure.scatters[0].color is None


def test_as_widget_rejects_string_color_selected(widget_session: Session) -> None:
ipv = pytest.importorskip("ipyvolume")

with session_context(widget_session):
fig = ipv.figure()
scatter = ipv.scatter([0.0, 1.0], [0.0, 1.0], [0.0, 1.0])
scatter.color_selected = ["tomato", "seagreen"]
with pytest.raises(
TypeError, match=r"Scatter\.color_selected.*array of strings"
):
as_widget(fig)
assert scatter.color_selected is None


def test_as_widget_rejects_mesh_string_colors(widget_session: Session) -> None:
ipv = pytest.importorskip("ipyvolume")
import numpy as np

with session_context(widget_session):
mesh = ipv.widgets.Mesh(
x=np.zeros((2, 2)), y=np.zeros((2, 2)), z=np.zeros((2, 2)), color="red"
)
mesh.color = ["tomato", "seagreen"]
fig = ipv.Figure(meshes=[mesh])
with pytest.raises(TypeError, match=r"Mesh\.color.*array of strings"):
as_widget(fig)
assert mesh.color is None


def test_flush_after_string_color_rejection_does_not_hang() -> None:
pytest.importorskip("ipyvolume")

# The coercion error must sanitize the invalid trait: the scatter's
# deferred comm open still runs at the next flush (nothing closes widgets
# created during a failed render) and would infinite-loop in ipyvolume's
# serializer on a leftover string array. Run out-of-process so a
# regression fails here instead of hanging the test suite.
code = """
import asyncio

import shinywidgets._shinywidgets as sw
import ipyvolume as ipv
from shiny import App, reactive, ui
from shiny._connection import MockConnection
from shiny.reactive._reactives import Context
from shiny.session import session_context
from shinywidgets import render_ipyvolume


async def main():
app = App(ui.page_fluid(), lambda input, output, session: None)
session = app._create_session(MockConnection())
sw.SHINYWIDGETS_CDN_ONLY = True

r = render_ipyvolume()

@r
def bad():
ipv.scatter(
[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color=["tomato", "seagreen"]
)
return ipv.gcf()

with session_context(session):
ctx = Context()
with ctx():
try:
await r.render()
except TypeError:
pass
await reactive.flush()
print("FLUSH OK", flush=True)


asyncio.run(main())
"""
try:
proc = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
check=False,
timeout=60,
)
except subprocess.TimeoutExpired:
pytest.fail(
"The flush after a rejected string color array hung -- the "
"rejected widget's deferred comm open serialized the invalid value"
)
assert "FLUSH OK" in proc.stdout, proc.stderr


def test_as_widget_accepts_a_scalar_string_color(widget_session: Session) -> None:
ipv = pytest.importorskip("ipyvolume")

with session_context(widget_session):
fig = ipv.figure()
ipv.scatter([0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color="tomato")
assert as_widget(fig) is fig


def test_as_widget_accepts_numeric_per_point_colors(widget_session: Session) -> None:
ipv = pytest.importorskip("ipyvolume")
import numpy as np

with session_context(widget_session):
fig = ipv.figure()
ipv.scatter(
[0.0, 1.0],
[0.0, 1.0],
[0.0, 1.0],
color=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]),
)
assert as_widget(fig) is fig


def test_vuetify_widget_open_serves_jupyter_vue(
widget_session: Session, monkeypatch: pytest.MonkeyPatch
) -> None:
Expand Down
Loading