Coding st.session_state inside st.sidebar

Hi! How do you put this code in a st.sidebar? I got it from another thread and found it better suited for my purpose. When you write with sidebar(): before it an error occurs. I think this st.columns comes out, doesn’t it?

def changevalue(category):
    st.session_state.df = st.session_state.df[st.session_state.df[category]==st.session_state[category]]
    
cols = st.columns[4]
product = cols[0].st.selectbox("PRODUCT", st.session_state.df.Product.unique(), key="Product", on_change=changevalue, kwargs={'category': "Product"})
region  = cols[1].st.selectbox("REGION", st.session_state.df.Region.unique(), key="Region" , on_change=changevalue, kwargs={'category': "Region"})
state = cols[2].st.selectbox("STATE", st.session_state.df.State.unique(), key="State" , on_change=changevalue, kwargs={'category': "State"})
city = cols[3].st.selectbox("CITY", st.session_state.df.City.unique(), key="City", on_change=changevalue, kwargs={'category': "City"})

if 'df' not in st.session_state or st.button("RESET FILTER"):
    st.session_state.df = df

st.dataframe(st.session_state.df)

You don’t need parentheses with st.sidebar

with st.sidebar:
    st.write('something')
1 Like

I changed it but still the error persists. Here is the error below:

AttributeError: st.session_state has no attribute "df". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization

Hi @doug2023,

Here’s a re-write of your code, along with the sidebar part:

import streamlit as st
import pandas as pd

if 'df' not in st.session_state:
    st.session_state.df = pd.DataFrame({ "Name": ["Audi", "Mercedes", "Region 1", 'Vermont', 'Region 3', 'Burlington'], 
                                         "Category": ['Product', 'Product', "Region", "State", "Region", "City"]})

cols1 = st.sidebar.columns(2)
product = cols1[0].selectbox("PRODUCT", options = st.session_state.df[st.session_state.df['Category']=="Product"], key="Product")
region  = cols1[1].selectbox("REGION", options = st.session_state.df[st.session_state.df['Category']=="Region"], key="Region" )

cols2 = st.sidebar.columns(2)
state = cols2[0].selectbox("STATE", options = st.session_state.df[st.session_state.df['Category']=="State"], key="State" )
city = cols2[1].selectbox("CITY", options = st.session_state.df[st.session_state.df['Category']=="City"], key="City")

st.dataframe(st.session_state.df)

All you need to do is add code if you want to finally filter the dataset (df) on the basis of the dropdown selections. Give it a try.

Cheers

1 Like

But is there a need to place columns to avoid an error? Isn’t it possible to have a selectbox below the other?

Hi @doug2023,

Then in that case, do the following:

  1. delete these lines
    cols1 = st.sidebar.columns(2)
    cols2 = st.sidebar.columns(2)

  2. replace the following text

cols1[0].
cols1[1].
cols2[0].
cols2[1].

with

st.sidebar.

Cheers

1 Like

Can you clarify what layout you are looking for?

It works, but it doesn’t filter. I have a .csv file with thousands of lines that I turn into a dataframe with pd.read_csv first. Not a dataframe created with pd.Dataframe. There’s no list of Categories, so it won’t work there in the selectbox. Sorry for my ignorance. I’m new to streamlit and learning more Python.

st.session_state.df = pd.DataFrame({ "Name": ["Audi", "Mercedes", "Region 1", 'Vermont', 'Region 3', 'Burlington'], 
                                         "Category": ['Product', 'Product', "Region", "State", "Region", "City"]})

It would be in sidebar but the method that @shawn_pereira passed works just needs a correction.

1 Like

Hi @doug2023

  1. The pd.dataframe was done because I didn’t have access to your dataframe, so I just created a temporary dataframe to illustrate the functionality.
  2. if you read the end of my initial message, you will see that I mentioned that you should try to filter the dataset (on your own). I can easily do that, but you would learn much better if you try somethings yourself. I guess, to that extent the code [does not] work, right?

YouTube has a lot of videos on Streamlit and pandas. Go through those too.

Cheers

1 Like

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