Skip to content

Commit

Permalink
Added background cell test
Browse files Browse the repository at this point in the history
This commit adds a test for `background` cells in interactive execution.
It also solves some issues with `autoawait` cells.
  • Loading branch information
GlassOfWhiskey committed Jun 2, 2023
1 parent 94c5b5d commit 68ca288
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
6 changes: 4 additions & 2 deletions jupyter_workflow/ipython/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]
122 changes: 122 additions & 0 deletions tests/testdata/background_cell.ipynb
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 68ca288

Please sign in to comment.