I can’t get the following code to work correctly no matter what I try. Could someone who knows please help me?
When st.session_state['now'] == 1, a variable is assigned to inf_est, and when st.session_state['now'] == 2, the condition is met. However, calling inf_est within this condition results in an empty dictionary.
running my app locally
Streamlit version 1.35.0
Python version 3.10.12
import streamlit as st
import pandas as pd
class SessionState:
def init(self):
self.inf_est = {}
def countdown():
if st.session_state[‘now’] > 0:
st.session_state[‘now’] -= 1
def reset():
st.session_state[‘now’] = 0
def buttons(now):
col1, col2, col3 = st.columns(3)
if now < 3:
col1.button(‘next’, on_click=countup)
if now > 0:
col2.button(‘back’, on_click=countdown)
if now > 1:
col3.button(‘from the biggining’, on_click=reset)
Can you edit your post to format your code into a code block so we can see the correct whitespace? Also, how are you calling these class methods?
One thing that you should be aware of is how classes work with Streamlit’s rerun structure. Since you are saving some information in (Streamlit) Session State and some information as an attribute to your custom class, I can imagine a way things might get out of sync.
Thank you for reaching out. I’ve read the materials, and as per Pattern1, I separated only the class’s init into a different file and ran it, but it resulted in similar behavior. What should I do next?
inf_est now1: is displayed, but inf_est now2: is not displayed.
I still don’t know how you’re calling or using these methods.
As written, you are overwriting the imported definition of Multipage with a new class that doesn’t have an __init__ defined. I’m not sure what you’re trying to do here. Please can you show how you are using this class?
Even more fundamentally, it might help to describe what you are trying to do with this custom class. Why do you need the class instead of just working directly with Session State. I think there is a risk of unexpected behavior from using a class where some information is stored in the class and other information is stored directly in Session State.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.