Run clean-up code after user clicked on stop button (left of the menu hamburger), or ctrl-c

Summary

When a streamlit script is running, the user can click on the โ€œStopโ€ button left of the menu hamburger, or press ctrl-c to stop streamlit altogether. I would like to run some code when this happens, in order to do some clean up before streamlit stops. Is there a way to do that please?

I am implementing something using asyncio per this thread, and when the user stops the processing, my asyncio thread is left hanging.

Thank you.

The best I can think of is to create a button or instructions at the top to click to either lure the user away from using the stop from the menu or instructing them how to properly refresh if they did click Stop.

Here is a thread about hiding that menu (by also hiding the header where the Running and Stop messages post, effectively taking the option away (though a user could circumvent it). Hereโ€™s the quick answer, but there are other solutions in the thread: How do i hide/ remove the menu in production? - #22 by snehankekre

You may be interested in GitHub Feature requests to customize that menu or hide it simply by command:

1 Like

Thank you very much, @mathcatsand