Skip to content

Commit 2ae8e4b

Browse files
authored
Allow for direct rendering of plotly's Figure class (#83)
1 parent c2b6d35 commit 2ae8e4b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

shinywidgets/_shinywidgets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ def _as_widget(x: object) -> Widget:
258258
)
259259
except Exception as e:
260260
raise RuntimeError(f"Failed to coerce {x} into a BokehModel: {e}")
261+
elif pkg == "plotly" and not isinstance(x, Widget):
262+
try:
263+
from plotly.graph_objects import Figure, FigureWidget
264+
265+
if isinstance(x, FigureWidget):
266+
pass
267+
elif isinstance(x, Figure):
268+
x = FigureWidget(x.data, x.layout)
269+
else:
270+
raise TypeError(
271+
f"{x} is not a plotly.graph_objects.Figure or FigureWidget"
272+
)
273+
except Exception as e:
274+
raise RuntimeError(
275+
f"Failed to coerce {x} into a plotly.graph_objects.FigureWidget: {e}"
276+
)
261277

262278
if isinstance(x, Widget):
263279
return x

0 commit comments

Comments
 (0)