diff --git a/CHANGES.md b/CHANGES.md index f4f245e..e100033 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,8 @@ ## Changes in 0.1.1 (in development) * Improve automated parameter extraction (#9) +* Handle notebooks without parameters (#11) +* Handle non-code cells (#12) ## Changes in 0.1.0 diff --git a/test/data/noparamtest.ipynb b/test/data/noparamtest.ipynb new file mode 100644 index 0000000..488aa3c --- /dev/null +++ b/test/data/noparamtest.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4aa5a3fc-c945-433f-9968-19705027ddcf", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "A notebook without any parameters or outputs." + ] + }, + { + "cell_type": "raw", + "id": "d46c55b7-9b5c-4cf8-99e5-5fd1b89dd6f0", + "metadata": { + "editable": true, + "raw_mimetype": "", + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "A raw cell." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7182cbd5-c34c-409b-8cfd-228d24f50570", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 + 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d2f6253-047a-4368-a2eb-d6a210fed778", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [] + } + ], + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/test/data/paramtest.ipynb b/test/data/paramtest.ipynb index 1daf501..1b910dd 100644 --- a/test/data/paramtest.ipynb +++ b/test/data/paramtest.ipynb @@ -1,5 +1,19 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "6445ff96-ab4e-4b05-aed9-041402cfa6e7", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "This is a Markdown cell." + ] + }, { "cell_type": "code", "execution_count": null, @@ -18,6 +32,21 @@ "my_constant = math.floor(3.5)" ] }, + { + "cell_type": "raw", + "id": "325a90ed-c291-4277-9c0f-99c5543ac218", + "metadata": { + "editable": true, + "raw_mimetype": "", + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "This is a raw cell." + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/test/test_core.py b/test/test_core.py index 0023daf..702141a 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -119,7 +119,7 @@ def test_chunk_stream(): assert BufferedReader(chunk_stream).read() == expected -def test_script_creator_init(): +def test_script_creator_init_with_parameters(): script_creator = ScriptCreator( pathlib.Path(__file__).parent / "data" / "paramtest.ipynb" ) @@ -127,3 +127,9 @@ def test_script_creator_init(): "parameter_1": (int, 6), "parameter_2": (str, "default value"), } + +def test_script_creator_init_no_parameters(): + script_creator = ScriptCreator( + pathlib.Path(__file__).parent / "data" / "noparamtest.ipynb" + ) + assert script_creator.nb_params.params == {} diff --git a/xcengine/core.py b/xcengine/core.py index 49a26a0..cd2cfa9 100755 --- a/xcengine/core.py +++ b/xcengine/core.py @@ -39,7 +39,7 @@ class ScriptCreator: """Turn a Jupyter notebook into a set of scripts""" notebook: nbformat.NotebookNode - nb_params: NotebookParameters + nb_params: NotebookParameters = NotebookParameters({}) def __init__(self, nb_path: pathlib.Path): with open(nb_path) as fh: @@ -76,7 +76,10 @@ def process_params_cell(self) -> None: setup_code = "\n".join( map( operator.attrgetter("source"), - self.notebook.cells[:params_cell_index], + filter( + lambda c: c.cell_type == "code", + self.notebook.cells[:params_cell_index], + ), ) ) self.nb_params = NotebookParameters.from_code(