Skip to main content

streamlit-session-memo

A decorator for session-specific caching on Streamlit.

from streamlit_session_memo import st_session_memo


@st_session_memo
def load_big_model():
    ...
    return model


model = load_big_model()

This is a simple wrapper around st.session_state that caches the return value of the decorated function to the session state and returns the cached value if the function is called again with the same arguments. We have been doing this manually with code like the following, but this decorator makes it simpler.

# Boilerplate code for session-specific caching.
cache_key = f"{arg_1}_{arg_2}_{arg_3}"
if cache_key in st.session_state:
    model = st.session_state[cache_key]
else:
    result = load_expensive_model(arg_1, arg_2, arg_3)
    st.session_state[cache_key] = result
    model = result

Arguments are hashed the way st.cache_data and st.cache_resource hash theirs, by delegating to Streamlit's own key builder: values such as dicts and DataFrames are keyed by content, passing an argument positionally or by keyword gives the same key, a parameter whose name starts with an underscore is left out of the key, and an argument of a type Streamlit cannot hash raises UnhashableParamError. That exception is Streamlit's own, so its message suggests @st.cache_resource; the underscore it advises works the same way here.

Note that, this decorator is a lightweight wrapper around st.session_state that acts like the code snippet above, and does not provide any additional features such as mutation guards that st.cache_data provides.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_session_memo-0.4.1.tar.gz (92.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_session_memo-0.4.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_session_memo-0.4.1.tar.gz.

File metadata

  • Download URL: streamlit_session_memo-0.4.1.tar.gz
  • Upload date:
  • Size: 92.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for streamlit_session_memo-0.4.1.tar.gz
Algorithm Hash digest
SHA256 cd5e58cb9c4ee3fd0cddfb635dcce29db070ce1f047da15229c48f55ae7ac93e
MD5 9cfad8130e32c1f18aa63fda91033781
BLAKE2b-256 2008eea5e047c2ef7e1ffaf3e5e364c0f6b65e9e8d9a242d913cc57093e51980

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_session_memo-0.4.1.tar.gz:

Publisher: main.yml on whitphx/streamlit-session-memo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file streamlit_session_memo-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_session_memo-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 db2eada07a846666be1c7d4bbf57a7da7ddbf54aabfaf685a0e64031f1f1bf6b
MD5 ba3fdfb4cfe57e72bd16b756f1869117
BLAKE2b-256 2810ebb8dad06b62ac857fb22ea734ad993b33dfdf61a09c57594e7e1f7e6085

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_session_memo-0.4.1-py3-none-any.whl:

Publisher: main.yml on whitphx/streamlit-session-memo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.5.0

2 files

0.4.2

2 files

This release

0.4.1 This release

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page