Hello,
I would like to have a button that has the following functionality:
Each time I click it, it creates a pair of widgets, a selectbox and a button right next to each other, the button next to the selectbox is deleting the selectbox and itself when clicked.
I had an idea of making each of these pairs a 1 by 2 table of widgets so that the button and selectbox would appear next to each other and would not drift apart.
Before that I tried using columns and got the following problem:
You need to create two columns separately for each row. Here is a simple example.
import streamlit as st
def remove_row(row):
st.session_state.rows.remove(row)
if "rows" not in st.session_state:
st.session_state.rows = []
add_button = st.button(label="Add")
if add_button:
next_row = max(st.session_state.rows) + 1 if st.session_state.rows else 1
st.session_state.rows.append(next_row)
for row in st.session_state.rows:
left, right = st.columns(2)
left.selectbox(label=f"Test {row}", options="abcde")
right.button(
label="Remove", key=f"remove_{row}", on_click=remove_row, kwargs={"row": row}
)
Thank you so much, it worked!
Is there any way to make the remove button work? It doesn’t work now.
Another problem I am facing is that whenever I click on remove or choose some option from the selectbox, the screen goes dark
Before I click something:
The Remove button works for me and I don’t see the screen going dark. But then your code seems to be different than mine so the behavior can also be different.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.