Skip to content

Commit 47b924b

Browse files
committed
🚧🐛 streamlit not easy to run from within vuegen script
- sys.exectuable is vuegen script, not the original Python Interpreter -> not easy to run `pthon -m streamlit run file.py` - manually starting and building streamlit_run command does not connect to browser atm
1 parent 41cdd53 commit 47b924b

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

executables/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,27 @@
22

33
- pyvis templates were not copied, so make these explicit (see [this](https://stackoverflow.com/a/72687433/9684872))
44

5-
```
5+
```bash
66
# from root of the project
7-
pyinstaller -D -w --collect-all pyvis -n vuegen src/vuegen/__main__.py
7+
pyinstaller -D --collect-all pyvis -n vuegen src/vuegen/__main__.py
8+
# from this README folder
9+
pyinstaller -D --collect-all pyvis --collect-all streamlit -n vuegen ../src/vuegen/__main__.py
10+
```
11+
12+
## Pyinstaller options
13+
14+
```bash
15+
What to generate:
16+
-D, --onedir Create a one-folder bundle containing an executable (default)
17+
-F, --onefile Create a one-file bundled executable.
18+
--specpath DIR Folder to store the generated spec file (default: current directory)
19+
-n NAME, --name NAME Name to assign to the bundled app and spec file (default: first script's basename)
20+
Windows and macOS specific options:
21+
-c, --console, --nowindowed
22+
Open a console window for standard i/o (default). On Windows this option has no effect if the first script is a
23+
'.pyw' file.
24+
-w, --windowed, --noconsole
25+
Windows and macOS: do not provide a console window for standard i/o. On macOS this also triggers building a
26+
macOS .app bundle. On Windows this option is automatically set if the first script is a '.pyw' file. This option
27+
is ignored on *NIX systems.
828
```

executables/vuegen.spec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ binaries = []
66
hiddenimports = []
77
tmp_ret = collect_all('pyvis')
88
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
9+
tmp_ret = collect_all('streamlit')
10+
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
11+
912

1013
a = Analysis(
11-
['src/vuegen/__main__.py'],
14+
['../src/vuegen/__main__.py'],
1215
pathex=[],
1316
binaries=binaries,
1417
datas=datas,

src/vuegen/streamlit_reportview.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import os
22
import subprocess
3+
import sys
34
from typing import List
45

56
import pandas as pd
7+
from streamlit.web.cli import _main_run as streamlit_run
68

79
from . import report as r
8-
from .utils import create_folder, is_url, generate_footer
10+
from .utils import create_folder, generate_footer, is_url
11+
12+
13+
def streamlit_run(
14+
file,
15+
args=None,
16+
flag_options=None,
17+
**kwargs
18+
) -> None:
19+
if args is None:
20+
args = []
21+
22+
if flag_options is None:
23+
flag_options = {}
24+
25+
import streamlit.web.bootstrap as bootstrap
26+
from streamlit.runtime.credentials import check_credentials
27+
28+
bootstrap.load_config_options(flag_options=kwargs)
29+
30+
check_credentials()
31+
32+
bootstrap.run(file, False, args, flag_options)
933

1034

1135
class StreamlitReportView(r.WebAppReportView):
@@ -115,9 +139,33 @@ def run_report(self, output_dir: str = SECTIONS_DIR) -> None:
115139
The folder where the report was generated (default is SECTIONS_DIR).
116140
"""
117141
if self.streamlit_autorun:
118-
self.report.logger.info(f"Running '{self.report.title}' {self.report_type} report.")
142+
self.report.logger.info(
143+
f"Running '{self.report.title}' {self.report_type} report."
144+
)
145+
self.report.logger.debug(
146+
f"Running Streamlit report from directory: {output_dir}"
147+
)
148+
# command = [
149+
# sys.executable, # ! will be vuegen main script, not the Python Interpreter
150+
# "-m",
151+
# "streamlit",
152+
# "run",
153+
# os.path.join(output_dir, self.REPORT_MANAG_SCRIPT),
154+
# ]
155+
self.report.logger.debug(sys.executable)
156+
self.report.logger.debug(sys.path)
119157
try:
120-
subprocess.run(["streamlit", "run", os.path.join(output_dir, self.REPORT_MANAG_SCRIPT)], check=True)
158+
# ! streamlit is not known in packaged app
159+
# self.report.logger.debug(f"Running command: {' '.join(command)}")
160+
# subprocess.run(
161+
# command,
162+
# check=True,
163+
# )
164+
target_file = os.path.join(output_dir, self.REPORT_MANAG_SCRIPT)
165+
self.report.logger.debug(
166+
f"Running Streamlit report from file: {target_file}"
167+
)
168+
streamlit_run(target_file)
121169
except KeyboardInterrupt:
122170
print("Streamlit process interrupted.")
123171
except subprocess.CalledProcessError as e:
@@ -747,4 +795,4 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
747795
component_imports.append('df_index = 1')
748796

749797
# Return the list of import statements
750-
return component_imports
798+
return component_imports

0 commit comments

Comments
 (0)