diff --git a/README.md b/README.md index d2758da..940b8bb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/asekuro/common.py b/asekuro/common.py index 41d112c..cfd17e1 100644 --- a/asekuro/common.py +++ b/asekuro/common.py @@ -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)}") diff --git a/tests/notebooks/commented-load.ipynb b/tests/notebooks/commented-load.ipynb new file mode 100644 index 0000000..43bd444 --- /dev/null +++ b/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 +} diff --git a/tests/notebooks/nbcontent-2.py b/tests/notebooks/nbcontent-2.py new file mode 100644 index 0000000..84fd19b --- /dev/null +++ b/tests/notebooks/nbcontent-2.py @@ -0,0 +1 @@ +another_test_value = 42 \ No newline at end of file diff --git a/tests/test_commandline.py b/tests/test_commandline.py index 3b24271..c3112eb 100644 --- a/tests/test_commandline.py +++ b/tests/test_commandline.py @@ -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