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')