Cope 2.5.0
My personal "standard library" of all the generally useful code I've written for various projects over the years
|
A better streamlit.session_state interface. More...
Public Member Functions | |
def | __init__ (self) |
The constructor. | |
def | __setitem__ (self, str name, value) |
Handles setting the value, the {var}_changes, and the prev{var} variables. | |
def | __setattr__ (self, str name, value) |
Shadows setitem() | |
def | __getitem__ (self, str name) |
Handles getting variables from either session_state or query parameters. | |
def | __getattr__ (self, str name) |
shadows getitem() | |
def | __delitem__ (self, str name) |
def | __delattr__ (self, str name) |
def | __contains__ (self, str name) |
def | setup (self, *queries, **vars) |
Pass default values of all the variables you're using. | |
def | ensure_exist (self) |
Ensures that all the variables we're keeping track of exist in st.session_state, at least with default values. | |
def | update (self, file) |
Maintain variable states across files, and notes the page we're in. | |
def | check_changed (self) |
Checks if any variables have been changed by widgets, and if so, updates their associated *_changed variable. | |
def | maintain_state (self, *args, exclude=[], calls=1, **kwargs) |
Maintain the current variables with the values they currently have across pages. | |
def | note_page (self, file, calls=1) |
Manually take note of what page we're running from. | |
def | reset (self) |
Reset all variables to their defaults. | |
def | reset_changed (self) |
Sets all the *_changed variables to False. | |
def | get (self, name) |
Available just for compatibility. | |
def | watch (self, str name, default) |
Sets a single variable to be maintained and guarenteed to exist in st.session_state. | |
def | add_query (self, *names) |
Adds variables to be monitored with query parameters as well. | |
Static Public Attributes | |
bool | maintain = True |
Protected Member Functions | |
def | _set_query_param (self, name, to) |
Handles how we serialize the _query params. | |
def | _get_query_param (self, name) |
Handles how we deserialize the query params TODO: this only evaluates the first level of containers. | |
Static Protected Attributes | |
dict | _dict = {} |
set | _query = set() |
A better streamlit.session_state interface.
This class is accessable via it's only instance, ss
. Do not instantiate this class directly. You can use ss
as a drop-in replacement for st.session_state, and it will work. Features:
current_page
and page_changed
variables store the filepath of the page we're currently in, and if the page was just changed, respectively. In order for the pages to work, you have to call either , update(), maintain_state() or note_page() at the top of each page in your project that you want variables to be maintained in.just_loaded
variable gets set to True
only if it's the first time the page is loaded. All other times it's set to False
. Resetting the page counts as loading the page for "the first time".just_loaded
. Current Limitations:Definition at line 16 of file streamlit.py.