awacke1 commited on
Commit
4c7af4b
·
1 Parent(s): ec740b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -1,25 +1,28 @@
1
-
2
  import streamlit as st
3
 
4
- def query_params():
5
- st.write("""
6
- # Introducing Query Params
7
- We have added to our experimental namespace the ability to get and set
8
- query parameters. With these query params, you can bookmark or share your app
9
- in various states. Thanks [@zhaoooyue](https://github.com/zhaoooyue) for the
10
- contribution!
11
- """)
12
- with st.echo("below"):
13
- radio_list = ['Eat', 'Sleep', 'Both']
14
- query_params = st.experimental_get_query_params()
 
 
 
 
 
 
 
 
 
15
 
16
- # Query parameters are returned as a list to support multiselect.
17
- # Get the first item in the list if the query parameter exists.
18
- default = int(query_params["activity"][0]) if "activity" in query_params else 0
19
- activity = st.radio(
20
- "What are you doing at home during quarantine?",
21
- radio_list,
22
- index = default
23
- )
24
- if activity:
25
- st.experimental_set_query_params(activity=radio_list.index(activity))
 
 
1
  import streamlit as st
2
 
3
+ # query params exist
4
+ try:
5
+ options = ['cat', 'dog', 'mouse', 'bat', 'duck']
6
+
7
+ query_params = st.experimental_get_query_params()
8
+ query_option = query_params['option'][0] #throws an exception when visiting http://host:port
9
+
10
+ option_selected = st.sidebar.selectbox('Pick option',
11
+ options,
12
+ index=options.index(query_option))
13
+ if option_selected:
14
+ st.experimental_set_query_params(option=option_selected)
15
+
16
+ # run when query params don't exist. e.g on first launch
17
+ except: # catch exception and set query param to predefined value
18
+ options = ['cat', 'dog', 'mouse', 'bat', 'duck']
19
+ st.experimental_set_query_params(option=options[1]) # defaults to dog
20
+
21
+ query_params = st.experimental_get_query_params()
22
+ query_option = query_params['option'][0]
23
 
24
+ option_selected = st.sidebar.selectbox('Pick option',
25
+ options,
26
+ index=options.index(query_option))
27
+ if option_selected:
28
+ st.experimental_set_query_params(option=option_selected)