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
11 changes: 9 additions & 2 deletions templates/1-basic/py/shinyreact.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from shiny.html_dependencies import shiny_deps
from shiny.types import Jsonifiable
from shiny.render.renderer import Renderer, ValueFn
from shiny.module import resolve_id
from typing import Any, Mapping, Optional, Sequence, Union


Expand All @@ -22,7 +23,6 @@ def page_react(
css_file: str | None = "main.css",
lang: str = "en",
) -> ui.Tag:

head_items: list[ui.TagChild] = []

if js_file:
Expand Down Expand Up @@ -89,6 +89,10 @@ async def post_message(session: Session, type: str, data: JsonifiableIn):
React components using useShinyMessageHandler() hook. This wraps messages in
a standard format and sends them via the "shinyReactMessage" channel.

When used within a Shiny module (@module.server), the type is automatically
namespaced using resolve_id(). Outside of modules, the type is passed through
unchanged.

Parameters
----------
session
Expand All @@ -99,4 +103,7 @@ async def post_message(session: Session, type: str, data: JsonifiableIn):
data
The data to send to the client
"""
await session.send_custom_message("shinyReactMessage", {"type": type, "data": data})
namespaced_type = resolve_id(type)
await session.send_custom_message(
"shinyReactMessage", {"type": namespaced_type, "data": data}
)
7 changes: 6 additions & 1 deletion templates/1-basic/r/shinyreact.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ render_json <- function(
#' React components using useShinyMessageHandler() hook. This wraps messages in a
#' standard format and sends them via the "shinyReactMessage" channel.
#'
#' When used within a Shiny module (moduleServer), the type is automatically
#' namespaced using session$ns(). Outside of modules, the type is passed through
#' unchanged.
#'
#' @param session The Shiny session object
#' @param type The message type (should match messageType in useShinyMessageHandler)
#' @param data The data to send to the client
post_message <- function(session, type, data) {
namespaced_type <- session$ns(type)
session$sendCustomMessage(
"shinyReactMessage",
list(
type = type,
type = namespaced_type,
data = data
)
)
Expand Down
11 changes: 9 additions & 2 deletions templates/2-scaffold/py/shinyreact.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from shiny.html_dependencies import shiny_deps
from shiny.types import Jsonifiable
from shiny.render.renderer import Renderer, ValueFn
from shiny.module import resolve_id
from typing import Any, Mapping, Optional, Sequence, Union


Expand All @@ -22,7 +23,6 @@ def page_react(
css_file: str | None = "main.css",
lang: str = "en",
) -> ui.Tag:

head_items: list[ui.TagChild] = []

if js_file:
Expand Down Expand Up @@ -89,6 +89,10 @@ async def post_message(session: Session, type: str, data: JsonifiableIn):
React components using useShinyMessageHandler() hook. This wraps messages in
a standard format and sends them via the "shinyReactMessage" channel.

When used within a Shiny module (@module.server), the type is automatically
namespaced using resolve_id(). Outside of modules, the type is passed through
unchanged.

Parameters
----------
session
Expand All @@ -99,4 +103,7 @@ async def post_message(session: Session, type: str, data: JsonifiableIn):
data
The data to send to the client
"""
await session.send_custom_message("shinyReactMessage", {"type": type, "data": data})
namespaced_type = resolve_id(type)
await session.send_custom_message(
"shinyReactMessage", {"type": namespaced_type, "data": data}
)
7 changes: 6 additions & 1 deletion templates/2-scaffold/r/shinyreact.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ render_json <- function(
#' React components using useShinyMessageHandler() hook. This wraps messages in a
#' standard format and sends them via the "shinyReactMessage" channel.
#'
#' When used within a Shiny module (moduleServer), the type is automatically
#' namespaced using session$ns(). Outside of modules, the type is passed through
#' unchanged.
#'
#' @param session The Shiny session object
#' @param type The message type (should match messageType in useShinyMessageHandler)
#' @param data The data to send to the client
post_message <- function(session, type, data) {
namespaced_type <- session$ns(type)
session$sendCustomMessage(
"shinyReactMessage",
list(
type = type,
type = namespaced_type,
data = data
)
)
Expand Down