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.2.tar.gz (92.3 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.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: streamlit_session_memo-0.4.2.tar.gz
  • Upload date:
  • Size: 92.3 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.2.tar.gz
Algorithm Hash digest
SHA256 764f64521d8beb395384eba5398e287a95b4429ce87370d15ed0108602b62a6e
MD5 5f7fd43ae152e8bb55b6d50371aa03b4
BLAKE2b-256 15576bb95c483497a81d558e352cbff72fa9d695a43497bdb5a0b768db597afb

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_session_memo-0.4.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_session_memo-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2b23134e6fe6af925bec753fc4dd6d95f0b00b9709aa872d4b217318c8a897eb
MD5 ffbb216ea7c11018122fded0c074df49
BLAKE2b-256 d128f45d7d1d939f937b14a5842db2174199e4bd6790d63acba1f6dc293f3ec7

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_session_memo-0.4.2-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

This release

0.4.2 This release

2 files

0.4.1

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