How can I create a dynamic form that refreshes data?

I’m trying to create a form that essentially will do the following:

  • Feeds an input dataframe one row at a time into a form
  • The form has an accept and reject option radio buttons
  • If accept is selected and the submit button pressed, the displalyed row will get written to an empty output dataframe
  • If reject is selected, a text input field pops up, this field will search the input dataframe keywords
  • It will then populate the results into a dropdown field that appears when the user hits enter and they will select one of the following listed items
  • once the user clicks submit, the input row gets pushed to the output dataframe as well as the value from the selected dropdown (gets added to a column called rejection suggestion)
  • when the submit but gets hit no matter what, the displayed row will refresh

Here’s kind of what I have so far. This version allows me to have a form because my earlier version wasn’t letting me display text fields when a radio button is selected within a form. But an issue that arises with this version is that I have to click the submit button in order to get the dropdown text to appear when the reject radio button is selected.

import streamlit as st
import pandas as pd

# Load your DataFrame (replace with your actual data)
data = {'description': ['apple', 'banana', 'orange', 'grape', 'chicken soup', 'ramen', 'green apple']
       , 'id': ['1', '2', '3', '4', '5', '6', '7']
       }
df = pd.DataFrame(data)
# Concatenate 'id' with 'description' using a lambda function
df['combined'] = df.apply(lambda row: f"{row['id']} | {row['description']}", axis=1)


def radio_change(label):
    if label == 'Reject':
        st.text_area(label='Reject Reason')
        st.text_input('Keyword search:', '')
        
def search_dropdown(search_keyword, df):
    if search_keyword:
        # Filtering logic
        filtered_df = df[df['combined'].str.contains(search_keyword, case=False)]

        # Display filtered results in a dropdown
        selected_item = st.selectbox('Select a suggestion:', filtered_df['combined'])
    
        st.write(f'Selected item: **{selected_item}**')


# Add radio buttons
label_options = ['Accept', 'Reject']
selected_label = st.radio("Select Label:", label_options, key='label_radio')

with st.form('test'):
    if selected_label == 'Reject':
    
        reject_text = st.text_area(label='Reject Reason')
        search_keyword = st.text_input('Keyword search:', '')
    
        # Displays a dropdown populated with search results from search_keyword
        search_dropdown(search_keyword, df)
    submit_button = st.form_submit_button('Submit')

I was able to get it working with this rudamentary setup below (manually defining the options values instead of data:

def radio_change(label):
    if label == 'Reject':
        st.text_area(label='Reject Reason')
        st.text_input('Keyword search:', '')


def search_dropdown(search_keyword):
    if search_keyword:
        # Filtering logic
        filtered_df = 'test'

        # Display filtered results in a dropdown
        selected_item = st.selectbox('Select a suggestion:', ['Opt 1', 'Opt 2', 'Opt 3', ])

        st.write(f'Selected item: **{selected_item}**')


label_options = ['Accept', 'Reject']
selected_label = st.radio("Select Label:", label_options, key='label_radio')

with st.form('test'):
    if selected_label == 'Reject':
        reject_text = st.text_area(label='Reject Reason')
        search_keyword = st.text_input('Keyword search:', '')

        # Displays a dropdown populated with search results from search_keyword
        search_dropdown(search_keyword)
    submit_button = st.form_submit_button('Submit')