Skip to content

Commit a13e784

Browse files
committed
✨ get quarto html output from bundle
- use quarto (pandoc?) to convert qmd to ipynb - use nbconvert and a copied Python executable to execute notebook - use quarto (pandoc?) ot convert executed ipynb to desired format
1 parent 1e236f8 commit a13e784

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

.github/workflows/cdci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ jobs:
153153
run: |
154154
cd gui
155155
pyinstaller -n vuegen_gui -D --collect-all pyvis --collect-all streamlit --collect-all st_aggrid --collect-all customtkinter --add-data ../docs/example_data/Basic_example_vuegen_demo_notebook:example_data/Basic_example_vuegen_demo_notebook app.py
156+
python copy_python_executable.py
156157
- name: Upload executable
157158
uses: actions/upload-artifact@v4
158159
with:

gui/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,14 @@ Windows and macOS specific options:
6060
macOS .app bundle. On Windows this option is automatically set if the first script is a '.pyw' file. This option
6161
is ignored on *NIX systems.
6262
```
63+
64+
## Quarto notebook execution
65+
66+
- add python exe to bundle as suggested [on stackoverflow](https://stackoverflow.com/a/72639099/9684872)
67+
- use [copy_python_executable.py](copy_python_executable.py) to copy the python executable to the bundle after PyInstaller is done
68+
69+
Basic workflow for bundle:
70+
71+
1. use quarto (pandoc?) to convert qmd to ipynb
72+
1. use nbconvert and a copied Python executable to execute notebook
73+
1. use quarto (pandoc?) ot convert executed ipynb to desired format

gui/copy_python_executable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import shutil
2+
import sys
3+
from pathlib import Path
4+
5+
python_exe = Path(sys.executable).with_stem("python")
6+
shutil.copy(python_exe, "dist/vuegen_gui/_internal")

src/vuegen/quarto_reportview.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ def run_report(self, output_dir: str = BASE_DIR) -> None:
199199
raise
200200
else:
201201
quarto_path = Path(sys._MEIPASS) / "quarto_cli" / "bin" / "quarto"
202-
if sys.platform == "win32":
203-
# ! to check
204-
quarto_path = self.quarto_path.with_suffix(".exe")
202+
_sys_exe = sys.executable
203+
# set executable to the bundled python (manually added to bundle)
204+
sys.executable = str(Path(sys._MEIPASS) / "python")
205205
self.report.logger.info(f"quarto_path: {quarto_path}")
206206

207207
args = [f"{quarto_path}", "convert", file_path_to_qmd]
@@ -213,13 +213,25 @@ def run_report(self, output_dir: str = BASE_DIR) -> None:
213213
f"Converted '{self.report.title}' '{self.report_type}' report to Jupyter Notebook after execution"
214214
)
215215
notebook_filename = Path(file_path_to_qmd).with_suffix(".ipynb")
216-
# ipynb will not be executed per default
217-
# ! check if execution works although qmd cannot be executed...
216+
# quarto does not try to execute ipynb files, just render them
217+
# execute manually using bundled python binary
218+
# https://nbconvert.readthedocs.io/en/latest/execute_api.html
219+
import nbformat
220+
from nbconvert.preprocessors import ExecutePreprocessor
221+
222+
with open(notebook_filename, encoding="utf-8") as f:
223+
nb = nbformat.read(f, as_version=4)
224+
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
225+
nb, _ = ep.preprocess(nb, {"metadata": {"path": "./"}})
226+
with open(notebook_filename, "w", encoding="utf-8") as f:
227+
nbformat.write(nb, f)
228+
# quarto does not try execute ipynb files per default, just render these
218229
args = [f"{quarto_path}", "render", notebook_filename]
219230
subprocess.run(
220231
args,
221232
check=True,
222233
)
234+
sys.executable = _sys_exe
223235

224236
def _create_yaml_header(self) -> str:
225237
"""

0 commit comments

Comments
 (0)