You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to store some data in local storage with javascript. But there is some problem - because getitem method always return 0.
Here is sample code:
import random
def rand_str():
s = [chr(random.randint(ord('a'), ord('z'))) for _ in range(30)]
return ''.join(s)
import streamlit as st
from streamlit_javascript import st_javascript
st.subheader("Javascript API call")
b1 = st.button('read')
if b1:
# j_code = "console.log(localStorage.getItem('local_sandbox'));"
j_code = "localStorage.getItem('local_sandbox');"
v = st_javascript(j_code)
st.session_state['text'] = f"Read from storage: {v}"
b2 = st.button('write')
if b2:
j_code = f"""localStorage.setItem('local_sandbox', '{rand_str()}');"""
v = st_javascript(j_code)
st.session_state['text'] = f"Write to storage: {v}"
st.json(st.session_state)
print(f"{st.session_state.get('text')}")
Here is stdout: (1 press write, 2 press read, 1 write again, 2 read again)
Write to storage: 0
Read from storage: 0
Read from storage: 0
Write to storage: 0
Read from storage: 0
Read from storage: 0
in browser i can see that variable created - but getitem always return 0
if use string with console.log( - then in browser log shows correct values from storage
What is the reason of such behaviour?
The text was updated successfully, but these errors were encountered:
rajanant
changed the title
Set not always work with Local storage
Getitem not always work with Local storage
Aug 31, 2022
I want to store some data in local storage with javascript. But there is some problem - because getitem method always return 0.
Here is sample code:
Here is stdout: (1 press write, 2 press read, 1 write again, 2 read again)
in browser i can see that variable created - but getitem always return 0
if use string with
console.log(
- then in browser log shows correct values from storageWhat is the reason of such behaviour?
The text was updated successfully, but these errors were encountered: