Skip to content

Commit 0c1d279

Browse files
committed
🎨 make Streamlit report aware of where it runs
- keep also subprocess option in code for local development
1 parent 363e8bf commit 0c1d279

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def __init__(
2828
):
2929
super().__init__(report=report, report_type=report_type)
3030
self.streamlit_autorun = streamlit_autorun
31+
self.BUNDLED_EXECUTION = False
32+
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
33+
self.report.logger.info("running in a PyInstaller bundle")
34+
self.BUNDLED_EXECUTION = True
35+
else:
36+
self.report.logger.info("running in a normal Python process")
3137

3238
def generate_report(
3339
self, output_dir: str = SECTIONS_DIR, static_dir: str = STATIC_FILES_DIR
@@ -171,20 +177,27 @@ def run_report(self, output_dir: str = SECTIONS_DIR) -> None:
171177
self.report.logger.debug(
172178
f"Running Streamlit report from file: {target_file}"
173179
)
174-
args = [
175-
"streamlit",
176-
"run",
177-
target_file,
178-
"--global.developmentMode=false",
179-
]
180-
sys.argv = args
181-
182-
sys.exit(stcli.main())
180+
if self.BUNDLED_EXECUTION:
181+
args = [
182+
"streamlit",
183+
"run",
184+
target_file,
185+
"--global.developmentMode=false",
186+
]
187+
sys.argv = args
188+
189+
sys.exit(stcli.main())
190+
else:
191+
self.report.logger.debug("Run using subprocess.")
192+
subprocess.run(
193+
[sys.executable, "-m", "streamlit", "run", target_file],
194+
check=True,
195+
)
183196
except KeyboardInterrupt:
184197
print("Streamlit process interrupted.")
185-
# except subprocess.CalledProcessError as e:
186-
# self.report.logger.error(f"Error running Streamlit report: {str(e)}")
187-
# raise
198+
except subprocess.CalledProcessError as e:
199+
self.report.logger.error(f"Error running Streamlit report: {str(e)}")
200+
raise
188201
else:
189202
# If autorun is False, print instructions for manual execution
190203
self.report.logger.info(

0 commit comments

Comments
 (0)