Get the previous selection of a slider in streamlit

Viewed 83

I am currently developing an important streamlit app, but I am facing one issue, which I wasn’t able to solve yet.

I have a range slider for years. The problem now is, that I want to have the previous selection of this slider. After clicking a specific button, the previous year range should set itself to the current year range (because after that the previous year range should be considered as the current year range). I will explain the exact problem below the code, because you can understand it better after reading the code.

Here is what I currently have:

# At the start of the app
if 'year_range' not in st.session_state:
   st.session_state.year_range = [2000, 2022]
if 'previous_year_range' not in st.session_state:
   st.session_state.previous_year_range = [2000, 2022]
 
# The slider where the user can select the year range (st.session_state.min_max is not important for this problem, so you can ignore it)
st.session_state.year_range[0], st.session_state.year_range[1] = 
st.slider("Select years range:",  min_value=st.session_state.min_max[0], value=st.session_state.year_range, max_value=st.session_state.min_max[1])

# The button -> When clicked, set previous_year_range to year_range
button = col1.button('Submit and Compare')
if button:
   populate_graph()

def populate_graph():
   if st.session_state.year_range != st.session_state.previous_year_range
      clear_graphs() # Function that should run when the year_range changed
      st.session_state.previous_year_range = st.session_state.year_range # Reset the previous year range to the new year range

So, what is the problem now exactly?

If I change the year range the first time, I can see, that the year range has changed and previous year range is not equal to year range.

Then, after clicking the button, populate_graph is executed and because they are unequal, it executes the if statement. Now I change the year slider again but now, previous year range sets itself automatically to year range, even if the button wasn’t clicked. I assume, it has something to do with st.session_state.previous_year_range = st.session_state.year_range, but I can’t figure out why it sets itself to the year range.

It would be very great if you could help me, I have a deadline till tomorrow and have to fix this bug till then

1 Answers

Create a new copy when you update the value of previous_year_range like the code below.

st.session_state.previous_year_range = st.session_state.year_range.copy()

Sample output

enter image description here

Now you can modify the slider without changing the previous_year_range unless the button is pressed.

Code

The code that I use.

import streamlit as st


# At the start of the app
if 'year_range' not in st.session_state:
   st.session_state.year_range = [2000, 2022]
if 'previous_year_range' not in st.session_state:
   st.session_state.previous_year_range = [2000, 2022]


def populate_graph():
   if st.session_state.year_range != st.session_state.previous_year_range:
      # clear_graphs() # Function that should run when the year_range changed

      # Reset the previous year range to the new year range
      st.session_state.previous_year_range = st.session_state.year_range.copy()


def main():
   # The slider where the user can select the year range
   # (st.session_state.min_max is not important for this problem, so you can ignore it)
   st.session_state.year_range[0], st.session_state.year_range[1] = st.slider(
      "Select years range:",
      min_value=2000,
      value = [2000, 2022],
      max_value=2022)  
   
   st.write(f'selected year_range: {st.session_state.year_range}')
   st.write(f'previous_year_range: {st.session_state.previous_year_range}')
   
   # The button -> When clicked, set previous_year_range to year_range
   col1, _ = st.columns(2)
   button = col1.button('Submit and Compare')
   if button:
      populate_graph()
      st.write(f'after button is clicked, previous_year_range: {st.session_state.previous_year_range}')

main()
Related