From 68ca288613e49ee9eecfb1cb0a2d8082ec26521f Mon Sep 17 00:00:00 2001 From: GlassOfWhiskey Date: Fri, 2 Jun 2023 23:34:52 +0200 Subject: [PATCH] Added background cell test This commit adds a test for `background` cells in interactive execution. It also solves some issues with `autoawait` cells. --- jupyter_workflow/ipython/shell.py | 6 +- tests/test_notebook.py | 8 ++ tests/testdata/background_cell.ipynb | 122 +++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 tests/testdata/background_cell.ipynb diff --git a/jupyter_workflow/ipython/shell.py b/jupyter_workflow/ipython/shell.py index b7d49f7..60267c1 100644 --- a/jupyter_workflow/ipython/shell.py +++ b/jupyter_workflow/ipython/shell.py @@ -145,7 +145,7 @@ async def _run_with_streamflow( name=cell_name, code=ast_nodes, compiler=compiler, metadata=cell_config ) # Create a notebook with a single cell - notebook = JupyterNotebook([cell]) + notebook = JupyterNotebook([cell], autoawait=self.autoawait) # Translate notebook into workflow translator = JupyterNotebookTranslator(context=self.context) workflow = await translator.translate(notebook=notebook, user_ns=self.user_ns) @@ -312,7 +312,9 @@ async def run_workflow(self, notebook): translator = JupyterNotebookTranslator(context=self.context) workflow = await translator.translate( notebook=JupyterNotebook( - cells=jupyter_cells, metadata=notebook.get("metadata") + cells=jupyter_cells, + autoawait=self.autoawait, + metadata=notebook.get("metadata"), ), user_ns=self.user_ns, ) diff --git a/tests/test_notebook.py b/tests/test_notebook.py index c8c220c..891c5a6 100644 --- a/tests/test_notebook.py +++ b/tests/test_notebook.py @@ -68,3 +68,11 @@ def test_scatter_and_non_scatter_sequences(tb): def test_scatter_and_non_scatter_sequences_workflow(tb): assert tb.cell_execute_result(3) == [{"text/plain": "[4, 5, 6, 7]"}] assert tb.cell_execute_result(4) == [{"text/plain": "[4, 5, 6, 7]"}] + + +@testflow( + get_file("background_cell.ipynb"), + execute=True, +) +def test_background_cell(tb): + assert tb.cell_execute_result(3) == [{"text/plain": "Hello, Anonymous"}] diff --git a/tests/testdata/background_cell.ipynb b/tests/testdata/background_cell.ipynb new file mode 100644 index 0000000..205cad8 --- /dev/null +++ b/tests/testdata/background_cell.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "17332601", + "metadata": {}, + "outputs": [], + "source": [ + "import aiohttp\n", + "import aiohttp.web\n", + "import asyncio" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f45ca1f", + "metadata": {}, + "outputs": [], + "source": [ + "async def handle(request):\n", + " name = request.match_info.get(\"name\", \"Anonymous\")\n", + " text = f\"Hello, {name}\"\n", + " return aiohttp.web.Response(text=text)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9649c2ef", + "metadata": { + "workflow": { + "step": { + "autoin": true, + "background": true, + "in": [], + "out": [] + }, + "version": "v1.0" + } + }, + "outputs": [], + "source": [ + "event = asyncio.Event()\n", + "\n", + "\n", + "async def stop(request):\n", + " event.set()\n", + "\n", + "\n", + "app = aiohttp.web.Application()\n", + "app.add_routes(\n", + " [\n", + " aiohttp.web.get(\"/\", handle),\n", + " aiohttp.web.get(\"/stop\", stop),\n", + " aiohttp.web.get(\"/{name}\", handle),\n", + " ]\n", + ")\n", + "\n", + "runner = aiohttp.web.AppRunner(app)\n", + "await runner.setup()\n", + "site = aiohttp.web.TCPSite(runner, \"localhost\", 43210)\n", + "await site.start()\n", + "\n", + "_ = await event.wait()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a293b211", + "metadata": {}, + "outputs": [], + "source": [ + "async def client():\n", + " async with aiohttp.ClientSession() as session:\n", + " async with session.get(\"http://localhost:43210/\") as response:\n", + " text = await response.text()\n", + " return text\n", + "\n", + "\n", + "await client()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fbf301c1", + "metadata": {}, + "outputs": [], + "source": [ + "async with aiohttp.ClientSession() as session:\n", + " try:\n", + " await session.get(\"http://localhost:43210/stop\")\n", + " except aiohttp.ServerDisconnectedError:\n", + " pass" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Jupyter Workflow", + "language": "python", + "name": "jupyter-workflow" + }, + "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.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}