Skip to content

Commit

Permalink
test executed notebooks... suspect it might not work on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
mccalluc committed Sep 24, 2024
1 parent 0ea41b6 commit e00cb71
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 14 deletions.
14 changes: 9 additions & 5 deletions dp_creator_ii/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess


def convert_py_to_nb(python_str):
def convert_py_to_nb(python_str, execute=False):
'''
Given Python code as a string, returns a notebook as a string.
Calls jupytext as a subprocess:
Expand All @@ -14,10 +14,14 @@ def convert_py_to_nb(python_str):
py_path = temp_dir_path / 'input.py'
py_path.write_text(python_str)
nb_path = temp_dir_path / 'output.ipynb'
argv = [
'jupytext',
'--to', 'ipynb', # Target format
'--output', nb_path.absolute(), # Output
] + (['--execute'] if execute else []) + [
py_path.absolute() # Input
]
subprocess.run(
['jupytext',
'--to', 'ipynb', # Target format
'--output', nb_path.absolute(), # Output
py_path.absolute()], # Input
argv,
check=True)
return nb_path.read_text()
69 changes: 69 additions & 0 deletions dp_creator_ii/tests/fixtures/fake-executed.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d68beaaf",
"metadata": {},
"source": [
"Introduction"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c4a24010",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-24T22:01:27.184792Z",
"iopub.status.busy": "2024-09-24T22:01:27.184540Z",
"iopub.status.idle": "2024-09-24T22:01:27.188766Z",
"shell.execute_reply": "2024-09-24T22:01:27.188362Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"print(2+2)"
]
},
{
"cell_type": "markdown",
"id": "aa6d5643",
"metadata": {},
"source": [
"Conclusion"
]
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all"
},
"kernelspec": {
"display_name": "kernel_name",
"language": "python",
"name": "kernel_name"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
26 changes: 23 additions & 3 deletions dp_creator_ii/tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@
from dp_creator_ii.converters import convert_py_to_nb


def norm_nb(nb_str):
normed_nb_str = nb_str
normed_nb_str = re.sub(
r'"id": "[^"]+"',
'"id": "12345678"',
normed_nb_str)
normed_nb_str = re.sub(
r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z',
'2024-01-01T00:00:00.000000Z',
normed_nb_str)
return normed_nb_str


def test_convert_py_to_nb():
fixtures_path = Path('dp_creator_ii/tests/fixtures')
python_str = (fixtures_path / 'fake.py').read_text()
actual_nb_str = convert_py_to_nb(python_str)
expected_nb_str = (fixtures_path / 'fake.ipynb').read_text()

def norm_nb(nb_str):
normed_nb_str = re.sub(r'"id": "[^"]+"', '"id": "12345678"', nb_str)
return normed_nb_str
normed_actual_nb_str = norm_nb(actual_nb_str)
normed_expected_nb_str = norm_nb(expected_nb_str)
assert normed_actual_nb_str == normed_expected_nb_str


def test_convert_py_to_nb_execute():
fixtures_path = Path('dp_creator_ii/tests/fixtures')
python_str = (fixtures_path / 'fake.py').read_text()
actual_nb_str = convert_py_to_nb(python_str, execute=True)
expected_nb_str = (fixtures_path / 'fake-executed.ipynb').read_text()

normed_actual_nb_str = norm_nb(actual_nb_str)
normed_expected_nb_str = norm_nb(expected_nb_str)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies = [
"shinywidgets",
"opendp[polars]",
"jupytext",
"jupyter-client",
"nbconvert",
"ipykernel",
]

[project.scripts]
Expand Down
5 changes: 5 additions & 0 deletions requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ scipy<1.14

# Conversion:
jupytext
jupyter-client
nbconvert
ipykernel
# May also require:
# python -m ipykernel install --name kernel_name --user

# Shiny:
shiny
Expand Down
89 changes: 83 additions & 6 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ anyio==4.4.0
# watchfiles
appdirs==1.4.4
# via shiny
appnope==0.1.4
# via ipykernel
asgiref==3.8.1
# via shiny
asttokens==2.4.1
Expand All @@ -20,6 +22,10 @@ attrs==24.2.0
# referencing
autoflake==2.3.1
# via -r requirements-dev.in
beautifulsoup4==4.12.3
# via nbconvert
bleach==6.1.0
# via nbconvert
build==1.2.2
# via pip-tools
certifi==2024.8.30
Expand All @@ -32,11 +38,17 @@ click==8.1.7
# shiny
# uvicorn
comm==0.2.2
# via ipywidgets
# via
# ipykernel
# ipywidgets
coverage==7.6.1
# via -r requirements-dev.in
debugpy==1.8.6
# via ipykernel
decorator==5.1.1
# via ipython
defusedxml==0.7.1
# via nbconvert
docutils==0.21.2
# via flit
executing==2.1.0
Expand All @@ -59,22 +71,39 @@ idna==3.10
# requests
iniconfig==2.0.0
# via pytest
ipykernel==6.29.5
# via -r requirements-dev.in
ipython==8.18.0
# via ipywidgets
# via
# ipykernel
# ipywidgets
ipywidgets==8.1.5
# via shinywidgets
jedi==0.19.1
# via ipython
jinja2==3.1.4
# via nbconvert
joblib==1.4.2
# via scikit-learn
jsonschema==4.23.0
# via nbformat
jsonschema-specifications==2023.12.1
# via jsonschema
jupyter-client==8.6.3
# via
# -r requirements-dev.in
# ipykernel
# nbclient
jupyter-core==5.7.2
# via
# ipykernel
# jupyter-client
# nbclient
# nbconvert
# nbformat
# shinywidgets
jupyterlab-pygments==0.3.0
# via nbconvert
jupyterlab-widgets==3.0.13
# via ipywidgets
jupytext==1.16.4
Expand All @@ -86,8 +115,14 @@ markdown-it-py==3.0.0
# jupytext
# mdit-py-plugins
# shiny
markupsafe==2.1.5
# via
# jinja2
# nbconvert
matplotlib-inline==0.1.7
# via ipython
# via
# ipykernel
# ipython
mccabe==0.7.0
# via flake8
mdit-py-plugins==0.4.2
Expand All @@ -96,12 +131,23 @@ mdit-py-plugins==0.4.2
# shiny
mdurl==0.1.2
# via markdown-it-py
mistune==3.0.2
# via nbconvert
mypy==1.11.2
# via -r requirements-dev.in
mypy-extensions==1.0.0
# via mypy
nbclient==0.10.0
# via nbconvert
nbconvert==7.16.4
# via -r requirements-dev.in
nbformat==5.10.4
# via jupytext
# via
# jupytext
# nbclient
# nbconvert
nest-asyncio==1.6.0
# via ipykernel
numpy==1.26.4
# via
# opendp
Expand All @@ -115,9 +161,13 @@ packaging==24.1
# via
# build
# htmltools
# ipykernel
# jupytext
# nbconvert
# pytest
# shiny
pandocfilters==1.5.1
# via nbconvert
parso==0.8.4
# via jedi
pexpect==4.9.0
Expand All @@ -135,6 +185,8 @@ prompt-toolkit==3.0.36
# ipython
# questionary
# shiny
psutil==6.0.0
# via ipykernel
ptyprocess==0.7.0
# via pexpect
pure-eval==0.2.3
Expand All @@ -148,19 +200,27 @@ pyflakes==3.2.0
# autoflake
# flake8
pygments==2.18.0
# via ipython
# via
# ipython
# nbconvert
pyproject-hooks==1.1.0
# via
# build
# pip-tools
pytest==8.3.3
# via -r requirements-dev.in
python-dateutil==2.9.0.post0
# via shinywidgets
# via
# jupyter-client
# shinywidgets
python-multipart==0.0.9
# via shiny
pyyaml==6.0.2
# via jupytext
pyzmq==26.2.0
# via
# ipykernel
# jupyter-client
questionary==2.0.1
# via shiny
randomgen==2.0.1
Expand Down Expand Up @@ -190,24 +250,37 @@ shinywidgets==0.3.3
six==1.16.0
# via
# asttokens
# bleach
# python-dateutil
sniffio==1.3.1
# via anyio
soupsieve==2.6
# via beautifulsoup4
stack-data==0.6.3
# via ipython
starlette==0.38.5
# via shiny
threadpoolctl==3.5.0
# via scikit-learn
tinycss2==1.3.0
# via nbconvert
tomli-w==1.0.0
# via flit
tornado==6.4.1
# via
# ipykernel
# jupyter-client
traitlets==5.14.3
# via
# comm
# ipykernel
# ipython
# ipywidgets
# jupyter-client
# jupyter-core
# matplotlib-inline
# nbclient
# nbconvert
# nbformat
typing-extensions==4.12.2
# via
Expand All @@ -224,6 +297,10 @@ watchfiles==0.24.0
# via shiny
wcwidth==0.2.13
# via prompt-toolkit
webencodings==0.5.1
# via
# bleach
# tinycss2
websockets==13.0.1
# via shiny
wheel==0.44.0
Expand Down

0 comments on commit e00cb71

Please sign in to comment.