From 2aba1bc6eaaf1d13a1348387e7c58b6251d79400 Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Mon, 28 Apr 2025 16:44:57 +0200 Subject: [PATCH 1/3] :art: figure out textwrap, fix warnings - separate common imports - remove import and specify encoding --- src/vuegen/streamlit_reportview.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/vuegen/streamlit_reportview.py b/src/vuegen/streamlit_reportview.py index 534a537..b0a3a74 100644 --- a/src/vuegen/streamlit_reportview.py +++ b/src/vuegen/streamlit_reportview.py @@ -1,7 +1,7 @@ import os -import re import subprocess import sys +import textwrap from pathlib import Path from typing import List @@ -75,16 +75,29 @@ def generate_report( self.report.logger.debug("Processing app navigation code.") # Define the Streamlit imports and report manager content report_manag_content = [] + report_manag_content.append( + textwrap.dedent( + """\ + import streamlit as st + """ + ) + ) if self.report.logo: report_manag_content.append( - f"""import streamlit as st\n -st.set_page_config(layout="wide", page_title="{self.report.title}", page_icon="{self.report.logo}") -st.logo("{self.report.logo}")""" + textwrap.dedent( + f"""\ + st.set_page_config(layout="wide", page_title="{self.report.title}", page_icon="{self.report.logo}") + st.logo("{self.report.logo}") + """ + ) ) else: report_manag_content.append( - f"""import streamlit as st\n -st.set_page_config(layout="wide", page_title="{self.report.title}")""" + textwrap.dedent( + f"""\ + st.set_page_config(layout="wide", page_title="{self.report.title}") + """ + ) ) report_manag_content.append( self._format_text( @@ -147,7 +160,9 @@ def generate_report( ) # Write the navigation and general content to a Python file - with open(Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w") as nav_manager: + with open( + Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w", encoding="utf8" + ) as nav_manager: nav_manager.write("\n".join(report_manag_content)) self.report.logger.info( f"Created app navigation script: {self.REPORT_MANAG_SCRIPT}" From 1d6db0737e69c37e2a5df178a344a5233c53bb0c Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Mon, 28 Apr 2025 16:45:25 +0200 Subject: [PATCH 2/3] :sparkles: Add shutdown button - see https://discuss.streamlit.io/t/close-streamlit-app-with-button-click/35132/5 --- src/vuegen/streamlit_reportview.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/vuegen/streamlit_reportview.py b/src/vuegen/streamlit_reportview.py index b0a3a74..d401b29 100644 --- a/src/vuegen/streamlit_reportview.py +++ b/src/vuegen/streamlit_reportview.py @@ -78,6 +78,10 @@ def generate_report( report_manag_content.append( textwrap.dedent( """\ + import os + import time + + import psutil import streamlit as st """ ) @@ -155,8 +159,24 @@ def generate_report( # Add navigation object to the home page content report_manag_content.append( - f"""report_nav = st.navigation(sections_pages) -report_nav.run()""" + textwrap.dedent( + """\ + report_nav = st.navigation(sections_pages) + + # Following https://discuss.streamlit.io/t/close-streamlit-app-with-button-click/35132/5 + exit_app = st.sidebar.button("Shut Down App") + if exit_app: + st.toast("Shutting down the app...") + time.sleep(1) + # Terminate streamlit python process + pid = os.getpid() + p = psutil.Process(pid) + p.terminate() + + + report_nav.run() + """ + ) ) # Write the navigation and general content to a Python file From 03d5700533a9f69ece114a9b51341592cb76e9c8 Mon Sep 17 00:00:00 2001 From: sayalaruano Date: Tue, 29 Apr 2025 12:09:47 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8=20Add=20emoji=20to=20the=20turn?= =?UTF-8?q?=20off=20button=20and=20make=20it=20the=20size=20of=20sidebar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/vuegen/streamlit_reportview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vuegen/streamlit_reportview.py b/src/vuegen/streamlit_reportview.py index d401b29..a91314c 100644 --- a/src/vuegen/streamlit_reportview.py +++ b/src/vuegen/streamlit_reportview.py @@ -164,7 +164,7 @@ def generate_report( report_nav = st.navigation(sections_pages) # Following https://discuss.streamlit.io/t/close-streamlit-app-with-button-click/35132/5 - exit_app = st.sidebar.button("Shut Down App") + exit_app = st.sidebar.button("Shut Down App", icon=":material/power_off:", use_container_width=True) if exit_app: st.toast("Shutting down the app...") time.sleep(1)