forked from Wauplin/streamlit-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy_example.py
70 lines (50 loc) · 2.17 KB
/
toy_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import streamlit as st
import streamlit_sync
# Cache dir is optional. If None is provided, sessions are synced in memory.
CACHE_DIR = "./.st_sync_cache"
st.sidebar.write(
"Example app using streamlit-sync library. "
"For more details, please visit https://github.com/Wauplin/streamlit-sync"
)
# This step is optional. You can also use a default room name common to all sessions..
room_name = streamlit_sync.select_room_widget(cache_dir=CACHE_DIR)
with streamlit_sync.sync(room_name=room_name, cache_dir=CACHE_DIR):
# Sliders
# Toy example from https://github.com/streamlit/streamlit#a-little-example
st.header("Sliders")
st.info(
'Sliders, as any other "normal" widgets, can have their value synced with '
"other sessions. It is also possible to explicitly not sync a widget."
)
st.subheader("Synced slider")
y = st.slider("Select a value 1")
st.write(y, "squared is", y * y)
st.subheader("Synced slider using custom key widget.")
x = st.slider("Select a value", key="key")
st.write(x, "squared is", x * x)
st.subheader("Not synced slider")
x = st.slider("Select a value", key=streamlit_sync.get_not_synced_key("key"))
st.write(x, "squared is", x * x)
# Button
st.header("Buttons")
st.info(
"Button action is never synced to avoid duplicate actions. "
"However, if a value is updated in the session state, this update is synced "
"with other sessions."
)
if st.session_state.get("NB_CLICKS") is None:
st.session_state["NB_CLICKS"] = 0
if st.button(label="click"):
st.session_state["NB_CLICKS"] += 1
st.write("Clicked **" + str(st.session_state["NB_CLICKS"]) + "** times !")
# Form
st.header("Form")
st.info("Form data is synced only when submit button is clicked.")
if st.session_state.get("guess") is None:
st.session_state["guess"] = ""
with st.form(key="my_form", clear_on_submit=True):
answer = st.text_input("Make a guess")
submit = st.form_submit_button(label="Submit")
if submit:
st.session_state["guess"] = answer
st.write("Your guess: **" + str(st.session_state["guess"]) + "**")