Skip to content

Commit ec74610

Browse files
committed
Add examples; Update changelog
1 parent e1e4526 commit ec74610

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
* `shiny create` includes new and improved `ui.Chat()` template options. Most of these templates leverage the new [`{chatlas}` package](https://posit-dev.github.io/chatlas/), our opinionated approach to interfacing with various LLM. (#1806)
1919

20-
* Client data values (e.g., url info, output sizes/styles, etc.) can now be accessed in the server-side Python code via `session.clientdata`, which is collection of reactive `Inputs`. For example, `session.clientdata.url_search()` reactively reads the URL search parameters. See [this PR](https://github.com/posit-dev/py-shiny/pull/1832) for a more complete example. (#1832)
20+
* Client data values (e.g., url info, output sizes/styles, etc.) can now be accessed in the server-side Python code via `session.clientdata`. For example, `session.clientdata.url_search()` reactively reads the URL search parameters. (#1832)
2121

2222
* Available `input` ids can now be listed via `dir(input)`. This also works on the new `session.clientdata` object. (#1832)
2323

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import textwrap
2+
3+
# pyright: reportUnknownMemberType=false, reportUnknownVariableType=false
4+
import matplotlib.pyplot as plt
5+
import numpy as np
6+
7+
from shiny import App, Inputs, Outputs, Session, render, ui
8+
9+
app_ui = ui.page_sidebar(
10+
ui.sidebar(
11+
ui.input_slider("obs", "Number of observations:", min=0, max=1000, value=500),
12+
open="closed",
13+
),
14+
ui.markdown(
15+
"""
16+
#### `session.clientdata` values
17+
18+
The following methods are available from the `session.clientdata` object and allow you
19+
to reactively read the client data values from the browser.
20+
"""
21+
),
22+
ui.output_text_verbatim("clientdatatext"),
23+
ui.output_plot("myplot"),
24+
)
25+
26+
27+
def server(input: Inputs, output: Outputs, session: Session):
28+
29+
@render.code
30+
def clientdatatext():
31+
return textwrap.dedent(
32+
f"""
33+
.url_hash() -> {session.clientdata.url_hash()}
34+
.url_hash_initial() -> {session.clientdata.url_hash_initial()}
35+
.url_hostname() -> {session.clientdata.url_hostname()}
36+
.url_pathname() -> {session.clientdata.url_pathname()}
37+
.url_port() -> {session.clientdata.url_port()}
38+
.url_protocol() -> {session.clientdata.url_protocol()}
39+
.url_search() -> {session.clientdata.url_search()}
40+
.pixelratio() -> {session.clientdata.pixelratio()}
41+
42+
.output_height("myplot") -> {session.clientdata.output_height("myplot")}
43+
.output_width("myplot") -> {session.clientdata.output_width("myplot")}
44+
.output_hidden("myplot") -> {session.clientdata.output_hidden("myplot")}
45+
.output_bg_color("myplot") -> {session.clientdata.output_bg_color("myplot")}
46+
.output_fg_color("myplot") -> {session.clientdata.output_fg_color("myplot")}
47+
.output_accent_color("myplot") -> {session.clientdata.output_accent_color("myplot")}
48+
.output_font("myplot") -> {session.clientdata.output_font("myplot")}
49+
50+
"""
51+
)
52+
53+
@render.plot
54+
def myplot():
55+
plt.figure()
56+
plt.hist(np.random.normal(size=input.obs())) # type: ignore
57+
plt.title("This is myplot")
58+
59+
60+
app = App(app_ui, server)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# pyright: reportUnknownMemberType=false, reportUnknownVariableType=false
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
from shiny.express import input, render, session, ui
6+
7+
with ui.sidebar(open="closed"):
8+
ui.input_slider("obs", "Number of observations:", min=0, max=1000, value=500)
9+
10+
ui.markdown(
11+
"""
12+
#### `session.clientdata` values
13+
14+
The following methods are available from the `session.clientdata` object and allow you
15+
to reactively read the client data values from the browser.
16+
"""
17+
)
18+
19+
20+
@render.code
21+
def clientdatatext():
22+
return f"""
23+
.url_hash() -> {session.clientdata.url_hash()}
24+
.url_hash_initial() -> {session.clientdata.url_hash_initial()}
25+
.url_hostname() -> {session.clientdata.url_hostname()}
26+
.url_pathname() -> {session.clientdata.url_pathname()}
27+
.url_port() -> {session.clientdata.url_port()}
28+
.url_protocol() -> {session.clientdata.url_protocol()}
29+
.url_search() -> {session.clientdata.url_search()}
30+
.pixelratio() -> {session.clientdata.pixelratio()}
31+
32+
.output_height("myplot") -> {session.clientdata.output_height("myplot")}
33+
.output_width("myplot") -> {session.clientdata.output_width("myplot")}
34+
.output_hidden("myplot") -> {session.clientdata.output_hidden("myplot")}
35+
.output_bg_color("myplot") -> {session.clientdata.output_bg_color("myplot")}
36+
.output_fg_color("myplot") -> {session.clientdata.output_fg_color("myplot")}
37+
.output_accent_color("myplot") -> {session.clientdata.output_accent_color("myplot")}
38+
.output_font("myplot") -> {session.clientdata.output_font("myplot")}
39+
"""
40+
41+
42+
@render.plot
43+
def myplot():
44+
plt.figure()
45+
plt.hist(np.random.normal(size=input.obs())) # type: ignore
46+
plt.title("This is myplot")

shiny/session/_session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ def _manage_inputs(self, data: dict[str, object]) -> None:
694694
# The keys[0] value is already a fully namespaced id; make that explicit by
695695
# wrapping it in ResolvedId, otherwise self.input will throw an id
696696
# validation error.
697-
698697
self.input[ResolvedId(keys[0])]._set(val)
699698

700699
self.output._manage_hidden()

0 commit comments

Comments
 (0)