Skip to content

Commit 94e36a9

Browse files
author
Quentin Peter
committed
Add magic to enable and disable the eventloop while waiting for input
1 parent f20c034 commit 94e36a9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

ipykernel/ipkernel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def __init__(self, **kwargs):
8585
import appnope
8686
appnope.nope()
8787

88+
self.shell.magics_manager.register_function(self.input_eventloop)
89+
8890
help_links = List([
8991
{
9092
'text': "Python Reference",

ipykernel/kernelbase.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from zmq.eventloop.zmqstream import ZMQStream
3030

3131
from traitlets.config.configurable import SingletonConfigurable
32-
from IPython.core.error import StdinNotImplementedError
32+
from IPython.core.error import StdinNotImplementedError, UsageError
3333
from ipython_genutils import py3compat
3434
from ipython_genutils.py3compat import unicode_type, string_types
3535
from ipykernel.jsonutil import json_clean
@@ -191,6 +191,26 @@ def handle_msg(msg):
191191
self._stdin_msg = msg
192192

193193
self.stdin_stream.on_recv(handle_msg, copy=False)
194+
# Should the eventloop be run while waiting for input
195+
self._input_eventloop = False
196+
197+
def input_eventloop(self, active):
198+
"""
199+
Activates and desactivates the eventloop while waiting for input.
200+
201+
active is "True" or "False" (Strings as it is used as a magic)
202+
203+
This allows eg. matplotlib plots to be used while debugging.
204+
205+
This should not be active while debugging a gui application that
206+
uses the same eventloop as the events will be processed.
207+
"""
208+
if active == "True":
209+
self._input_eventloop = True
210+
elif active == "False":
211+
self._input_eventloop = False
212+
else:
213+
raise UsageError('Please use "True" or "False"')
194214

195215
@gen.coroutine
196216
def dispatch_control(self, msg):
@@ -943,7 +963,7 @@ def _input_request_loop_step(self):
943963
else:
944964
is_main_thread = isinstance(threading.current_thread(),
945965
threading._MainThread)
946-
if is_main_thread and self.eventloop:
966+
if is_main_thread and self.eventloop and self._input_eventloop:
947967
self.eventloop(self)
948968
return self._stdin_msg
949969
else:

0 commit comments

Comments
 (0)