|  | 
|  | 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) | 
0 commit comments