aemin commited on
Commit
47c7f9e
1 Parent(s): 2997866

Upload SessionState.py

Browse files
Files changed (1) hide show
  1. SessionState.py +117 -0
SessionState.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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