Unexpected values in st.form using random

To help the user I’m trying to prefill st.form with random values, but the text_input field updates with random values when the form is clicked, and ignores the user input.

I understand it’s to do with how streamlit control flow works – but I thought that was the point of st.form, to pause the flow for user input?


import streamlit as st
import random

selection = None
cities = ['London', 'Singapore', 'Alexandria']
rand_city = random.sample(cities, 1)[0]

with st.form(key='selector'):
    city = st.text_input('Select city', rand_city)
    submitted = st.form_submit_button('➡️ Submit')
    if submitted: selection = city

st.write('rand_city = ', rand_city)
st.write('selection = ', selection)

Thanks :v:

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