I have a local streamlit app, which is a chat interface. I have button for placeholder messages, user can click on one of them and the appropriate message will be sent automatically.
1- User can click on : Click here to display placeholder messages
I would like to fix an UI issue, once user has clicked on a message, I want all placeholders messages and the call to action button “Click here” to disappear.
What would be the best “streamlit” way to do so ?
You can use the function ‘on_click’ combine with session_state.
# Initialize session state for controlling the visibility
if 'clicked' not in st.session_state:
st.session_state.clicked = False
# Function to handle placeholder message click
def handle_placeholder_click(message=None):
# Set the session state to True so that placeholders and button are hidden
st.session_state.clicked = ~st.session_state.clicked
# Simulate sending the message and display it
st.write(f"Message sent: {message}")
# If no message has been clicked, show the "Click here" button and placeholders
if not st.session_state.clicked:
# Button to display the placeholders
if st.button("Click here to display placeholder messages"):
# Display the placeholder messages (as buttons)
st.button("Placeholder 1", on_click=handle_placeholder_click, args=("Placeholder 1",))
st.button("Placeholder 2", on_click=handle_placeholder_click, args=("Placeholder 2",))
st.button("Placeholder 3", on_click=handle_placeholder_click, args=("Placeholder 3",))
else:
# If a message has been sent, display a message saying it is being processed
st.write("The message has been sent and is being processed.")
st.button('RESET',on_click=handle_placeholder_click, args=("RESET BUTTON",))