Update value for selectbox in streamlit in real-time

Viewed 304

How do I update the value of selectbox in streamlit dynamically?

here is a minimum non-working example

import streamlit as st
import time
def main():
 st.session_state['choice'] = list(range(2))
 st.selectbox("myoptions", options=st.session_state['choice'])
 while True:
  st.session_state['choice'] = list(range(5))
  time.sleep(2)

The reason I try to do this is to simulate the process of fetching new data and then updating the options.

In reality, i'll update the options every hour or so

1 Answers

Here is the solution:

import streamlit as st
import time
import random
def skeleton():
    left, right = st.columns(2)
    with right:
        numbers = st.empty()
    return left, right, numbers

left, right, numbers = skeleton()
while True:
    with right:
        with numbers.container():
            st.selectbox('food',random.sample(range(10, 40), 4))
            time.sleep(2)


Related