Session_state

May I know if there any way to prevent system to reset session state.
The web page reset session_state to default state when I click button.

Can you provide some code? Or give more detail?

Thanks for replying me! When I click “st.button(“Plot Graph”)”, the program backs to st.session_state[“count”] = 0

st.session_state[“count”] = 0

if st.session_state[“count”] == 0:
ac=st.empty()
pw=st.empty()
btn=st.empty()

text1 = ac.text_input("Username")
text2 = pw.text_input("Password", type="password")
isclick = btn.button("Login")
if isclick and text1=="admin" and text2=="password":
    ac.empty()
    pw.empty()
    btn.empty()
    st.session_state["count"] = 1

if st.session_state[“count”] == 1:
st.markdown(“Welcome!”)
st.button(“Plot Graph”)

Thank you!

1 Like

Hi @gegreg ,

This is because of this first line ,
(the app reruns on your Plot Graph button click, and st.session_state.count reassigns to 0)

Try initialising session state as,

if 'count' not in st.session_state:
     st.session_state.count = 0

However, If I see your code further, there will be further errors, so please debug it accordingly.
Hope this helps.

Best,
Avra

1 Like

Hi @gegreg

I’ve reformatted your code as follows:

import streamlit as st

st.session_state["count"] = 0

if st.session_state["count"] == 0:
    ac=st.empty()
    pw=st.empty()
    btn=st.empty()

    text1 = ac.text_input("Username")
    text2 = pw.text_input("Password", type="password")
    isclick = btn.button("Login")

if isclick and text1=="admin" and text2=="password":
    ac.empty()
    pw.empty()
    btn.empty()
    st.session_state["count"] = 1

if st.session_state["count"] == 1:
    st.markdown("Welcome!")
    if st.button("Plot Graph"):
        st.write('Show plot here ...')

And I could reproduce your error where clicking on the Plot Graph button resets the app.

Solution
Thanks to my colleague @Marisa_Smith for discussion and the above suggestion from @AvratanuBiswas, here’s a possible solution:

import streamlit as st
import pandas as pd
import numpy as np

if "submitted" not in st.session_state:
    st.session_state.submitted = False

text1 = st.text_input("Username")
text2 = st.text_input("Password", type="password")

if (st.button("Login") or st.session_state.submitted) and text1=="admin" and text2=="password":
    st.session_state.submitted = True
    st.success("Welcome!")

    if st.button("Plot Graph"):
        st.session_state.submitted = False
        chart_data = pd.DataFrame(
                np.random.randn(20, 3),
                columns=['a', 'b', 'c'])
        st.area_chart(chart_data)

Please note that in the above example, the Username and Password are explicitly mentioned in the code which is not recommended. You can refer to the use of Secrets Management.

You can try out the Deployed app here:
Streamlit App

The GitHub repo is available here.

Hope this helps :smiley:

2 Likes

Hi everyone, thanks for spending so much time for my issue : )
I am thankful for it!

2 Likes

Hi DataProfessor,

Thanks for replying me : ) Actually I enjoyed watching your video very much and it’s my first video to learn Streamlit.

However, I faced another issue. I put a st.sidebar_date_input under the if (st.button(“Login”) or st.session_state.submitted) and text1==“admin” and text2==“password”:, the webpage will refresh occasionally when I try to input words on the date_input field.

Would you mind giving me some hints and thanks for your time!

1 Like

Hi @gegreg,

Glad to hear that.

I would recommend to use the st.form() function so that the results would not be reactive and update at all times when input widgets are adjusted. Particularly, users can make all the necessary adjustments to input widgets and the app will not update unless the user clicks on the Submit button.

Links:

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