So, when I hit the ‘random plot’ button, I want to remove the input widget from the UI.
I understand that this is a problem, but I was wondering if anyone knew of a workaround or had any ideas on how to solve it.
You can actually use st.empty() to create a placeholder for the number input widget, which you can then clear after a button click.
Here’s how:
import streamlit as st
import numpy as np
# Function to clear the number input widget
def clear_number_widget(widget):
widget.empty()
# Insert a single element container
placeholder = st.empty()
if 'flag' not in st.session_state:
st.session_state.flag = 0
if st.session_state.flag ==0:
# Use the placeholder container to hold a number input widget
n = placeholder.number_input("enter number:",1,10,4)
if st.button("random plot"):
st.session_state.flag =1
# Clear the number input widget
clear_number_widget(placeholder)
st.bar_chart(np.random.randn(50, 3))