Streamlit Modal X button is not working


I attempted to create a modal for a pop-up in my app using streamlit-modal. While the modal works, I had to manually add a close button to each modal, which is cumbersome. This is especially frustrating because the default “X” button in the modal is present but doesn’t seem to work as expected. Am I missing something here?

import streamlit as st
from streamlit_modal import Modal

modal = Modal(key="confirmation_modal")
if st.button("Open Pop-up"):
    modal.open()

# Render the modal
if modal.is_open():
    with modal.container():
        st.write("Are you sure you want to proceed?")
        col1, col2 = st.columns(2)

        with col1:
            if st.button("Yes"):
                st.success("You selected Yes!")
                modal.close()

        with col2:
            if st.button("No"):
                st.warning("You selected No!")
                modal.close()

Is tere a reason you cannot use dialog?

Using dialog box comes with cons. LIke it mentioned in it st.dialog - Streamlit Docs

In a same script, I need to use 2 dialog at the same time. Do we have anything other than this. Thanks in advance

Your code raises an error for me:

TypeError: Modal.__init__() missing 1 required positional argument: 'title'

After fixing that, it seens to work as expected. Specifically, the default close button closes the modal.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.