|
1 | 1 | import os |
2 | 2 | import subprocess |
3 | 3 | import sys |
| 4 | +import textwrap |
4 | 5 | from pathlib import Path |
5 | 6 | from typing import List |
6 | 7 |
|
@@ -105,16 +106,33 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None: |
105 | 106 | self.report.logger.debug("Processing app navigation code.") |
106 | 107 | # Define the Streamlit imports and report manager content |
107 | 108 | 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 | + ) |
108 | 120 | if self.report.logo: |
109 | 121 | 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 | + ) |
113 | 128 | ) |
114 | 129 | else: |
115 | 130 | 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 | + ) |
118 | 136 | ) |
119 | 137 | report_manag_content.append( |
120 | 138 | self._format_text( |
@@ -189,12 +207,30 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None: |
189 | 207 |
|
190 | 208 | # Add navigation object to the home page content |
191 | 209 | 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 | + ) |
194 | 228 | ) |
195 | 229 |
|
196 | 230 | # 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: |
198 | 234 | nav_manager.write("\n".join(report_manag_content)) |
199 | 235 | self.report.logger.info( |
200 | 236 | f"Created app navigation script: {self.REPORT_MANAG_SCRIPT}" |
|
0 commit comments