Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel flag #58

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ pytest --nbmake **/*ipynb

## Allow errors and Configure Cell Timeouts

You can configure the notebook run timeout with a the following pytest flag:
You can configure the cell timeout with the following pytest flag:

```sh
pytest --nbmake --nbmake-timeout=3000 # allows each notebook 3000 seconds to finish
pytest --nbmake --nbmake-timeout=3000 # allows each cell 3000 seconds to finish
```

Each notebook can also be separately overidden to allow errors and fail if running exceeds a timeout.

This configuration must be placed in the notebook's top-level metadata (not cell-level metadata).
This configuration must be placed in the notebook's **top-level metadata** (not cell-level metadata).

Your notebook should look like this:

Expand All @@ -50,10 +50,17 @@ Your notebook should look like this:
}
```

## Override Notebook Kernels when Testing

Regardless of the kernel configured in the notebook JSON, you can force nbmake to use a specific kernel when testing:

```
pytest --nbmake --nbmake-kernel=mycustomkernel
```

## Add Missing Jupyter Kernel to Your CI Environment

If you are using a kernel name other than the default ‘python3’. You will see an error message when executing your notebooks in a fresh CI environment: `Error - No such kernel: 'mycustomkernel'`
If you are not using the flag above and are using a kernel name other than the default ‘python3’, you will see an error message when executing your notebooks in a fresh CI environment: `Error - No such kernel: 'mycustomkernel'`

Use ipykernel to install the custom kernel:

Expand Down
7 changes: 7 additions & 0 deletions src/nbmake/nb_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def __init__(
filename: Path,
default_timeout: int,
verbose: bool = False,
kernel: Optional[str] = None,
) -> None:
self.filename = filename
self.verbose = verbose
self.default_timeout = default_timeout
self.kernel = kernel

def execute(
self,
Expand All @@ -46,12 +48,17 @@ def execute(

error: Optional[NotebookError] = None

extra_kwargs = {}
if self.kernel:
extra_kwargs["kernel_name"] = self.kernel

try:
c = NotebookClient(
nb,
timeout=timeout,
allow_errors=allow_errors,
record_timing=True,
**extra_kwargs,
)
c.execute(cwd=self.filename.parent)
except CellExecutionError:
Expand Down
1 change: 1 addition & 0 deletions src/nbmake/pytest_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def runtest(self):
source,
option.nbmake_timeout,
verbose=bool(option.verbose),
kernel=option.nbmake_kernel,
)

res: NotebookResult = run.execute()
Expand Down
8 changes: 7 additions & 1 deletion src/nbmake/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ def pytest_addoption(parser: Any):
group.addoption(
"--nbmake-timeout",
action="store",
help="Sets the default timeout for a notebook (seconds)",
help="Sets the default cell timeout (seconds)",
default=300,
type=int,
)
group.addoption(
"--nbmake-kernel",
action="store",
help="Overrides the kernel used for all notebooks",
type=str,
)


def pytest_collect_file(path: str, parent: Any) -> Optional[Any]:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,21 @@ def test_when_explicit_metadata_then_ignore_timeout(testdir: Testdir):

hook_recorder = testdir.inline_run("--nbmake", "--nbmake-timeout=1")
assert hook_recorder.ret == ExitCode.OK


def test_when_kernel_passed_then_override(testdir: Testdir):
write_nb(
passing_nb,
Path(f"x.ipynb"),
metadata={
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "blah",
}
},
)

hook_recorder = testdir.inline_run("--nbmake", "--nbmake-kernel=python3")

assert hook_recorder.ret == ExitCode.OK