St.form showing old results even when input is cleared. 2nd form is updating based on input in 1st form

Hi, I have a function that recommends 3 random but similar cities, based on an input of the users current city. And another function that recommends 3 random, non-similar cities.
I am using st.form, st.text_input and submit_button for each function.
I am new to Streamlit and have 2 problems:

  1. When clearing out the input city, the results from the previous input still remain. Even when refreshing the page. Is there are way for the form to reset after the input is cleared out?

  2. When submitting an input for my first st.form (similar city), the 2nd st.form (non-similar city) populates based on the input in the first form. And vice versa.

This is the code I am using to put my function on streamlit:

#take user city as input
with st.form(key=‘my_cities’):
my_city = st.text_input(label=“Enter your current city (City, Country):”)
submit_button = st.form_submit_button(label=‘Get my next living destination!’)
similar_city_recommendations(city_country = my_city)

I would like the results to clear once the text box is cleared. I am making a demo video and I can’t have the previous results shown in the demo.

Again, both forms update based on input from just 1 form.

Any guidance would be appreciated!!

Using Streamlit version 0.87.0 and python version 3.8.8

1 Like

Just signed up but I think the clear_on_submit = True should work for this issue.

use st.session_state to save data

code example

import streamlit as st 
from datetime import datetime

def my_callback():
    print(f"curdate: {datetime.now()} first: {st.session_state.value1} sec: {st.session_state.value2}")

with st.form('my_form',clear_on_submit=True):
    st.radio(label='🙆‍♀️ part1:', options=[
                        '解决', '未解决', '未完全解决'], horizontal=True,
                        key='value1')

    error_list = ['答非所问', '推荐错误', '推荐不准确', '回答不详细', '信息更新不及时']
    not_saf_reason = st.radio(
        label='🤦‍♀️ part2:', options=error_list, horizontal=True,key='value2')

    submit_button = st.form_submit_button(
                    label='📥 点我提交', on_click=my_callback
                    )