I just found out about Streamlit recently.
This framework is exactly what I have wanted.
I love the new multipage feature.
But, I’m stumbling sharing session_state between pages.
When jump to each page from the sidebar menu,
The value of session_state is preserved.
But,if I jump to each page from the link set in HTML with st.markdown,
The value of session_state is not preserved (it seems clear).
Is there a difference between the link in the sidebar menu and the link set in HTML st.markdown?
Is session_state used incorrectly?
Please give me advice.
Home.py
import streamlit as st
print('Home 1')
print(st.session_state)
st.session_state.update(st.session_state)
if "shared" not in st.session_state:
st.session_state.shared = True
if 'products' not in st.session_state:
st.session_state.products = ['None']
print('Home 2')
print(st.session_state)
st.title("Home")
st.sidebar.header('Home')
print('Home 3')
print(st.session_state)
st.markdown('<a href="http://localhost:8501/Page2" target="_self">Page2</a>', unsafe_allow_html=True)
st.markdown('<a href="http://localhost:8501/Page3" target="_self">Page3</a>', unsafe_allow_html=True)
Page1.py
import streamlit as st
print('Page1 1')
print(st.session_state)
#st.session_state.update(st.session_state)
print('Page1 2')
print(st.session_state)
if "shared" not in st.session_state:
st.session_state.shared = True
if 'products' not in st.session_state:
st.session_state.products = ['None']
print('Page1 3')
print(st.session_state)
st.title("Page1")
st.sidebar.header('Page1')
st.session_state.products = ['Apple','Grape','Lemon','Orange']
print('Page1 4')
print(st.session_state)
Page2.py
import streamlit as st
print('Page2 1')
print(st.session_state)
#st.session_state.update(st.session_state)
print('Page2 2')
print(st.session_state)
if "shared" not in st.session_state:
st.session_state.shared = True
if 'products' not in st.session_state:
st.session_state.products = ['None']
print('Page2 3')
print(st.session_state)
st.title("Page2")
st.sidebar.header('Page2')
print('Page2 4')
print(st.session_state)
Page3.py
import streamlit as st
print('Page3 1')
print(st.session_state)
#st.session_state.update(st.session_state)
print('Page3 2')
print(st.session_state)
if "shared" not in st.session_state:
st.session_state.shared = True
if 'products' not in st.session_state:
st.session_state.products = ['None']
print('Page3 3')
print(st.session_state)
st.title("Page3")
st.sidebar.header('Page3')
print('Page3 ')
print(st.session_state)
##Console output
– Home -----------
Home 1
{}
#initialize session_state
Home 2
{‘shared’: True, ‘products’: [‘None’]}
Home 3
{‘shared’: True, ‘products’: [‘None’]}
– Jump to Page1 from Sidebarmenu -----------
– Page1 -----------
#find shared in session_state
Page1 1
{‘shared’: True, ‘products’: [‘None’]}
Page1 2
{‘shared’: True, ‘products’: [‘None’]}
Page1 3
{‘shared’: True, ‘products’: [‘None’]}
#Set products to session_state
Page1 4
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
– Jump to Page2 from Sidebarmenu -----------
– Page2 -----------
#find products in session_state
Page2 1
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
Page2 2
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
Page2 3
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
Page2 4
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
– Jump to Page3 from Sidebarmenu -----------
– Page3 -----------
#find products in session_state
Page3 1
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
Page3 2
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
Page3 3
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
Page3
{‘shared’: True, ‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’]}
– Jump to Home from Sidebarmenu -----------
– Home -----------
#find products in session_state
Home 1
{‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’], ‘shared’: True}
Home 2
{‘products’: [‘Apple’, ‘Grape’, ‘Lemon’, ‘Orange’], ‘shared’: True}
– Jump to Page2 from created link -----------
– Page2 -----------
#empty session_state
Page2 1
{}
Page2 2
{}
#initialize session_state
Page2 3
{‘shared’: True, ‘products’: [‘None’]}
Page2 4
{‘shared’: True, ‘products’: [‘None’]}