-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add notebook_vars utility with example notebook and unit test depicti…
…ng how it can be ised to construct a pytest fixture with the notebook state after execution (#39) Co-authored-by: Sylwester Arabas <sylwester.arabas@agh.edu.pl>
- Loading branch information
1 parent
5a12ee2
commit a04d4e6
Showing
8 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "51913260-f89a-4237-bc7b-af5a0b1785f9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"a = 44\n", | ||
"b = 666\n", | ||
"c = a + b" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" helper routines for use in smoke tests """ | ||
|
||
from pathlib import Path | ||
|
||
import nbformat | ||
|
||
|
||
def notebook_vars(file: Path, plot: bool): | ||
"""Executes the code from all cells of the Jupyter notebook `file` and | ||
returns a dictionary with the notebook variables. If the `plot` argument | ||
is set to `True`, any code line within the notebook starting with `show_plot(` | ||
(see [open_atmos_jupyter_utils docs](https://pypi.org/p/open_atmos_jupyter_utils)) | ||
is replaced with `pyplot.show() #`, otherwise it is replaced with `pyplot.gca().clear() #` | ||
to match the smoke-test conventions.""" | ||
notebook = nbformat.read(file, nbformat.NO_CONVERT) | ||
context = {} | ||
for cell in notebook.cells: | ||
if cell.cell_type != "markdown": | ||
lines = cell.source.splitlines() | ||
for i, line in enumerate(lines): | ||
if line.strip().startswith("!"): | ||
lines[i] = line.replace("!", "pass #") | ||
if line.strip().startswith("show_plot("): | ||
lines[i] = line.replace( | ||
"show_plot(", | ||
"from matplotlib import pyplot; " | ||
+ ("pyplot.show() #" if plot else "pyplot.gca().clear() #"), | ||
) | ||
|
||
exec("\n".join(lines), context) # pylint: disable=exec-used | ||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
test checking notebook_vars function | ||
""" | ||
|
||
from pathlib import Path | ||
import pytest | ||
|
||
from open_atmos_jupyter_utils import notebook_vars | ||
import examples | ||
|
||
@pytest.fixture(scope="session", name="notebook_variables") | ||
def notebook_variables_fixture(): | ||
"""returns variables from the notebook """ | ||
print(examples.__file__) | ||
return notebook_vars( | ||
file=Path(examples.__file__).parent / "notebook_vars_example.ipynb", | ||
plot=False, | ||
) | ||
|
||
def test_notebook_vars(notebook_variables): | ||
""" checks for a value known only after notebook execution""" | ||
assert notebook_variables["c"] == notebook_variables["a"] + notebook_variables["b"] |