Change in session storage is not reflected with st_javascript

Hello, I am using st_javascript in order to set, get or remove items in session storage.
Here I have created a page to showcase the problem I am having.


I have created 2 buttons one will set a session storage and one will clear it and below I have used get to print the session storage.
Now when I click set Item

As we can see a new item is now in the session storage but it is not reflected in the get and even after rerun it still doesnโ€™t shows anything.
But when I reload the page from the top left

It start showing it.
Same goes when I remove item and rerun it stays there but when reload the page only then it updates.

How can I solve this as I canโ€™t use it like this.

Code snippet:

import streamlit as st
from streamlit_javascript import st_javascript

jdata = '''{"type": "AdaptiveCard","$schema": "http://adaptivecards.io/schemas/adaptive-card.json","version": "1.6","body": []}'''
if st.button("setitem"):
    st_javascript(
        f"sessionStorage.setItem('test', JSON.stringify({jdata}));"
    )
if st.button("removeitem"):
    st_javascript(
        f"sessionStorage.clear();"
    )

st.write(st_javascript(
    f"JSON.parse(sessionStorage.getItem('test'));"
))

Can anyone help I am trying to get data from session storage?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.