Slider Creation and Update in While Loop


Hi everyone,
for an application I need a slider that selects an index for data to display. This slider is only needed after the first iteration though, which makes it tough to implement (at least for meโ€ฆ).

Steps to reproduce

Code snippet:

import streamlit as st
import numpy as np
import time

def add_locations(iteration_number):
    # add new locations
    locations.append(np.random.rand(iteration_number, 2))

iteration_number = 0
locations = []
i = 0 # selected index

# create widget for slider
slider_container = st.empty()
loc_container = st.empty()

while True:
    iteration_number += 1

    #add slider
    if iteration_number > 1:
        i = slider_container.slider('Iteration', min_value=1, max_value=iteration_number, step=1)

    loc_container.write(locations[i - 1])

    if iteration_number == 5:

Expected behavior:

What I want:

  • on the first iteration the first locations generated are shown
  • on the second the user can choose betweeen 1st and 2nd data (and so on)
  • for every iteration the value of the slider should stay at its old value, i.e., if I am looking at data from index 2 and data for index 3 was added I want the slider to change but to (1, 3) with value 2 so that the data does not change

Actual behavior:

  • Once I slide the slider the routine starts again instead of just showing the different data.
  • I think session states are important for the slider to stay at its current value. Since I create the slider every iteration. I cant give it a key though which hinders me using session states

Any help would be highly appreciated :slight_smile:

Maybe Iโ€™m misunderstanding, but I donโ€™t think a while loop is what you want here at all. Can you re-explain what you want (not in terms of what you what each loop to be doing but instead in terms of what you want the user to see and the result to be accomplished)? Are you wanting the user to make a selection as each stage? Are you wanting to force a selection if they fail to respond in 5 seconds or do you just want to wait for their selection?

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