I’m trying to close a popup created in HTML format. I’ve added a button inside the popup, and I want it to trigger the close_popup function when clicked. How can I set it up so that the close_popup function is called when the “Got it” button is clicked?
Also, I want to close the browser tab with one separate button click. Is it possible to do that in a streamlit?
If you want to have a frontend component with JavaScript (that doesn’t communicate with your Python backend), you can inject scripts with the components module:
import streamlit as st
from streamlit.component.v1 import html
html("<script> ... </script>")
However, from what you are describing, perhaps you can accomplish what you want with Streamlit dialogs so you don’t have to inject your own scripts.
thanks for response. using st.dialog method I didn’t get the desire output like I show it using HTML format but yes it works. One advantage of using st.dialog is that it provides the default close button so I don’t need to manage it separately.
Here is the code. @st.dialog(“Instruction”)
def show_popup():
st.markdown(“”"
- instruciton goes here
“”")
For security reasons, JavaScript has some tight restrictions for closing a tab programmatically. It will only work under specific circumstances, so I can’t provide a general solution to that…
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.