Skip to content

Commit

Permalink
Revert to reactive property approach. Only provide decorators for pac…
Browse files Browse the repository at this point in the history
…kages that need coercion
  • Loading branch information
cpsievert committed Jan 16, 2024
1 parent 5764d44 commit 0ef35ad
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 317 deletions.
2 changes: 0 additions & 2 deletions shinywidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ._render_widget import (
render_altair,
render_bokeh,
render_leaflet,
render_plotly,
render_pydeck,
render_widget,
Expand All @@ -22,7 +21,6 @@
"render_widget",
"render_altair",
"render_bokeh",
"render_leaflet",
"render_plotly",
"render_pydeck",
# Reactive read second
Expand Down
8 changes: 4 additions & 4 deletions shinywidgets/_as_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def as_widget(x: object) -> Widget:

def as_widget_altair(x: object) -> Optional[Widget]:
try:
from altair import JupyterChart
from altair import JupyterChart # pyright: ignore[reportMissingTypeStubs]
except ImportError:
raise RuntimeError(
"Failed to import altair.JupyterChart (do you need to pip install -U altair?)"
Expand All @@ -55,9 +55,9 @@ def as_widget_bokeh(x: object) -> Optional[Widget]:
# TODO: ideally we'd do this in set_layout_defaults() but doing
# `BokehModel(x)._model.sizing_mode = "stretch_both"`
# there, but that doesn't seem to work??
from bokeh.plotting import figure
from bokeh.plotting import figure # pyright: ignore[reportMissingTypeStubs]

if isinstance(x, figure):
if isinstance(x, figure): # type: ignore
x.sizing_mode = "stretch_both" # pyright: ignore[reportGeneralTypeIssues]

return BokehModel(x) # type: ignore
Expand All @@ -67,7 +67,7 @@ def as_widget_plotly(x: object) -> Optional[Widget]:
# Don't need a try import here since this won't be called unless x is a plotly object
import plotly.graph_objects as go # pyright: ignore[reportMissingTypeStubs]

if not isinstance(x, go.Figure):
if not isinstance(x, go.Figure): # type: ignore
raise TypeError(
f"Don't know how to coerce {x} into a plotly.graph_objects.FigureWidget object."
)
Expand Down
25 changes: 8 additions & 17 deletions shinywidgets/_render_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,41 @@

from typing import TYPE_CHECKING

from ipywidgets.widgets import Widget # pyright: ignore[reportMissingTypeStubs]

if TYPE_CHECKING:
from altair import JupyterChart
from altair import JupyterChart # pyright: ignore[reportMissingTypeStubs]
from jupyter_bokeh import BokehModel # pyright: ignore[reportMissingTypeStubs]
from plotly.graph_objects import ( # pyright: ignore[reportMissingTypeStubs]
FigureWidget,
)
from pydeck.widget import DeckGLWidget # pyright: ignore[reportMissingTypeStubs]

# Leaflet Widget class is the same as a Widget
# from ipyleaflet import Widget as LeafletWidget
else:
JupyterChart = BokehModel = FigureWidget = DeckGLWidget = object

from ._render_widget_base import ValueT, WidgetT, render_widget_base

__all__ = (
"render_widget",
"render_altair",
"render_bokeh",
"render_leaflet",
"render_plotly",
"render_pydeck",
)


class render_widget(render_widget_base[ValueT, Widget]):
# In the generic case, just relay whatever the user's return type is
# since we're not doing any coercion
class render_widget(render_widget_base[WidgetT, WidgetT]):
...


# Package specific renderers that require coercion (via as_widget())
# NOTE: the types on these classes should mirror what as_widget() does
class render_altair(render_widget_base[ValueT, JupyterChart]):
...


class render_bokeh(render_widget_base[ValueT, BokehModel]):
...


class render_leaflet(render_widget_base[WidgetT, WidgetT]):
...


class render_plotly(render_widget_base[ValueT, FigureWidget]):
...


class render_pydeck(render_widget_base[ValueT, DeckGLWidget]):
...
Loading

0 comments on commit 0ef35ad

Please sign in to comment.