Skip to content

Commit c1ef2e3

Browse files
committed
add working example
1 parent 3aea02b commit c1ef2e3

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

nbclient/client.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,7 @@ async def async_execute_cell(
818818
if self.parse_md_expressions and cell.cell_type == 'markdown':
819819
expressions = self.parse_md_expressions(cell.source)
820820
if expressions:
821-
if not "attachments" in cell:
822-
cell.attachments = {}
823-
cell.attachments.update(await self.async_execute_expressions(cell, cell_index, expressions))
821+
await self.async_execute_expressions(cell, cell_index, expressions)
824822

825823
if cell.cell_type != 'code' or not cell.source.strip():
826824
self.log.debug("Skipping non-executing cell %s", cell_index)
@@ -1052,20 +1050,18 @@ def handle_comm_msg(
10521050
self.comm_objects[comm_id].handle_msg(msg)
10531051

10541052
async def async_execute_expressions(self, cell, cell_index: int, expressions: t.List[str]) -> t.Dict[str, Any]:
1055-
user_expressions = {f"md-expr-{i}": expr for i, expr in enumerate(expressions)}
1056-
print(user_expressions)
10571053
parent_msg_id = await ensure_async(
10581054
self.kc.execute(
10591055
'',
1060-
silent=False,
1061-
user_expressions=user_expressions,
1056+
silent=True,
1057+
user_expressions={f"md-expr-{i}": expr for i, expr in enumerate(expressions)},
10621058
)
10631059
)
10641060
task_poll_kernel_alive = asyncio.ensure_future(
10651061
self._async_poll_kernel_alive()
10661062
)
10671063
task_poll_expr_msg = asyncio.ensure_future(
1068-
self._async_poll_expr_msg(parent_msg_id, cell, cell_index)
1064+
self._async_poll_expr_msg(parent_msg_id)
10691065
)
10701066
exec_timeout = None
10711067
self.task_poll_for_reply = asyncio.ensure_future(
@@ -1087,6 +1083,11 @@ async def async_execute_expressions(self, cell, cell_index: int, expressions: t.
10871083
task_poll_expr_msg.cancel()
10881084
finally:
10891085
raise
1086+
self._check_raise_for_error(cell, exec_reply)
1087+
attachments = {key: val["data"] for key, val in exec_reply["content"]["user_expressions"].items()}
1088+
cell.setdefault("attachments", {}).update(attachments)
1089+
self.nb['cells'][cell_index] = cell
1090+
return cell
10901091

10911092
async def _async_poll_for_expr_reply(
10921093
self,
@@ -1129,24 +1130,17 @@ async def _async_poll_for_expr_reply(
11291130

11301131
async def _async_poll_expr_msg(
11311132
self,
1132-
parent_msg_id: str,
1133-
cell: NotebookNode,
1134-
cell_index: int) -> None:
1133+
parent_msg_id: str) -> None:
11351134

11361135
assert self.kc is not None
11371136
while True:
11381137
msg = await ensure_async(self.kc.iopub_channel.get_msg(timeout=None))
11391138
if msg['parent_header'].get('msg_id') == parent_msg_id:
11401139
try:
1141-
# Will raise CellExecutionComplete when completed
1142-
# self.process_message(msg, cell, cell_index)
1143-
print(msg)
11441140
msg_type = msg['msg_type']
11451141
if msg_type == 'status':
11461142
if msg['content']['execution_state'] == 'idle':
11471143
raise CellExecutionComplete()
1148-
# elif msg_type != 'execute_input':
1149-
# raise ValueError(msg)
11501144
except CellExecutionComplete:
11511145
return
11521146

nbclient/tests/files/Markdown_expressions.ipynb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
{
1414
"cell_type": "markdown",
1515
"metadata": {},
16+
"attachments": {
17+
"md-expr-0": {"text/plain": "1"},
18+
"md-expr-1": {"text/plain": "'c'"}
19+
},
1620
"source": [
1721
"# Variables\n",
1822
"\n",

nbclient/tests/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ def assert_notebooks_equal(expected, actual):
232232
actual_execution_count = actual_cell.get('execution_count', None)
233233
assert expected_execution_count == actual_execution_count
234234

235+
expected_execution_count = expected_cell.get('attachments', None)
236+
actual_execution_count = actual_cell.get('attachments', None)
237+
assert expected_execution_count == actual_execution_count
238+
235239

236240
def notebook_resources():
237241
"""

0 commit comments

Comments
 (0)