Cope 2.5.0
My personal "standard library" of all the generally useful code I've written for various projects over the years
Loading...
Searching...
No Matches
Cope.streamlit.SS Class Reference

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()
 

Detailed Description

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:

  • Variables are specified once with defaults, and can be reset and maintained across pages via the reset() and maintain_state() methods, respectively. You can use square brackets or the . operator to access and set variables, and it won't throw an error if it doesn't already exist, instead it will return None.
  • *_changed (named {varname}_changed) gets updated automatically when the value gets set. The *_changed variable will not get reset upon the script rerunning though, unless you call reset_changed() at the end of the script. It will also not detect if the value was changed by widgets. To check if any variables were changed at all, use the check_changed() method. Note that *_changed doesn't ever get reset to False, unless a variable is manually set to the same value it already has. To reset all the variables, call ss.reset_changed(). This is usually done at the end of a file and/or before any st.rerun() calls.
  • 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.
  • maintain_state() will keep all the variables the same across pages. If arguements are passed, it only maintains those. kwargs passed sets the given variables to those values.
  • The 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".
  • Variables you want to keep track of via query parameters (so they get preserved between page reloads, or for sharing URLs with people), you can do that by either specifying names of the variables positionally to setup(), or by calling the add_query() method. The setup() and add_query() methods will automatically load them on page startup, or you can load them yourself on first run using just_loaded. Current Limitations:
  • Query params will not stay maintained across pages, if the varibles change between pages.
  • Using containers with query params behaves oddly sometimes. You may have to parse them yourself using eval().
  • TODO: auto-setting the page config on all pages

Definition at line 16 of file streamlit.py.


The documentation for this class was generated from the following file: