-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_app.py
97 lines (71 loc) · 2.88 KB
/
streamlit_app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import datetime
import random
import time
import numpy as np
import pandas as pd
import streamlit as st
np.random.seed(0)
random.seed(0)
chart_data_1 = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])
chart_data_2 = pd.DataFrame(
np.random.randn(50, 4), columns=["col 1", "col 2", "col 3", "col 4"]
)
if "reruns" not in st.session_state:
st.session_state.reruns = 0
st.session_state.reruns += 1
st.button("Button in main script")
with st.spinner("Doing some heavy work in the main script..."):
time.sleep(3)
@st.partial
def interactive_form():
st.caption(
"Interacting with any of the elements in this partial will only rerun the code of this partial."
)
# Changes to the selectbox only "reruns" the group function and its widgets
option = st.selectbox("How would you like to be contacted?", ("Email", "Phone"))
contact_info = st.text_input(f"Enter {option}")
if st.button("Submit"):
st.success(f"Contact data ({option}: {contact_info}) submitted successfully!")
@st.partial
def chart_filtering(data: pd.DataFrame):
st.caption(
"Interacting with any of the elements in this partial will only rerun the code of this partial."
)
# filter chart_data columns via a multiselect widget:
columns = st.multiselect("Select columns", data.columns.tolist())
st.line_chart(data if not columns else data[columns])
@st.partial(run_every=2)
def stream_data():
rng = np.random.default_rng()
chart_data = pd.DataFrame(rng.random((20, 3)), columns=["a", "b", "c"])
columns = st.multiselect("Select columns", chart_data.columns.tolist(), key="foo")
st.bar_chart(chart_data if not columns else chart_data[columns])
st.caption(f"Last updated {datetime.datetime.now()}")
# cache_data doesn't seem to work right now resulting in: TypeError: cannot pickle '_thread.RLock' object
@st.cache_resource
def transform_data():
return chart_data_1.to_csv().encode("utf-8")
@st.partial
def improved_download_button():
st.download_button(
label="Download data as CSV",
data=transform_data(),
file_name="large_df.csv",
mime="text/csv",
)
with st.expander("Chart filtering 1 with partial rerun", expanded=True):
with st.spinner("Crunching data..."):
time.sleep(1)
chart_filtering(chart_data_1)
with st.expander("Chart filtering 2 with partial rerun", expanded=True):
with st.spinner("Crunching data..."):
time.sleep(1)
chart_filtering(chart_data_2)
with st.expander("Interactive form with partial rerun", expanded=True):
interactive_form()
with st.expander("Streaming chart with partial rerun", expanded=True):
stream_data()
# with st.expander("Download button that doesn't require full rerun", expanded=True):
# improved_download_button()
other_input = st.text_input(f"Input in main script")
st.write(f"Reruns of main script: {st.session_state.reruns}")