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",))
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.