Skip to content

Commit

Permalink
Merge pull request #969 from maartenbreddels/fix_clear_output_with_wait
Browse files Browse the repository at this point in the history
FIX: respect wait for clear_output
  • Loading branch information
MSeal authored Mar 21, 2019
2 parents 0ef9283 + 741332a commit 12e148f
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 18 deletions.
60 changes: 42 additions & 18 deletions nbconvert/preprocessors/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def run_cell(self, cell, cell_index=0):
exec_reply = self._wait_for_reply(msg_id, cell)

outs = cell.outputs = []
self.clear_before_next_output = False

while True:
try:
Expand Down Expand Up @@ -475,13 +476,10 @@ def run_cell(self, cell, cell_index=0):
elif msg_type == 'execute_input':
continue
elif msg_type == 'clear_output':
outs[:] = []
# clear display_id mapping for this cell
for display_id, cell_map in self._display_id_map.items():
if cell_index in cell_map:
cell_map[cell_index] = []
self.clear_output(outs, msg, cell_index)
continue
elif msg_type.startswith('comm'):
self.handle_comm_msg(outs, msg, cell_index)
continue

display_id = None
Expand All @@ -493,22 +491,48 @@ def run_cell(self, cell, cell_index=0):
# update_display_data doesn't get recorded
continue

try:
out = output_from_msg(msg)
except ValueError:
self.log.error("unhandled iopub msg: " + msg_type)
continue
if display_id:
# record output index in:
# _display_id_map[display_id][cell_idx]
cell_map = self._display_id_map.setdefault(display_id, {})
output_idx_list = cell_map.setdefault(cell_index, [])
output_idx_list.append(len(outs))

outs.append(out)
self.output(outs, msg, display_id, cell_index)

return exec_reply, outs

def output(self, outs, msg, display_id, cell_index):
msg_type = msg['msg_type']
if self.clear_before_next_output:
self.log.debug('Executing delayed clear_output')
outs[:] = []
self.clear_display_id_mapping(cell_index)
self.clear_before_next_output = False

try:
out = output_from_msg(msg)
except ValueError:
self.log.error("unhandled iopub msg: " + msg_type)
return
if display_id:
# record output index in:
# _display_id_map[display_id][cell_idx]
cell_map = self._display_id_map.setdefault(display_id, {})
output_idx_list = cell_map.setdefault(cell_index, [])
output_idx_list.append(len(outs))
outs.append(out)

def clear_output(self, outs, msg, cell_index):
content = msg['content']
if content.get('wait'):
self.log.debug('Wait to clear output')
self.clear_before_next_output = True
else:
self.log.debug('Immediate clear output')
outs[:] = []
self.clear_display_id_mapping(cell_index)

def clear_display_id_mapping(self, cell_index):
for display_id, cell_map in self._display_id_map.items():
if cell_index in cell_map:
cell_map[cell_index] = []

def handle_comm_msg(self, outs, msg, cell_index):
pass

def executenb(nb, cwd=None, km=None, **kwargs):
"""Execute a notebook's code, updating outputs within the notebook object.
Expand Down
152 changes: 152 additions & 0 deletions nbconvert/preprocessors/tests/files/Clear Output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,158 @@
" clear_output()\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"print(\"Hello world\")\n",
"clear_output()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
]
}
],
"source": [
"print(\"Hello world\")\n",
"clear_output(wait=True) # no output after this"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"world\n"
]
}
],
"source": [
"print(\"Hello\")\n",
"clear_output(wait=True) # here we have new output after wait=True\n",
"print(\"world\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello world'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"handle0 = display(\"Hello world\", display_id=\"id0\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'world'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"handle1 = display(\"Hello\", display_id=\"id1\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"handle1.update('world')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"handle2 = display(\"Hello world\", display_id=\"id2\")\n",
"clear_output() # clears all output, also with display_ids"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello world'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"handle3 = display(\"Hello world\", display_id=\"id3\")\n",
"clear_output(wait=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"world\n"
]
}
],
"source": [
"handle4 = display(\"Hello\", display_id=\"id4\")\n",
"clear_output(wait=True)\n",
"print('world')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"handle4.update('Hello world') # it is cleared, so it should not show up in the above cell"
]
}
],
"metadata": {},
Expand Down

0 comments on commit 12e148f

Please sign in to comment.