Skip to content

Commit 68ca288

Browse files
Added background cell test
This commit adds a test for `background` cells in interactive execution. It also solves some issues with `autoawait` cells.
1 parent 94c5b5d commit 68ca288

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

jupyter_workflow/ipython/shell.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async def _run_with_streamflow(
145145
name=cell_name, code=ast_nodes, compiler=compiler, metadata=cell_config
146146
)
147147
# Create a notebook with a single cell
148-
notebook = JupyterNotebook([cell])
148+
notebook = JupyterNotebook([cell], autoawait=self.autoawait)
149149
# Translate notebook into workflow
150150
translator = JupyterNotebookTranslator(context=self.context)
151151
workflow = await translator.translate(notebook=notebook, user_ns=self.user_ns)
@@ -312,7 +312,9 @@ async def run_workflow(self, notebook):
312312
translator = JupyterNotebookTranslator(context=self.context)
313313
workflow = await translator.translate(
314314
notebook=JupyterNotebook(
315-
cells=jupyter_cells, metadata=notebook.get("metadata")
315+
cells=jupyter_cells,
316+
autoawait=self.autoawait,
317+
metadata=notebook.get("metadata"),
316318
),
317319
user_ns=self.user_ns,
318320
)

tests/test_notebook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,11 @@ def test_scatter_and_non_scatter_sequences(tb):
6868
def test_scatter_and_non_scatter_sequences_workflow(tb):
6969
assert tb.cell_execute_result(3) == [{"text/plain": "[4, 5, 6, 7]"}]
7070
assert tb.cell_execute_result(4) == [{"text/plain": "[4, 5, 6, 7]"}]
71+
72+
73+
@testflow(
74+
get_file("background_cell.ipynb"),
75+
execute=True,
76+
)
77+
def test_background_cell(tb):
78+
assert tb.cell_execute_result(3) == [{"text/plain": "Hello, Anonymous"}]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "17332601",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import aiohttp\n",
11+
"import aiohttp.web\n",
12+
"import asyncio"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "4f45ca1f",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"async def handle(request):\n",
23+
" name = request.match_info.get(\"name\", \"Anonymous\")\n",
24+
" text = f\"Hello, {name}\"\n",
25+
" return aiohttp.web.Response(text=text)"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"id": "9649c2ef",
32+
"metadata": {
33+
"workflow": {
34+
"step": {
35+
"autoin": true,
36+
"background": true,
37+
"in": [],
38+
"out": []
39+
},
40+
"version": "v1.0"
41+
}
42+
},
43+
"outputs": [],
44+
"source": [
45+
"event = asyncio.Event()\n",
46+
"\n",
47+
"\n",
48+
"async def stop(request):\n",
49+
" event.set()\n",
50+
"\n",
51+
"\n",
52+
"app = aiohttp.web.Application()\n",
53+
"app.add_routes(\n",
54+
" [\n",
55+
" aiohttp.web.get(\"/\", handle),\n",
56+
" aiohttp.web.get(\"/stop\", stop),\n",
57+
" aiohttp.web.get(\"/{name}\", handle),\n",
58+
" ]\n",
59+
")\n",
60+
"\n",
61+
"runner = aiohttp.web.AppRunner(app)\n",
62+
"await runner.setup()\n",
63+
"site = aiohttp.web.TCPSite(runner, \"localhost\", 43210)\n",
64+
"await site.start()\n",
65+
"\n",
66+
"_ = await event.wait()"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"id": "a293b211",
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"async def client():\n",
77+
" async with aiohttp.ClientSession() as session:\n",
78+
" async with session.get(\"http://localhost:43210/\") as response:\n",
79+
" text = await response.text()\n",
80+
" return text\n",
81+
"\n",
82+
"\n",
83+
"await client()"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"id": "fbf301c1",
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"async with aiohttp.ClientSession() as session:\n",
94+
" try:\n",
95+
" await session.get(\"http://localhost:43210/stop\")\n",
96+
" except aiohttp.ServerDisconnectedError:\n",
97+
" pass"
98+
]
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "Jupyter Workflow",
104+
"language": "python",
105+
"name": "jupyter-workflow"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 3
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython3",
117+
"version": "3.10.6"
118+
}
119+
},
120+
"nbformat": 4,
121+
"nbformat_minor": 5
122+
}

0 commit comments

Comments
 (0)