Skip to content

Commit eaa6fb2

Browse files
esantorellafacebook-github-bot
authored andcommitted
Use papermill for running tutorials (#1706)
Summary: ## Motivation We are using nbconvert to run tutorials. nbconvert is not really made for this use case, but papermill is, so we have some handwritten code than can be handled by papermill. With papermill, we can go a bit further and use SMOKE_TEST as a [parameter](https://papermill.readthedocs.io/en/latest/usage-parameterize.html) rather than an environment variable. That would make it easy for people to work with the tutorials as notebooks. Pull Request resolved: #1706 Test Plan: Ran tutorials locally and made sure smoke-test flag was getting used appropriately. ## Related pull requests Enabling papermill will make #1703, which automates running a notebook, a bit easier. Reviewed By: saitcakmak Differential Revision: D43631568 Pulled By: esantorella fbshipit-source-id: e7bfeb68e221fff4f1633af8deb9a11d1ff1c0e6
1 parent af93f40 commit eaa6fb2

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

scripts/run_tutorials.py

+3-22
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
import datetime
1111
import os
1212
import subprocess
13-
import tempfile
1413
import time
1514
from pathlib import Path
1615
from subprocess import CalledProcessError
1716
from typing import Any, Dict, Optional, Tuple
1817

19-
import nbformat
2018
import pandas as pd
2119
from memory_profiler import memory_usage
22-
from nbconvert import PythonExporter
2320

2421

2522
IGNORE_ALWAYS = { # ignored in smoke tests and full runs
@@ -64,33 +61,18 @@ def get_output_file_path(smoke_test: bool) -> str:
6461
return fname
6562

6663

67-
def parse_ipynb(file: Path) -> str:
68-
with open(file, "r") as nb_file:
69-
nb_str = nb_file.read()
70-
nb = nbformat.reads(nb_str, nbformat.NO_CONVERT)
71-
exporter = PythonExporter()
72-
script, _ = exporter.from_notebook_node(nb)
73-
return script
74-
75-
7664
def run_script(
77-
script: str, timeout_minutes: int, env: Optional[Dict[str, str]] = None
65+
tutorial: Path, timeout_minutes: int, env: Optional[Dict[str, str]] = None
7866
) -> None:
79-
# need to keep the file around & close it so subprocess does not run into I/O issues
80-
with tempfile.NamedTemporaryFile(delete=False) as tf:
81-
tf_name = tf.name
82-
with open(tf_name, "w") as tmp_script:
83-
tmp_script.write(script)
8467
if env is not None:
8568
env = {**os.environ, **env}
8669
run_out = subprocess.run(
87-
["ipython", tf_name],
70+
["papermill", tutorial, "|"],
8871
capture_output=True,
8972
text=True,
9073
env=env,
9174
timeout=timeout_minutes * 60,
9275
)
93-
os.remove(tf_name)
9476
return run_out
9577

9678

@@ -102,13 +84,12 @@ def run_tutorial(
10284
them as a string, and returns runtime and memory information as a dict.
10385
"""
10486
timeout_minutes = 5 if smoke_test else 30
105-
script = parse_ipynb(tutorial)
10687
tic = time.monotonic()
10788
print(f"Running tutorial {tutorial.name}.")
10889
env = {"SMOKE_TEST": "True"} if smoke_test else None
10990
try:
11091
mem_usage, run_out = memory_usage(
111-
(run_script, (script, timeout_minutes), {"env": env}),
92+
(run_script, (tutorial, timeout_minutes), {"env": env}),
11293
retval=True,
11394
include_children=True,
11495
)

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"kaleido",
2424
"matplotlib",
2525
"memory_profiler",
26+
"papermill",
2627
"pykeops",
2728
"torchvision",
2829
]

0 commit comments

Comments
 (0)