Spaces:
Sleeping
Sleeping
nan-motherboard commited on
Commit ·
c8bcd58
1
Parent(s): a1b34b4
modify8
Browse files- SessionState.py +0 -117
- app.py +78 -67
SessionState.py
DELETED
|
@@ -1,117 +0,0 @@
|
|
| 1 |
-
"""Hack to add per-session state to Streamlit.
|
| 2 |
-
|
| 3 |
-
Usage
|
| 4 |
-
-----
|
| 5 |
-
|
| 6 |
-
>>> import SessionState
|
| 7 |
-
>>>
|
| 8 |
-
>>> session_state = SessionState.get(user_name='', favorite_color='black')
|
| 9 |
-
>>> session_state.user_name
|
| 10 |
-
''
|
| 11 |
-
>>> session_state.user_name = 'Mary'
|
| 12 |
-
>>> session_state.favorite_color
|
| 13 |
-
'black'
|
| 14 |
-
|
| 15 |
-
Since you set user_name above, next time your script runs this will be the
|
| 16 |
-
result:
|
| 17 |
-
>>> session_state = get(user_name='', favorite_color='black')
|
| 18 |
-
>>> session_state.user_name
|
| 19 |
-
'Mary'
|
| 20 |
-
|
| 21 |
-
"""
|
| 22 |
-
try:
|
| 23 |
-
import streamlit.ReportThread as ReportThread
|
| 24 |
-
from streamlit.server.Server import Server
|
| 25 |
-
except Exception:
|
| 26 |
-
# Streamlit >= 0.65.0
|
| 27 |
-
import streamlit.report_thread as ReportThread
|
| 28 |
-
from streamlit.server.server import Server
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
class SessionState(object):
|
| 32 |
-
def __init__(self, **kwargs):
|
| 33 |
-
"""A new SessionState object.
|
| 34 |
-
|
| 35 |
-
Parameters
|
| 36 |
-
----------
|
| 37 |
-
**kwargs : any
|
| 38 |
-
Default values for the session state.
|
| 39 |
-
|
| 40 |
-
Example
|
| 41 |
-
-------
|
| 42 |
-
>>> session_state = SessionState(user_name='', favorite_color='black')
|
| 43 |
-
>>> session_state.user_name = 'Mary'
|
| 44 |
-
''
|
| 45 |
-
>>> session_state.favorite_color
|
| 46 |
-
'black'
|
| 47 |
-
|
| 48 |
-
"""
|
| 49 |
-
for key, val in kwargs.items():
|
| 50 |
-
setattr(self, key, val)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def get(**kwargs):
|
| 54 |
-
"""Gets a SessionState object for the current session.
|
| 55 |
-
|
| 56 |
-
Creates a new object if necessary.
|
| 57 |
-
|
| 58 |
-
Parameters
|
| 59 |
-
----------
|
| 60 |
-
**kwargs : any
|
| 61 |
-
Default values you want to add to the session state, if we're creating a
|
| 62 |
-
new one.
|
| 63 |
-
|
| 64 |
-
Example
|
| 65 |
-
-------
|
| 66 |
-
>>> session_state = get(user_name='', favorite_color='black')
|
| 67 |
-
>>> session_state.user_name
|
| 68 |
-
''
|
| 69 |
-
>>> session_state.user_name = 'Mary'
|
| 70 |
-
>>> session_state.favorite_color
|
| 71 |
-
'black'
|
| 72 |
-
|
| 73 |
-
Since you set user_name above, next time your script runs this will be the
|
| 74 |
-
result:
|
| 75 |
-
>>> session_state = get(user_name='', favorite_color='black')
|
| 76 |
-
>>> session_state.user_name
|
| 77 |
-
'Mary'
|
| 78 |
-
|
| 79 |
-
"""
|
| 80 |
-
# Hack to get the session object from Streamlit.
|
| 81 |
-
|
| 82 |
-
ctx = ReportThread.get_report_ctx()
|
| 83 |
-
|
| 84 |
-
this_session = None
|
| 85 |
-
|
| 86 |
-
current_server = Server.get_current()
|
| 87 |
-
if hasattr(current_server, '_session_infos'):
|
| 88 |
-
# Streamlit < 0.56
|
| 89 |
-
session_infos = Server.get_current()._session_infos.values()
|
| 90 |
-
else:
|
| 91 |
-
session_infos = Server.get_current()._session_info_by_id.values()
|
| 92 |
-
|
| 93 |
-
for session_info in session_infos:
|
| 94 |
-
s = session_info.session
|
| 95 |
-
if (
|
| 96 |
-
# Streamlit < 0.54.0
|
| 97 |
-
(hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg)
|
| 98 |
-
or
|
| 99 |
-
# Streamlit >= 0.54.0
|
| 100 |
-
(not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue)
|
| 101 |
-
or
|
| 102 |
-
# Streamlit >= 0.65.2
|
| 103 |
-
(not hasattr(s, '_main_dg') and s._uploaded_file_mgr == ctx.uploaded_file_mgr)
|
| 104 |
-
):
|
| 105 |
-
this_session = s
|
| 106 |
-
|
| 107 |
-
if this_session is None:
|
| 108 |
-
raise RuntimeError(
|
| 109 |
-
"Oh noes. Couldn't get your Streamlit Session object. "
|
| 110 |
-
'Are you doing something fancy with threads?')
|
| 111 |
-
|
| 112 |
-
# Got the session object! Now let's attach some state into it.
|
| 113 |
-
|
| 114 |
-
if not hasattr(this_session, '_custom_session_state'):
|
| 115 |
-
this_session._custom_session_state = SessionState(**kwargs)
|
| 116 |
-
|
| 117 |
-
return this_session._custom_session_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,35 +1,44 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# with col2:
|
| 23 |
-
# clear = st.form_submit_button("Clear")
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
# with st.form("my_output"):
|
| 28 |
# if submitted:
|
| 29 |
-
# st.write("product", product)
|
| 30 |
-
# st.write("gender", gender)
|
| 31 |
-
# st.write("profession", profession)
|
| 32 |
-
# st.write("hobby", hobby)
|
| 33 |
# # Clear the user inputs
|
| 34 |
# if clear:
|
| 35 |
# st.experimental_rerun()
|
|
@@ -54,48 +63,50 @@
|
|
| 54 |
# st.write_stream(stream_data)
|
| 55 |
|
| 56 |
|
| 57 |
-
import streamlit as st
|
| 58 |
-
import SessionState
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
# Initialize session state
|
| 61 |
-
session_state = SessionState.get(product="", gender="", profession="", hobby="")
|
| 62 |
|
| 63 |
-
# Create a form for user inputs
|
| 64 |
-
with st.form("my_input"):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# Display the user inputs
|
| 80 |
-
with st.form("my_output"):
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
|
| 4 |
+
def form_callback():
|
| 5 |
+
st.write(st.session_state.product)
|
| 6 |
+
st.write(st.session_state.gender)
|
| 7 |
+
st.write(st.session_state.profession)
|
| 8 |
+
st.write(st.session_state.hobby)
|
| 9 |
+
|
| 10 |
+
with st.form("my_input"):
|
| 11 |
+
st.write("Input")
|
| 12 |
+
# product
|
| 13 |
+
product=st.text_input("product", key="product")
|
| 14 |
|
| 15 |
+
# gender
|
| 16 |
+
gender=st.radio("gender", ["male", "female"], key="gender")
|
| 17 |
|
| 18 |
+
# profession
|
| 19 |
+
profession=st.text_input("profession", key="profession")
|
| 20 |
+
|
| 21 |
+
# hobby
|
| 22 |
+
hobby=st.text_input("hobby", key="hobby")
|
| 23 |
+
|
| 24 |
+
# Every form must have a submit button.
|
| 25 |
+
col1, col2=st.columns(2)
|
| 26 |
+
|
| 27 |
+
# Place a button in each column
|
| 28 |
+
with col1:
|
| 29 |
+
submitted = st.form_submit_button(label='Submit', on_click=form_callback)
|
| 30 |
+
|
| 31 |
+
with col2:
|
| 32 |
+
clear = st.form_submit_button("Clear")
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
|
| 36 |
# with st.form("my_output"):
|
| 37 |
# if submitted:
|
| 38 |
+
# st.write("product", st.session_state.product)
|
| 39 |
+
# st.write("gender", st.session_state.gender)
|
| 40 |
+
# st.write("profession", st.session_state.profession)
|
| 41 |
+
# st.write("hobby", st.session_state.hobby)
|
| 42 |
# # Clear the user inputs
|
| 43 |
# if clear:
|
| 44 |
# st.experimental_rerun()
|
|
|
|
| 63 |
# st.write_stream(stream_data)
|
| 64 |
|
| 65 |
|
| 66 |
+
# import streamlit as st
|
| 67 |
+
# import SessionState
|
| 68 |
+
# from streamlit.server.server import Server
|
| 69 |
+
# import streamlit.report_thread as ReportThread
|
| 70 |
|
| 71 |
+
# # Initialize session state
|
| 72 |
+
# session_state = SessionState.get(product="", gender="", profession="", hobby="")
|
| 73 |
|
| 74 |
+
# # Create a form for user inputs
|
| 75 |
+
# with st.form("my_input"):
|
| 76 |
+
# st.write("Input")
|
| 77 |
+
# # product
|
| 78 |
+
# product = st.text_input("product", value=session_state.product)
|
| 79 |
+
# # gender
|
| 80 |
+
# gender = st.radio("gender", ["male", "female"], index=["male", "female"].index(session_state.gender) if session_state.gender else 0)
|
| 81 |
+
# # profession
|
| 82 |
+
# profession = st.text_input("profession", value=session_state.profession)
|
| 83 |
+
# # hobby
|
| 84 |
+
# hobby = st.text_input("hobby", value=session_state.hobby)
|
| 85 |
+
|
| 86 |
+
# # Every form must have a submit button.
|
| 87 |
+
# submitted = st.form_submit_button("Submit")
|
| 88 |
+
# clear = st.form_submit_button("Clear")
|
| 89 |
+
|
| 90 |
+
# # Display the user inputs
|
| 91 |
+
# with st.form("my_output"):
|
| 92 |
+
# if submitted and not clear:
|
| 93 |
+
# # Save inputs to session state
|
| 94 |
+
# session_state.product = product
|
| 95 |
+
# session_state.gender = gender
|
| 96 |
+
# session_state.profession = profession
|
| 97 |
+
# session_state.hobby = hobby
|
| 98 |
+
|
| 99 |
+
# st.write("product", product)
|
| 100 |
+
# st.write("gender", gender)
|
| 101 |
+
# st.write("profession", profession)
|
| 102 |
+
# st.write("hobby", hobby)
|
| 103 |
+
|
| 104 |
+
# # Clear the user inputs
|
| 105 |
+
# if clear:
|
| 106 |
+
# # Clear session state
|
| 107 |
+
# session_state.product = ""
|
| 108 |
+
# session_state.gender = ""
|
| 109 |
+
# session_state.profession = ""
|
| 110 |
+
# session_state.hobby = ""
|
| 111 |
+
|
| 112 |
+
# st.experimental_rerun()
|