Test a notebook that has code cells with outputs. Given a code cell and multiple output objects: When ``process_notebook`` is called with ``include_output=True``, Then the outputs should be appended as commented lines under the code.
(write_notebook: WriteNotebookFunc)
| 223 | |
| 224 | |
| 225 | def test_process_notebook_with_output(write_notebook: WriteNotebookFunc) -> None: |
| 226 | """Test a notebook that has code cells with outputs. |
| 227 | |
| 228 | Given a code cell and multiple output objects: |
| 229 | When ``process_notebook`` is called with ``include_output=True``, |
| 230 | Then the outputs should be appended as commented lines under the code. |
| 231 | """ |
| 232 | notebook_content = { |
| 233 | "cells": [ |
| 234 | { |
| 235 | "cell_type": "code", |
| 236 | "source": [ |
| 237 | "import matplotlib.pyplot as plt\n", |
| 238 | "print('my_data')\n", |
| 239 | "my_data = [1, 2, 3, 4, 5]\n", |
| 240 | "plt.plot(my_data)\n", |
| 241 | "my_data", |
| 242 | ], |
| 243 | "outputs": [ |
| 244 | {"output_type": "stream", "text": ["my_data"]}, |
| 245 | {"output_type": "execute_result", "data": {"text/plain": ["[1, 2, 3, 4, 5]"]}}, |
| 246 | {"output_type": "display_data", "data": {"text/plain": ["<Figure size 640x480 with 1 Axes>"]}}, |
| 247 | ], |
| 248 | }, |
| 249 | ], |
| 250 | } |
| 251 | |
| 252 | nb_path = write_notebook("with_output.ipynb", notebook_content) |
| 253 | with_output = process_notebook(nb_path, include_output=True) |
| 254 | without_output = process_notebook(nb_path, include_output=False) |
| 255 | |
| 256 | expected_source = ( |
| 257 | "# Jupyter notebook converted to Python script.\n\n" |
| 258 | "import matplotlib.pyplot as plt\n" |
| 259 | "print('my_data')\n" |
| 260 | "my_data = [1, 2, 3, 4, 5]\n" |
| 261 | "plt.plot(my_data)\n" |
| 262 | "my_data\n" |
| 263 | ) |
| 264 | |
| 265 | expected_output = "# Output:\n# my_data\n# [1, 2, 3, 4, 5]\n# <Figure size 640x480 with 1 Axes>\n" |
| 266 | |
| 267 | expected_combined = expected_source + expected_output |
| 268 | |
| 269 | assert with_output == expected_combined, "Should include source code and comment-ified output." |
| 270 | assert without_output == expected_source, "Should include only the source code without output." |
nothing calls this directly
no test coverage detected