When user close st.dialog, i want user use my button not use x or other method


The st.dialog doc not show how to stop the x or esc, I hope can use my button not x or esc.

Because I set a st.session_state[“key”] to make data_editor can reset.
There is any method can solve it?

1 Like

You can refer to the following methods to make adjustments, where the method of hiding buttons is the CSS style design in ‘st. html’.

import streamlit as st

st.html(
    '''
        <style>
            div[aria-label="dialog"]>button[aria-label="Close"] {
                display: none;
            }
        </style>
    '''
)

@st.experimental_dialog(title='title')
def test_dialog():
    st.write('Hello Streamlit')

if st.button(label='open_dialog', key='test_dialog', type='primary'):
    test_dialog()
2 Likes

Thank!!! This help me a lot.
I also want to know how to prevent the dialog from closing when the user clicks on the grey background(purple section).
I can’t find name of the grey background(purple section).

1 Like

There is also an open request around customizing the behavior around the X to close. The discussion includes ignoring the X and basically configuring the dialog to be dismissible or not. Feel free to vote or add to the conversation:

2 Likes

Since the dialog can be closed if you press “esc”, I used “pyautogui”, a python library, to handle pressing the “esc” button after the button click. This worked for me:
image

This sounded like a cool idea but pyautogui doesn’t run from the streamlit run since mouse is not available on the server, the package cannot be loaded to the client. Did it work for you?

2024-09-07 09:33:42.321 Uncaught app exception
Traceback (most recent call last):
  File "/workspace/voting-app/cx/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "/workspace/voting-app/cx/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 590, in code_to_exec
    exec(code, module.__dict__)
  File "/workspace/voting-app/cx/app/app.py", line 51, in <module>
    pg.run()
  File "/workspace/voting-app/cx/venv/lib/python3.12/site-packages/streamlit/navigation/page.py", line 291, in run
    exec(code, module.__dict__)
  File "/workspace/voting-app/cx/app/ui/channel_list.py", line 5, in <module>
    import pyautogui
  File "/workspace/voting-app/cx/venv/lib/python3.12/site-packages/pyautogui/__init__.py", line 246, in <module>
    import mouseinfo
  File "/workspace/voting-app/cx/venv/lib/python3.12/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
                       ~~~~~~~~~~^^^^^^^^^^^
  File "<frozen os>", line 714, in __getitem__
KeyError: 'DISPLAY'

You’re right, it worked when I run it on windows, but once I migrated to the server I got this error. I used the “keyboard” library (keyboard · PyPI) as a workaround but encountered the same issue (os.environ[‘DISPLAY’]) with other libraries. So it’s better to fix that problem first to avoid any issues(still can’t fix it :smiling_face_with_tear:). But You can still try the keyboard library as a quick fix.