Skip to content

Refactor inputhook to allow easy extension - #5666

Merged
minrk merged 6 commits into
ipython:masterfrom
takluyver:inputhook-extensible
Sep 19, 2014
Merged

minrk merged 6 commits into
ipython:masterfrom
takluyver:inputhook-extensible

Conversation

@takluyver

Copy link
Copy Markdown
Member

Not ready for merge

Prompted by #5645, @minrk suggested that we should provide an extension mechanism for input hooks, so that that code (which we don't use, test or maintain very much) can be maintained outside IPython. This is a prototype of that.

To define a new GUI integration, you register a class like this:

from IPython.lib.inputhook import register, InputHookBase

@register('clutter')
class ClutterInputHook(InputHookBase):
    def enable(self, app=None):
        self.manager.set_inputhook(inputhook_clutter)

It can also define a disable() method, but only one of the 7 we include needs that. Once this code has run (e.g. by importing a module or using %load_ext), the user should be able to do %gui clutter.

To do:

  • Similar mechanisms for the kernel event loop
  • Docs

@takluyver takluyver mentioned this pull request Apr 18, 2014
@takluyver

Copy link
Copy Markdown
Member Author

@vindolin, do you have time to have a look at this before I go ahead with details like tests and docs?

@vindolin

Copy link
Copy Markdown

Hi, sorry I was a bit occupied :(
Do you have any examples for one of the current GUIs using this mechanism?

@takluyver

Copy link
Copy Markdown
Member Author

I rewrote all of the current GUIs to use this mechanism: https://github.com/takluyver/ipython/blob/inputhook-extensible/IPython/lib/inputhook.py#L278

The only difference is that they're in one module, but it should work just the same for code in another module.

@vindolin

Copy link
Copy Markdown

Ok great, I try to play around with this on Friday.
I'll see if I can use the existing hooks as a starting point.

@vindolin

Copy link
Copy Markdown

https://github.com/vindolin/IPython-Clutter-GUI-Integration
This seems to work now.
I first thought that the console and notebook version used the same mechanism and it was driving me crazy that, no matter what I did, I always got the error message "ERROR: Invalid GUI request 'clutter'," in notebook :)

Finally I can poke around in the running GUI instance, that makes learning the framework so much easier :)

@minrk minrk added this to the 3.0 milestone May 7, 2014
@takluyver

Copy link
Copy Markdown
Member Author

I've rebased this and added an extension mechanism for event loops in the kernel, along with documentation of both systems. I don't see an easy way to add tests (and this is a part of our code that already only has some manual tests), so I've set it back to 'Needs review'.

@takluyver takluyver mentioned this pull request Sep 18, 2014
Comment thread IPython/lib/inputhook.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want a deprecation warning?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done.

Also, update examples to not use these

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note about deprecated functions here or the new config/eventloops doc?

@minrk

minrk commented Sep 18, 2014

Copy link
Copy Markdown
Member

This is really nice. And great job on the thorough doc. What are your plans for testing? I see an unchecked task box.

@takluyver

Copy link
Copy Markdown
Member Author

I added a whatsnew note about the deprecated functions.

My plans for testing currently amount to: don't, because it's hard, and we've never had automated tests for this, as far as I know. I've verified that the examples in examples/IPython Kernel/gui/ still run, both in the terminal and the kernel, where possible.

@minrk

minrk commented Sep 19, 2014

Copy link
Copy Markdown
Member

Fair enough, I just saw the unchecked task box and wanted to check in. Merging away, then.

minrk added a commit that referenced this pull request Sep 19, 2014
Refactor inputhook to allow easy extension
@minrk
minrk merged commit c8c43c8 into ipython:master Sep 19, 2014
Comment thread IPython/lib/inputhook.py

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhamrick says I broke the --gui=osx option. It looks like it was already doing nothing, and it just got lost in the refactor. So now ipython --gui=osx refuses to start instead of silently ignoring the option.

The option is used in @jhamrick's 'moderately popular' emacs config, so we may want to put it back with a deprecation warning for a release cycle.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A deprecation is fine, if we can figure it out. Some further explanation: this key is only defined because enable_matplotlib('foo') implies enable_gui('foo'). Nothing needs to be done for OS X eventloop integration, but the key needs to be defined in order for enable_matplotlib('osx') to pass. If there's a better way to express 'this matplotlib backend doesn't actually need eventloop integration' beyond a null function in the dict, then that would be fine, too. But enable_matplotlib('osx') does need to work, and it doesn't, currently.

@erikhvatum

Copy link
Copy Markdown
Contributor

On both OS X and Linux with PyQt5, I have to revert out this commit. If I leave this commit in, control-c is borked.

(control C pressed here)
In [4]: Traceback (most recent call last):
File "/Users/ehvatum/Desktop/ipython_git/IPython/lib/inputhookqt4.py", line 123, in inputhook_qt4
event_loop.exec_()
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "_ctypes/callbacks.c", line 277, in 'calling callback function'
File "/Users/ehvatum/Desktop/ipython_git/IPython/lib/inputhookqt4.py", line 130, in inputhook_qt4
mgr.clear_inputhook()
AttributeError: 'Qt4InputHook' object has no attribute 'clear_inputhook'

If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True

Traceback (most recent call last):
File "/usr/local/bin/ipython", line 9, in
load_entry_point('ipython==3.0.0-dev', 'console_scripts', 'ipython')()
File "/Users/ehvatum/Desktop/ipython_git/IPython/init.py", line 120, in start_ipython
return launch_new_instance(argv=argv, *_kwargs)
File "/Users/ehvatum/Desktop/ipython_git/IPython/config/application.py", line 549, in launch_instance
app.start()
File "/Users/ehvatum/Desktop/ipython_git/IPython/terminal/ipapp.py", line 372, in start
self.shell.mainloop()
File "/Users/ehvatum/Desktop/ipython_git/IPython/terminal/interactiveshell.py", line 383, in mainloop
self.interact(display_banner=display_banner)
File "/Users/ehvatum/Desktop/ipython_git/IPython/terminal/interactiveshell.py", line 446, in interact
self.hooks.pre_prompt_hook()
File "/Users/ehvatum/Desktop/ipython_git/IPython/core/hooks.py", line 137, in call
return cmd(_args, **kw)
File "/Users/ehvatum/Desktop/ipython_git/IPython/lib/inputhookqt4.py", line 174, in preprompthook_qt4
mgr.set_inputhook(inputhook_qt4)
AttributeError: 'Qt4InputHook' object has no attribute 'set_inputhook'

If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True

@takluyver

Copy link
Copy Markdown
Member Author

Thanks @erikhvatum . Can you make a new issue for that so it doesn't drop under the radar.

@erikhvatum

Copy link
Copy Markdown
Contributor

@takluyver Issue for PyQt5 ctrl-c: #6804

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Refactor inputhook to allow easy extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants