Skip to content

Commit 8480f90

Browse files
authored
Merge pull request #50 from rstudio/module-resolve
Make output_widget compatible with Shiny modules
2 parents a02ef62 + 39467d6 commit 8480f90

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

shinywidgets/_shinywidgets.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from shiny.http_staticfiles import StaticFiles
2727
from shiny.render import RenderFunction, RenderFunctionAsync
2828
from shiny.session import get_current_session, require_active_session
29+
from shiny.module import resolve_id
2930

3031
from ._comm import BufferType, ShinyComm, ShinyCommManager
3132
from ._dependencies import (
@@ -39,6 +40,7 @@
3940
def output_widget(
4041
id: str, *, width: Optional[str] = None, height: Optional[str] = None
4142
) -> Tag:
43+
id = resolve_id(id)
4244
return tags.div(
4345
*libembed_dependency(),
4446
output_binding_dependency(),
@@ -59,6 +61,10 @@ def init_shiny_widget(w: Widget):
5961
raise RuntimeError(
6062
"shinywidgets requires that all ipywidgets be constructed within an active Shiny session"
6163
)
64+
# Break out of any module-specific session. Otherwise, input.shinywidgets_comm_send
65+
# will be some module-specific copy.
66+
while hasattr(session, "_parent"):
67+
session = session._parent
6268

6369
# `Widget` has `comm = Instance('ipykernel.comm.Comm')` which means we'd get a
6470
# runtime error if we try to set this attribute to a different class, but
@@ -121,9 +127,9 @@ def init_shiny_widget(w: Widget):
121127
# Handle messages from the client. Note that widgets like qgrid send client->server messages
122128
# to figure out things like what filter to be shown in the table.
123129
@reactive.Effect
124-
@reactive.event(session.input["shinywidgets_comm_send"])
130+
@reactive.event(session.input.shinywidgets_comm_send)
125131
def _():
126-
msg_txt = session.input["shinywidgets_comm_send"]()
132+
msg_txt = session.input.shinywidgets_comm_send()
127133
msg = json.loads(msg_txt)
128134
comm_id = msg["content"]["comm_id"]
129135
comm: ShinyComm = COMM_MANAGER.comms[comm_id]

0 commit comments

Comments
 (0)