session.ClientData
self, session) session.ClientData(
Access (client-side) information from the browser.
Provides access to client-side information, such as the URL components, the pixel ratio of the device, and the properties of outputs.
Each method in this class reads a reactive input value, which means that the method will error if called outside of a reactive context.
Raises
: RuntimeError
-
If a method is called outside of a reactive context.
Examples
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.py
# pyright: reportUnknownMemberType=false, reportUnknownVariableType=false
import matplotlib.pyplot as plt
import numpy as np
from shiny.express import input, render, session, ui
with ui.sidebar(open="closed"):
ui.input_slider("obs", "Number of observations:", min=0, max=1000, value=500)
ui.markdown(
"""
#### `session.clientdata` values
The following methods are available from the `session.clientdata` object and allow you
to reactively read the client data values from the browser.
"""
)
@render.code
def clientdatatext():
return f"""
.url_hash() -> {session.clientdata.url_hash()}
.url_hash_initial() -> {session.clientdata.url_hash_initial()}
.url_hostname() -> {session.clientdata.url_hostname()}
.url_pathname() -> {session.clientdata.url_pathname()}
.url_port() -> {session.clientdata.url_port()}
.url_protocol() -> {session.clientdata.url_protocol()}
.url_search() -> {session.clientdata.url_search()}
.pixelratio() -> {session.clientdata.pixelratio()}
.output_height("myplot") -> {session.clientdata.output_height("myplot")}
.output_width("myplot") -> {session.clientdata.output_width("myplot")}
.output_hidden("myplot") -> {session.clientdata.output_hidden("myplot")}
.output_bg_color("myplot") -> {session.clientdata.output_bg_color("myplot")}
.output_fg_color("myplot") -> {session.clientdata.output_fg_color("myplot")}
.output_accent_color("myplot") -> {session.clientdata.output_accent_color("myplot")}
.output_font("myplot") -> {session.clientdata.output_font("myplot")}
"""
@render.plot
def myplot():
plt.figure()
plt.hist(np.random.normal(size=input.obs())) # type: ignore
plt.title("This is myplot")
Methods
Name | Description |
---|---|
output_accent_color | Reactively read the accent color of an output. |
output_bg_color | Reactively read the background color of an output. |
output_fg_color | Reactively read the foreground color of an output. |
output_font | Reactively read the font(s) of an output. |
output_height | Reactively read the height of an output. |
output_hidden | Reactively read whether an output is hidden. |
output_width | Reactively read the width of an output. |
pixelratio | Reactively read the pixel ratio of the device. |
url_hash | Reactively read the hash part of the URL. |
url_hash_initial | Reactively read the initial hash part of the URL. |
url_hostname | Reactively read the hostname part of the URL. |
url_pathname | The pathname part of the URL. |
url_port | Reactively read the port part of the URL. |
url_protocol | Reactively read the protocol part of the URL. |
url_search | Reactively read the search part of the URL. |
output_accent_color
id) session.ClientData.output_accent_color(
Reactively read the accent color of an output.
Parameters
id : str
-
The id of the output.
Returns
: str | None
-
The accent color of the output, or None if the output does not exist (or does not report its accent color).
output_bg_color
id) session.ClientData.output_bg_color(
Reactively read the background color of an output.
Parameters
id : str
-
The id of the output.
Returns
: str | None
-
The background color of the output, or None if the output does not exist (or does not report its bg color).
output_fg_color
id) session.ClientData.output_fg_color(
Reactively read the foreground color of an output.
Parameters
id : str
-
The id of the output.
Returns
: str | None
-
The foreground color of the output, or None if the output does not exist (or does not report its fg color).
output_font
id) session.ClientData.output_font(
Reactively read the font(s) of an output.
Parameters
id : str
-
The id of the output.
Returns
: str | None
-
The font family of the output, or None if the output does not exist (or does not report its font styles).
output_height
id) session.ClientData.output_height(
Reactively read the height of an output.
Parameters
id : str
-
The id of the output.
Returns
: float | None
-
The height of the output, or None if the output does not exist (or does not report its height).
output_width
id) session.ClientData.output_width(
Reactively read the width of an output.
Parameters
id : str
-
The id of the output.
Returns
: float | None
-
The width of the output, or None if the output does not exist (or does not report its width).
pixelratio
session.ClientData.pixelratio()
Reactively read the pixel ratio of the device.
url_hash
session.ClientData.url_hash()
Reactively read the hash part of the URL.
url_hash_initial
session.ClientData.url_hash_initial()
Reactively read the initial hash part of the URL.
url_hostname
session.ClientData.url_hostname()
Reactively read the hostname part of the URL.
url_pathname
session.ClientData.url_pathname()
The pathname part of the URL.
url_port
session.ClientData.url_port()
Reactively read the port part of the URL.
url_protocol
session.ClientData.url_protocol()
Reactively read the protocol part of the URL.
url_search
session.ClientData.url_search()
Reactively read the search part of the URL.