1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
Support for interactive mode to allow VisPy's event loop to be run alongside
a console terminal, without using threads. This code relies on inputhooks
built-in to the Python interpreter, and supports IPython too. The underlying
inputhook implementation is from IPython 3.x.
Note that IPython notebook integration is not supported, as the browser does
not use Python's PyOS_InputHook functionality.
"""
from ..ext.ipy_inputhook import inputhook_manager, InputHookBase, stdin_ready
def set_interactive(enabled=True, app=None):
"""Activate the IPython hook for VisPy. If the app is not specified, the
default is used.
"""
if enabled:
inputhook_manager.enable_gui('vispy', app)
else:
inputhook_manager.disable_gui()
@inputhook_manager.register('vispy')
class VisPyInputHook(InputHookBase):
"""Implementation of an IPython 3.x InputHook for VisPy. This is loaded
by default when you call vispy.app.run() in a console-based interactive
session, but you can also trigger it manually by importing this module
then typing:
>>> %enable_gui vispy
"""
def enable(self, app=None):
"""Activate event loop integration with this VisPy application.
Parameters
----------
app : instance of Application
The VisPy application that's being used. If None, then the
default application is retrieved.
Notes
-----
This methods sets the ``PyOS_InputHook`` to this implementation,
which allows Vispy to integrate with terminal-based applications
running in interactive mode (Python or IPython).
"""
from .. import app as _app
self.app = app or _app.use_app()
self.manager.set_inputhook(self._vispy_inputhook)
return app
def _vispy_inputhook(self):
try:
while not stdin_ready():
self.app.process_events()
# for more context.
# we need to wait out on the event loop to prevent CPU stress
# but not wait too much, to maintain fluidity.
# refer https://github.com/vispy/vispy/issues/945
self.app.sleep(duration_sec=0.03)
except KeyboardInterrupt:
pass
return 0
|