Skip to content

Commit ebf1212

Browse files
committed
Merge pull request #4363 from pablooliveira/set_next_input-reuses-cell
set_next_input: keep only last input when repeatedly called in a single cell change applied in general to payloads
2 parents 2cdbe39 + e9db6d5 commit ebf1212

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

IPython/core/payload.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,23 @@ class PayloadManager(Configurable):
2929

3030
_payload = List([])
3131

32-
def write_payload(self, data):
32+
def write_payload(self, data, single=True):
33+
"""Include or update the specified `data` payload in the PayloadManager.
34+
35+
If a previous payload with the same source exists and `single` is True,
36+
it will be overwritten with the new one.
37+
"""
38+
3339
if not isinstance(data, dict):
3440
raise TypeError('Each payload write must be a dict, got: %r' % data)
41+
42+
if single and 'source' in data:
43+
source = data['source']
44+
for i, pl in enumerate(self._payload):
45+
if 'source' in pl and pl['source'] == source:
46+
self._payload[i] = data
47+
return
48+
3549
self._payload.append(data)
3650

3751
def read_payload(self):

IPython/kernel/tests/test_message_spec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ def test_kernel_info_request():
406406
validate_message(reply, 'kernel_info_reply', msg_id)
407407

408408

409+
def test_single_payload():
410+
flush_channels()
411+
msg_id, reply = execute(code="for i in range(3):\n"+
412+
" x=range?\n")
413+
payload = reply['payload']
414+
next_input_pls = [pl for pl in payload if pl["source"] == "set_next_input"]
415+
nt.assert_equal(len(next_input_pls), 1)
416+
417+
409418
# IOPub channel
410419

411420

0 commit comments

Comments
 (0)