2022-02-19 10:19:07 +01:00
|
|
|
"""Convert a jupytext-compliant format in to a python script
|
|
|
|
and execute it with parsed arguments."""
|
2022-02-07 16:47:03 +01:00
|
|
|
|
|
|
|
import argparse
|
2022-06-15 11:34:45 -07:00
|
|
|
import subprocess
|
2022-02-07 16:47:03 +01:00
|
|
|
import sys
|
2022-06-15 11:34:45 -07:00
|
|
|
import tempfile
|
2022-05-17 10:50:42 +02:00
|
|
|
from pathlib import Path
|
2022-02-07 16:47:03 +01:00
|
|
|
|
|
|
|
import jupytext
|
|
|
|
|
2022-02-19 10:19:07 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Run a jupytext parsable file.")
|
2022-02-07 16:47:03 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--path",
|
2022-02-19 10:19:07 +01:00
|
|
|
help="path to the jupytext-compatible file",
|
2022-02-07 16:47:03 +01:00
|
|
|
)
|
2022-05-17 10:50:42 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--find-recursively",
|
|
|
|
action="store_true",
|
|
|
|
help="if true, will attempt to find path recursively in cwd",
|
|
|
|
)
|
2022-02-07 16:47:03 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
args, remainder = parser.parse_known_args()
|
|
|
|
|
2022-05-17 10:50:42 +02:00
|
|
|
path = Path(args.path)
|
|
|
|
cwd = Path.cwd()
|
|
|
|
if args.find_recursively and not path.exists():
|
|
|
|
path = next((p for p in cwd.rglob("*") if str(p).endswith(args.path)), None)
|
|
|
|
assert path and path.exists()
|
|
|
|
|
|
|
|
with open(path, "r") as f:
|
2022-02-07 16:47:03 +01:00
|
|
|
notebook = jupytext.read(f)
|
|
|
|
|
|
|
|
name = ""
|
|
|
|
with tempfile.NamedTemporaryFile("w", delete=False) as f:
|
|
|
|
jupytext.write(notebook, f, fmt="py:percent")
|
|
|
|
name = f.name
|
|
|
|
|
|
|
|
remainder.insert(0, name)
|
|
|
|
remainder.insert(0, sys.executable)
|
|
|
|
|
|
|
|
# Run the notebook
|
2022-07-09 19:47:21 -07:00
|
|
|
subprocess.run(remainder, check=True)
|