Skip to content

βœ‰οΈΒ Β Local Storage

Submitted by Joshua Carroll

Summary

Use browser localStorage across a user's sessions with a dict-like API.

Functions

local_storage

Create a dict-like store backed by browser localStorage.

Key, value pairs stored in local_storage will persist across user sessions in the same browser. See: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

This extra is built on streamlit-js: https://pypi.org/project/streamlit-js/

NOTE: Storing data on an app viewer's machine may have privacy and compliance implications for your app. Be sure to take that into account with any usage.

Source code in src/streamlit_extras/local_storage/__init__.py
@extra
def local_storage() -> StLocalStorage:
    """
    Create a dict-like store backed by browser localStorage.

    Key, value pairs stored in local_storage will persist across user sessions in the
    same browser. See:
    https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

    This extra is built on streamlit-js:
    https://pypi.org/project/streamlit-js/

    **NOTE:** Storing data on an app viewer's machine may have privacy and compliance
    implications for your app. Be sure to take that into account with any usage.
    """

    return StLocalStorage()

Import:

from streamlit_extras.local_storage import local_storage # (1)!
  1. You should add this to the top of your .py file πŸ› 

Examples

example

def example():
    st.title("local_storage basic example")

    "Any values you save will be available after leaving / refreshing the tab"

    ls = local_storage()

    key = st.text_input("Key")
    value = st.text_input("Value")
    if st.button("Save"):
        ls[key] = value

    if st.button("Delete"):
        del ls[key]

    if key:
        f"Current value of {key} is:"
        st.write(ls[key])
Output (beta)