Skip to content

Commit 456e65b

Browse files
enryHsayalaruano
andauthored
✨ Shut down button for streamlit app (#113)
* 🎨 figure out textwrap, fix warnings - separate common imports - remove import and specify encoding * ✨ Add shutdown button - see https://discuss.streamlit.io/t/close-streamlit-app-with-button-click/35132/5 * ✨ Add emoji to the turn off button and make it the size of sidebar --------- Co-authored-by: sayalaruano <[email protected]>
1 parent 12965eb commit 456e65b

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
2-
import re
32
import subprocess
43
import sys
4+
import textwrap
55
from pathlib import Path
66
from typing import List
77

@@ -75,16 +75,33 @@ def generate_report(
7575
self.report.logger.debug("Processing app navigation code.")
7676
# Define the Streamlit imports and report manager content
7777
report_manag_content = []
78+
report_manag_content.append(
79+
textwrap.dedent(
80+
"""\
81+
import os
82+
import time
83+
84+
import psutil
85+
import streamlit as st
86+
"""
87+
)
88+
)
7889
if self.report.logo:
7990
report_manag_content.append(
80-
f"""import streamlit as st\n
81-
st.set_page_config(layout="wide", page_title="{self.report.title}", page_icon="{self.report.logo}")
82-
st.logo("{self.report.logo}")"""
91+
textwrap.dedent(
92+
f"""\
93+
st.set_page_config(layout="wide", page_title="{self.report.title}", page_icon="{self.report.logo}")
94+
st.logo("{self.report.logo}")
95+
"""
96+
)
8397
)
8498
else:
8599
report_manag_content.append(
86-
f"""import streamlit as st\n
87-
st.set_page_config(layout="wide", page_title="{self.report.title}")"""
100+
textwrap.dedent(
101+
f"""\
102+
st.set_page_config(layout="wide", page_title="{self.report.title}")
103+
"""
104+
)
88105
)
89106
report_manag_content.append(
90107
self._format_text(
@@ -142,12 +159,30 @@ def generate_report(
142159

143160
# Add navigation object to the home page content
144161
report_manag_content.append(
145-
f"""report_nav = st.navigation(sections_pages)
146-
report_nav.run()"""
162+
textwrap.dedent(
163+
"""\
164+
report_nav = st.navigation(sections_pages)
165+
166+
# Following https://discuss.streamlit.io/t/close-streamlit-app-with-button-click/35132/5
167+
exit_app = st.sidebar.button("Shut Down App", icon=":material/power_off:", use_container_width=True)
168+
if exit_app:
169+
st.toast("Shutting down the app...")
170+
time.sleep(1)
171+
# Terminate streamlit python process
172+
pid = os.getpid()
173+
p = psutil.Process(pid)
174+
p.terminate()
175+
176+
177+
report_nav.run()
178+
"""
179+
)
147180
)
148181

149182
# Write the navigation and general content to a Python file
150-
with open(Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w") as nav_manager:
183+
with open(
184+
Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w", encoding="utf8"
185+
) as nav_manager:
151186
nav_manager.write("\n".join(report_manag_content))
152187
self.report.logger.info(
153188
f"Created app navigation script: {self.REPORT_MANAG_SCRIPT}"

0 commit comments

Comments
 (0)