Skip to content

Commit

Permalink
Merge pull request #41 from godatadriven/commented-out-load
Browse files Browse the repository at this point in the history
Support commented-out %load magics
  • Loading branch information
cobac committed Aug 1, 2023
2 parents 43aec38 + 1c6d522 commit 9f7655f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -13,7 +13,8 @@ features we wanted in this app is that we had a nice way to:

1. Clear notebook cells
2. Be able to confirm that the notebook can run without errors.
3. Be able to deal with solutions via `%load` magic.
3. Be able to deal with solutions via `%load` magic.
- Including cells starting with `%load`, and commented-out versions `# %load` and `#%load`.

# Quick-Start

Expand Down
2 changes: 1 addition & 1 deletion asekuro/__init__.py
@@ -1 +1 @@
__version__ = "0.2.0"
__version__ = "0.2.1"
10 changes: 8 additions & 2 deletions asekuro/common.py
Expand Up @@ -108,10 +108,16 @@ def make_testable_notebook(nbpath, remove_meta=True):
if cell['cell_type'] == 'code':
if '%load ' in cell['source']:
logger.info(f'found %load-magic in cell with id={cell["execution_count"]}')
logger.info(cell['source'])
py_path = cell['source'].replace('%load ', '')
logger.info(f'original cell content: {cell["source"]}')
py_path = (
cell['source']
.replace('# %load ', '')
.replace('#%load ', '')
.replace('%load ', '')
)
with open(py_path, 'r') as f:
cell['source'] = f.read()
logger.info(f'after loading cell content: {cell["source"]}')
nbformat.write(notebook, open(_testfile(nbpath=nbpath), mode='w'))
logger.info(f"wrote notebook ready for testing over at {_testfile(nbpath=nbpath)}")

Expand Down
65 changes: 65 additions & 0 deletions tests/notebooks/commented-load.ipynb
@@ -0,0 +1,65 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "6a355e27-c054-45f8-b779-b3c804ec6dee",
"metadata": {},
"outputs": [],
"source": [
"# %load nbcontent.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e4dd49f-abf1-4f0a-96d8-06c60d5ab24c",
"metadata": {},
"outputs": [],
"source": [
"test_value == 42"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee9c81cb-e1b4-40ed-9c98-32973ad4c3ea",
"metadata": {},
"outputs": [],
"source": [
"#%load nbcontent-2.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0131b124-1e5d-4d14-a9ab-79e4968460ca",
"metadata": {},
"outputs": [],
"source": [
"another_test_value == 42"
]
}
],
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions tests/notebooks/nbcontent-2.py
@@ -0,0 +1 @@
another_test_value = 42
4 changes: 4 additions & 0 deletions tests/test_commandline.py
Expand Up @@ -31,6 +31,10 @@ def test_data_nb(self):
status = subprocess.call(['asekuro', 'test', 'data-nb.ipynb'])
assert status == 0

def test_commented_load(self):
status = subprocess.call(['asekuro', 'test', 'commented-load.ipynb'])
assert status == 0

def test_bad_bn(self):
status = subprocess.call(['asekuro', 'test', 'bad-nb.ipynb'])
assert status == 2
Expand Down

0 comments on commit 9f7655f

Please sign in to comment.