Skip to content

Commit 84e34a3

Browse files
chrisjsewelldavidbrochart
authored andcommitted
add working example
1 parent 523309b commit 84e34a3

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
@@ -802,9 +802,7 @@ async def async_execute_cell(
802802
if self.parse_md_expressions and cell.cell_type == 'markdown':
803803
expressions = self.parse_md_expressions(cell.source)
804804
if expressions:
805-
if not "attachments" in cell:
806-
cell.attachments = {}
807-
cell.attachments.update(await self.async_execute_expressions(cell, cell_index, expressions))
805+
await self.async_execute_expressions(cell, cell_index, expressions)
808806

809807
if cell.cell_type != 'code' or not cell.source.strip():
810808
self.log.debug("Skipping non-executing cell %s", cell_index)
@@ -1028,20 +1026,18 @@ def handle_comm_msg(self, outs: t.List, msg: t.Dict, cell_index: int) -> None:
10281026
self.comm_objects[comm_id].handle_msg(msg)
10291027

10301028
async def async_execute_expressions(self, cell, cell_index: int, expressions: t.List[str]) -> t.Dict[str, Any]:
1031-
user_expressions = {f"md-expr-{i}": expr for i, expr in enumerate(expressions)}
1032-
print(user_expressions)
10331029
parent_msg_id = await ensure_async(
10341030
self.kc.execute(
10351031
'',
1036-
silent=False,
1037-
user_expressions=user_expressions,
1032+
silent=True,
1033+
user_expressions={f"md-expr-{i}": expr for i, expr in enumerate(expressions)},
10381034
)
10391035
)
10401036
task_poll_kernel_alive = asyncio.ensure_future(
10411037
self._async_poll_kernel_alive()
10421038
)
10431039
task_poll_expr_msg = asyncio.ensure_future(
1044-
self._async_poll_expr_msg(parent_msg_id, cell, cell_index)
1040+
self._async_poll_expr_msg(parent_msg_id)
10451041
)
10461042
exec_timeout = None
10471043
self.task_poll_for_reply = asyncio.ensure_future(
@@ -1063,6 +1059,11 @@ async def async_execute_expressions(self, cell, cell_index: int, expressions: t.
10631059
task_poll_expr_msg.cancel()
10641060
finally:
10651061
raise
1062+
self._check_raise_for_error(cell, exec_reply)
1063+
attachments = {key: val["data"] for key, val in exec_reply["content"]["user_expressions"].items()}
1064+
cell.setdefault("attachments", {}).update(attachments)
1065+
self.nb['cells'][cell_index] = cell
1066+
return cell
10661067

10671068
async def _async_poll_for_expr_reply(
10681069
self,
@@ -1105,24 +1106,17 @@ async def _async_poll_for_expr_reply(
11051106

11061107
async def _async_poll_expr_msg(
11071108
self,
1108-
parent_msg_id: str,
1109-
cell: NotebookNode,
1110-
cell_index: int) -> None:
1109+
parent_msg_id: str) -> None:
11111110

11121111
assert self.kc is not None
11131112
while True:
11141113
msg = await ensure_async(self.kc.iopub_channel.get_msg(timeout=None))
11151114
if msg['parent_header'].get('msg_id') == parent_msg_id:
11161115
try:
1117-
# Will raise CellExecutionComplete when completed
1118-
# self.process_message(msg, cell, cell_index)
1119-
print(msg)
11201116
msg_type = msg['msg_type']
11211117
if msg_type == 'status':
11221118
if msg['content']['execution_state'] == 'idle':
11231119
raise CellExecutionComplete()
1124-
# elif msg_type != 'execute_input':
1125-
# raise ValueError(msg)
11261120
except CellExecutionComplete:
11271121
return
11281122

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
@@ -230,6 +230,10 @@ def assert_notebooks_equal(expected, actual):
230230
actual_execution_count = actual_cell.get('execution_count', None)
231231
assert expected_execution_count == actual_execution_count
232232

233+
expected_execution_count = expected_cell.get('attachments', None)
234+
actual_execution_count = actual_cell.get('attachments', None)
235+
assert expected_execution_count == actual_execution_count
236+
233237

234238
def notebook_resources():
235239
"""

0 commit comments

Comments
 (0)