Skip to content
Open
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
8 changes: 6 additions & 2 deletions jupyter_workflow/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ def execute(
if not isinstance(code, str):
raise ValueError("code %r must be a string" % code)
validate_string_dict(user_expressions)
workflow = self.metadata.get()
content = {
"code": code,
"silent": silent,
"store_history": store_history,
"user_expressions": user_expressions,
"allow_stdin": allow_stdin,
"stop_on_error": stop_on_error,
"workflow": self.metadata.get(),
"workflow": workflow,
}
msg = self.session.msg("execute_request", content)
if workflow.get('step', {}).get('background', False):
msg = self.session.msg("background_execute_request", content)
else:
msg = self.session.msg("execute_request", content)
self.shell_channel.send(msg)
return msg["header"]["msg_id"]

Expand Down
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
}