|
1 | 1 | import os |
2 | | -import re |
3 | 2 | import subprocess |
4 | 3 | import sys |
| 4 | +import textwrap |
5 | 5 | from pathlib import Path |
6 | 6 | from typing import List |
7 | 7 |
|
@@ -75,16 +75,33 @@ def generate_report( |
75 | 75 | self.report.logger.debug("Processing app navigation code.") |
76 | 76 | # Define the Streamlit imports and report manager content |
77 | 77 | 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 | + ) |
78 | 89 | if self.report.logo: |
79 | 90 | 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 | + ) |
83 | 97 | ) |
84 | 98 | else: |
85 | 99 | 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 | + ) |
88 | 105 | ) |
89 | 106 | report_manag_content.append( |
90 | 107 | self._format_text( |
@@ -142,12 +159,30 @@ def generate_report( |
142 | 159 |
|
143 | 160 | # Add navigation object to the home page content |
144 | 161 | 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 | + ) |
147 | 180 | ) |
148 | 181 |
|
149 | 182 | # 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: |
151 | 186 | nav_manager.write("\n".join(report_manag_content)) |
152 | 187 | self.report.logger.info( |
153 | 188 | f"Created app navigation script: {self.REPORT_MANAG_SCRIPT}" |
|
0 commit comments