You can try this:
import streamlit as st
import time
import keyboard
import os
import psutil
exit_app = st.sidebar.button("Shut Down")
if exit_app:
# Give a bit of delay for user experience
time.sleep(5)
# Close streamlit browser tab
keyboard.press_and_release('ctrl+w')
# Terminate streamlit python process
pid = os.getpid()
p = psutil.Process(pid)
p.terminate()