Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
95 changes: 95 additions & 0 deletions test/data/noparamtest.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions test/data/paramtest.ipynb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,17 @@ 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"
)
assert script_creator.nb_params.params == {
"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 == {}
7 changes: 5 additions & 2 deletions xcengine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down