|
| 1 | +from typing import Literal, overload |
| 2 | + |
| 3 | +from shiny import App, Inputs, Outputs, Session, module, reactive, render, ui |
| 4 | + |
| 5 | +random_values: dict[str, list[str | float]] = { |
| 6 | + "word": [ |
| 7 | + "serendipity", |
| 8 | + "ephemeral", |
| 9 | + "mellifluous", |
| 10 | + "nebulous", |
| 11 | + "quintessential", |
| 12 | + "ethereal", |
| 13 | + "luminescent", |
| 14 | + "cascade", |
| 15 | + "zenith", |
| 16 | + "labyrinth", |
| 17 | + ], |
| 18 | + "sentence": [ |
| 19 | + "The old oak tree whispered secrets to the wind.", |
| 20 | + "Clouds painted shadows on the mountain peaks.", |
| 21 | + "Stars danced across the midnight canvas.", |
| 22 | + "Time flows like honey on a summer day.", |
| 23 | + "Music filled the empty spaces between thoughts.", |
| 24 | + ], |
| 25 | + "number": [ |
| 26 | + 42, |
| 27 | + 3.14159, |
| 28 | + 1729, |
| 29 | + 2.71828, |
| 30 | + 1.41421, |
| 31 | + 987654321, |
| 32 | + 123.456, |
| 33 | + 7.77777, |
| 34 | + 9999.99, |
| 35 | + 0.12345, |
| 36 | + ], |
| 37 | + "password": [ |
| 38 | + "Tr0ub4dor&3", |
| 39 | + "P@ssw0rd123!", |
| 40 | + "C0mpl3x1ty#", |
| 41 | + "S3cur3P@ss", |
| 42 | + "Str0ngP@55w0rd", |
| 43 | + "Un1qu3C0d3!", |
| 44 | + "K3yM@st3r99", |
| 45 | + "P@ssPhr@s3", |
| 46 | + ], |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +@overload |
| 51 | +def random_value(category: Literal["number"], index: int) -> float: ... |
| 52 | + |
| 53 | + |
| 54 | +@overload |
| 55 | +def random_value( |
| 56 | + category: Literal["word", "sentence", "password"], index: int |
| 57 | +) -> str: ... |
| 58 | + |
| 59 | + |
| 60 | +def random_value( |
| 61 | + category: Literal["word", "sentence", "number", "password"], index: int |
| 62 | +) -> str | float: |
| 63 | + selected_list = random_values[category] |
| 64 | + wrapped_index = (index - 1) % len(selected_list) |
| 65 | + return selected_list[wrapped_index] |
| 66 | + |
| 67 | + |
| 68 | +@module.ui |
| 69 | +def text_input_ui(update_on: Literal["change", "blur"] = "change"): |
| 70 | + return ui.TagList( |
| 71 | + ui.h2(f'updateOn="{update_on}"'), |
| 72 | + ui.input_text("txt", "Text", value="Hello", update_on=update_on), |
| 73 | + ui.input_text_area("txtarea", "Text Area", update_on=update_on), |
| 74 | + ui.input_numeric("num", "Numeric", value=1, update_on=update_on), |
| 75 | + ui.input_password("pwd", "Password", update_on=update_on), |
| 76 | + ui.output_text_verbatim("value"), |
| 77 | + ui.input_action_button("update_text", "Update Text"), |
| 78 | + ui.input_action_button("update_text_area", "Update Text Area"), |
| 79 | + ui.input_action_button("update_number", "Update Number"), |
| 80 | + ui.input_action_button("update_pwd", "Update Password"), |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +@module.server |
| 85 | +def text_input_server(input: Inputs, output: Outputs, session: Session): |
| 86 | + @render.text |
| 87 | + def value() -> str: |
| 88 | + return "\n".join( |
| 89 | + [ |
| 90 | + "---- Text ----", |
| 91 | + input.txt(), |
| 92 | + "---- Text Area ----", |
| 93 | + input.txtarea(), |
| 94 | + "---- Numeric ----", |
| 95 | + str(input.num()), |
| 96 | + "---- Password ----", |
| 97 | + input.pwd(), |
| 98 | + ] |
| 99 | + ) |
| 100 | + |
| 101 | + @reactive.effect |
| 102 | + @reactive.event(input.update_text) |
| 103 | + def _(): |
| 104 | + ui.update_text( |
| 105 | + "txt", |
| 106 | + value=" ".join( |
| 107 | + [random_value("word", input.update_text() + i) for i in range(2)] |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + @reactive.effect |
| 112 | + @reactive.event(input.update_text_area) |
| 113 | + def _(): |
| 114 | + ui.update_text_area( |
| 115 | + "txtarea", |
| 116 | + value="\n".join( |
| 117 | + [ |
| 118 | + random_value("sentence", input.update_text_area() + i) |
| 119 | + for i in range(2) |
| 120 | + ] |
| 121 | + ), |
| 122 | + ) |
| 123 | + |
| 124 | + @reactive.Effect |
| 125 | + @reactive.event(input.update_number) |
| 126 | + def _(): |
| 127 | + ui.update_numeric("num", value=random_value("number", input.update_number())) |
| 128 | + |
| 129 | + @reactive.Effect |
| 130 | + @reactive.event(input.update_pwd) |
| 131 | + def _(): |
| 132 | + ui.update_text("pwd", value=random_value("password", input.update_pwd())) |
| 133 | + |
| 134 | + |
| 135 | +app_ui = ui.page_fluid( |
| 136 | + ui.row( |
| 137 | + ui.column(6, ui.div({"class": "col-sm-12"}, text_input_ui("change"))), |
| 138 | + ui.column(6, ui.div({"class": "col-sm-12"}, text_input_ui("blur"))), |
| 139 | + ) |
| 140 | +) |
| 141 | + |
| 142 | + |
| 143 | +def server(input: Inputs): |
| 144 | + text_input_server("change") |
| 145 | + text_input_server("blur") |
| 146 | + |
| 147 | + |
| 148 | +app = App(app_ui, server) |
0 commit comments