Streamlit component that allows you to do manage local storage
Project description
Streamlit local storage
This repo is to help streamlt users manage data in a browser's local storage.
pip install streamlit-local-storage
Important!
# Need to initialise component like this. Unfortunately, cannot call items from local storage on command meaning if say an item in local storage were deleted and you try to call the same key/item it will return the previous value (as though the deleting the key never happened at least in a session). It will only reset when you get a new key/item or create a new session (refresh the browser). Hence need to cache the initialisation of the component because it calls/gets all the items saved in local storage. Then other methods like setItem or getItem are conducted on this class variable and also to the react component so that when a new session is created you can get the saved items and can use the values saved to the class variable in the current session.
from streamlit_local_storage import LocalStorage
@st.cache_data(experimental_allow_widgets=True) or @st.cache_resource(experimental_allow_widgets=True)
def LocalStorageManager():
return LocalStorage()
localS = LocalStorageManager()
store an array and individual items to local storage
localStorageArray = [
{"key":"Games", "toStore":[{"name":"Basically does this work"}]},
{"key":"Winners", "toStore":[{"name":"Basically does this work Though"}]}
]
localS.setList(localStorageArray)
itemKey = "camera"
itemValue = "Tarah"
localS.setItem(itemKey, itemValue)
get list of items and individual item from local storage
data_ = ["camera", "JamesLook"]
saved_ = localS.getList(data_)
itemKey = "Taylor Swift"
local_storage_item = localS.getItem(itemKey)
get all items saved on local storage
saved_individual = localS.getAll()
st.write(saved_individual)
delete item and item list from local storage
saved_individual = localS.deleteList(["ThomasKing", "Somethingelse"])
localS.deleteItem('Tony')
delete all
localS.deleteAll()
when getting local storage items with streamlit widgets
st.subheader("Method 1")
@st.cache_data(experimental_allow_widgets=True)
def LocalStorageManager():
return LocalStorage()
localS = LocalStorageManager()
if "get_val" not in st.session_state:
st.session_state["get_val"] = None
with st.form("get_data"):
st.text_input("key", key="get_local_storage_v")
st.form_submit_button("Submit")
if st.session_state["get_local_storage_v"] != "":
val_ = localS.getItem(st.session_state["get_local_storage_v"], key="test_get_item")
st.session_state["get_val"] = val_
st.write(st.session_state["get_val"])
st.subheader("Method 2 - using callback")
@st.cache_data(experimental_allow_widgets=True)
def LocalStorageManager():
return LocalStorage()
localS = LocalStorageManager()
if "get_val_2" not in st.session_state:
st.session_state["get_val_2"] = None
if "st_col_test" not in st.session_state:
st.session_state["st_col_test"] = None
def change_state_2():
with st.session_state["st_col_test"][0]: # call method from below form
localS.getItem(st.session_state["get_local_storage_v_2"], key="test_get_item_2") # initialise/run method and value will be stored in `session_state`
with st.form("get_data_2"):
st.text_input("key", key="get_local_storage_v_2")
st.form_submit_button("Submit", on_click=change_state_2)
st.session_state["st_col_test"] = st.columns(1) #to make sure rendering happens below form (or other streamlit element. Else the re-rendering of component upon re-running of up will be run at the top of the streamlit component (form here) which creates nasty ui experience.)
if "test_get_item_2" in st.session_state and st.session_state["test_get_item_2"] != None:
st.session_state["get_val_2"] = st.session_state["test_get_item_2"] # if local storage method is initialised, get the stored value and use it throughout the app.
st.write(st.session_state["get_val_2"])
Remember to refresh browser if it does not pop up instantly in local storage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
streamlit-local-storage-0.0.9.tar.gz
(445.3 kB
view hashes)
Built Distribution
Close
Hashes for streamlit-local-storage-0.0.9.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e37715b1248d1bbad26198b8776a110b9a6293cd73246020d5a1f4c7dda7c62 |
|
MD5 | aa3274c1870b88af061a6574b3261eb9 |
|
BLAKE2b-256 | c69989a3816ec823682205e37522e07a3d940cc8107a1da7d0941d4403c4b032 |
Close
Hashes for streamlit_local_storage-0.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca2356bb46f5ddc50d0f4a7c650c7443466c7b3d95e5efaa67cf21180f18e2c5 |
|
MD5 | 90bd96264fe6edf89129de4a7ebb4129 |
|
BLAKE2b-256 | 1412a5dbd790eca2460ac52473021af2868b347667a02aa5d78a27f69f559e34 |