Skip to content

Commit

Permalink
Merge pull request #47 from TheoMF/ipynb_load_snippet
Browse files Browse the repository at this point in the history
test on ipynbs use generate action first
  • Loading branch information
nim65s authored Aug 16, 2023
2 parents c92a1b7 + 929e705 commit 65030f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 13 additions & 1 deletion gepetuto/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def generate_from_id(tp_id: int):
generate_ipynb(ipynb, folder)


def generate_ipynb(ipynb, folder): # noqa: C901
def generate_ipynb(ipynb, folder, force_load=False): # noqa: C901
"""Cut python files in bits loadable by ipython."""
LOG.info(f"processing '{ipynb}' with scripts in '{folder}'")
with ipynb.open() as f:
Expand Down Expand Up @@ -67,6 +67,18 @@ def generate_ipynb(ipynb, folder): # noqa: C901
f"# %load {dest}\n",
*content,
]
elif cell["source"][0].endswith(f"%load_snippet {dest}"):
data["cells"][cell_number]["source"] = [
f"# %load {dest}\n",
*content,
]
elif force_load and cell["source"][0].endswith(
f"%do_not_load_snippet {dest}",
):
data["cells"][cell_number]["source"] = [
f"# %load {dest}\n",
*content,
]
# if f'%do_not_load {dest}' in cell['source'][0]:
# data['cells'][cell_number]['source'] \
# = [f'%do_not_load {dest}\n']
Expand Down
7 changes: 6 additions & 1 deletion gepetuto/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pathlib import Path
from subprocess import check_call

from .generate import generate_ipynb

LOG = logging.getLogger("gepetuto.test")


Expand Down Expand Up @@ -38,8 +40,11 @@ def get_ipynbs(files):

def check_ipynb(ipynb, python_interpreter):
"""Check .ipynb files from given tp_number."""
check_call(["jupyter", "nbconvert", "--to", "script", f"{ipynb}"])
prefix = str(ipynb).split("-")[0]
tp_path = Path(f"tp{prefix}" if prefix.isdecimal() else prefix)
if tp_path.exists():
generate_ipynb(ipynb, tp_path, True)
check_call(["jupyter", "nbconvert", "--to", "script", f"{ipynb}"])
converted_ipynb = next(Path().glob(f"{prefix}-*.py"))
LOG.debug(f"Checking temporary file {converted_ipynb}")
check_call([python_interpreter, converted_ipynb])
Expand Down

0 comments on commit 65030f7

Please sign in to comment.