Skip to content

Commit 0f39cf9

Browse files
authored
Merge branch 'main' into more_flexible_folder_structure
2 parents e013038 + 456e65b commit 0f39cf9

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import sys
4+
import textwrap
45
from pathlib import Path
56
from typing import List
67

@@ -105,16 +106,33 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
105106
self.report.logger.debug("Processing app navigation code.")
106107
# Define the Streamlit imports and report manager content
107108
report_manag_content = []
109+
report_manag_content.append(
110+
textwrap.dedent(
111+
"""\
112+
import os
113+
import time
114+
115+
import psutil
116+
import streamlit as st
117+
"""
118+
)
119+
)
108120
if self.report.logo:
109121
report_manag_content.append(
110-
f"""import streamlit as st\n
111-
st.set_page_config(layout="wide", page_title="{self.report.title}", page_icon="{self.report.logo}")
112-
st.logo("{self.report.logo}")"""
122+
textwrap.dedent(
123+
f"""\
124+
st.set_page_config(layout="wide", page_title="{self.report.title}", page_icon="{self.report.logo}")
125+
st.logo("{self.report.logo}")
126+
"""
127+
)
113128
)
114129
else:
115130
report_manag_content.append(
116-
f"""import streamlit as st\n
117-
st.set_page_config(layout="wide", page_title="{self.report.title}")"""
131+
textwrap.dedent(
132+
f"""\
133+
st.set_page_config(layout="wide", page_title="{self.report.title}")
134+
"""
135+
)
118136
)
119137
report_manag_content.append(
120138
self._format_text(
@@ -189,12 +207,30 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
189207

190208
# Add navigation object to the home page content
191209
report_manag_content.append(
192-
f"""report_nav = st.navigation(sections_pages)
193-
report_nav.run()"""
210+
textwrap.dedent(
211+
"""\
212+
report_nav = st.navigation(sections_pages)
213+
214+
# Following https://discuss.streamlit.io/t/close-streamlit-app-with-button-click/35132/5
215+
exit_app = st.sidebar.button("Shut Down App", icon=":material/power_off:", use_container_width=True)
216+
if exit_app:
217+
st.toast("Shutting down the app...")
218+
time.sleep(1)
219+
# Terminate streamlit python process
220+
pid = os.getpid()
221+
p = psutil.Process(pid)
222+
p.terminate()
223+
224+
225+
report_nav.run()
226+
"""
227+
)
194228
)
195229

196230
# Write the navigation and general content to a Python file
197-
with open(Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w") as nav_manager:
231+
with open(
232+
Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w", encoding="utf8"
233+
) as nav_manager:
198234
nav_manager.write("\n".join(report_manag_content))
199235
self.report.logger.info(
200236
f"Created app navigation script: {self.REPORT_MANAG_SCRIPT}"

0 commit comments

Comments
 (0)