The variable crossing conditional boundaries gets reset

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.

  1. running my app locally
  2. 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 draw_estimate(self):
        inf_est = {
            'department': department,
            'name': name,
            'email': email,
            'est_no': est_no,
            'company': company,
            'customer': customer,
            'site': site,
            'subject': subject,
            'p_code': p_code,
            'p_model': p_model
            }

        submitted = st.form_submit_button("submit")
        if submitted:
            return inf_est

def product_1(self):

    if st.session_state['now'] == 0:
        buttons(st.session_state['now'])

    if st.session_state['now'] == 1:
        self.inf_est = self.draw_estimate()
        st.write("inf_est now=1:", self.inf_est)
        buttons(st.session_state['now'])
    elif st.session_state['now'] == 2:
        st.write("inf_est now=2 :", self.inf_est)
    else:
        reset()

def countup():
st.session_state[‘now’] += 1

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.

  1. running my app locally
  2. Streamlit version 1.35.0
    Python version 3.10.12

multipage2.py

class Multipage:
def __init__(self):
    """Initialize SessionState instance."""
    self.filtered_df2 = pd.DataFrame()
    self.filtered_df_op2 = pd.DataFrame()
    self.total_price = pd.DataFrame()
    self.inf_est = {}
    self.file_path = {}

multipage.py

from multipage2 import Multipage

class Multipage:
def handle_now_0(self):
    buttons(st.session_state['now'])

def handle_now_1(self):
    self.inf_est = self.draw_estimate()
    st.write("inf_est now1 :", self.inf_est)
    buttons(st.session_state['now'])

def handle_now_2(self):
    st.write("total_price now2 :", self.total_price)
    st.write("inf_est now2 :", self.inf_est)
    self.make()
    buttons(st.session_state['now'])
    
def product_1(self):
    result = self.product_search()
    self.filtered_df2 = result[0]
    self.filtered_df_op2 = result[1]
    self.total_price = result[2]
    if st.session_state['now'] == 0:
        self.handle_now_0()
    elif st.session_state['now'] == 1:
        self.handle_now_1()
    elif st.session_state['now'] == 2:
        self.handle_now_2()
    else:
        st.warning("Invalid value for 'now'. Resetting...")
        reset()

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.

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