Initialize session state not working

Summary

I am trying to run this simple code to test the session state, but every time I run it I get the same error (see below). I have read already about several issues like this, but the solutions do not suit to me

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “c:\Users\thomas\Desktop\python_codes\regenschreiber_frontend\teste_render.py”, line 12, in
st.write('Count = ', st.session_state.count)
File “C:\Users\thomas\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state_proxy.py”, line 121, in getattr
raise AttributeError(_missing_attr_error_message(key))
AttributeError: st.session_state has no attribute “count”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

Steps to reproduce

Code snippet:

import streamlit as st

st.title("Counter Example")

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

increment = st.button("Increment")
if increment:
    st.session_state.count += 1

st.write('Count = ', st.session_state.count)

Expected behavior:

I expect that session state would be initialized

Actual behavior:

Session state not initialized

Debug info

  • Streamlit version: 1.31.0
  • Python version: 3.10.11
  • Using Conda? Conda, PipEnv
  • OS version: Windows
  • Browser version: Firefox

Requirements file

streamlit==1.31.0

Hi @bruno_castro93

Using the if conditional with st.button would allow showing a temporary message that may be reset upon click.

In your case, you may want to use a callback function via the on_click parameter with st.button

Consider the following code snippet example from Button behavior and examples - Streamlit Docs

import streamlit as st

if ‘clicked’ not in st.session_state:
st.session_state.clicked = False

def click_button():
st.session_state.clicked = True

st.button(‘Click me’, on_click=click_button)

if st.session_state.clicked:
# The message and nested widget will remain on the page
st.write(‘Button clicked!’)
st.slider(‘Select a value’)

Thank you for the fast reply, but even with your code, the app does not initialize. See the error below:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “c:\Users\thomas\Desktop\python_codes\regenschreiber_frontend\session.py”, line 11, in
if st.session_state.clicked:
File “C:\Users\thomas\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state_proxy.py”, line 121, in getattr
raise AttributeError(_missing_attr_error_message(key))
AttributeError: st.session_state has no attribute “clicked”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs
PS C:\Users\thomas\Desktop\python_codes\regenschreiber_frontend>

What do you mean by that? Your code seems to work for me. Count = 0 appears and clicking the button increases the counter.

I mean, I get this message in my terminal and the app does not run

AttributeError: st.session_state has no attribute “clicked”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

That is weird. I copied ans pasted your code and it just works here.

¿Can you reproduce the issue in streamlit cloud?

the app works in the streamlit cloud, but it does not work locally :frowning:

I have no idea why this happens. The code in the OP is as good as it goes, the problem must lie somewhere else.