Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable hook for pinfo (?) akin to _ipython_display_ #1005

Open
b3m2a1 opened this issue Oct 14, 2022 · 1 comment
Open

Customizable hook for pinfo (?) akin to _ipython_display_ #1005

b3m2a1 opened this issue Oct 14, 2022 · 1 comment

Comments

@b3m2a1
Copy link

b3m2a1 commented Oct 14, 2022

[Related to https://github.com/jupyterlab/jupyterlab/issues/13217]

This is mostly applicable to working in a notebook or lab environment, but I would like to be able to customize what happens when I call ? on the classes and objects in the libraries I write. This kind of hook already exists for displaying objects through _ipython_display_ and now I would just like it to also work for calling like ?MyNiceClass

In principle, this should be easy as ? just calls into the pinfo magic which could check for the existence of some hook like _ipython_pinfo_ or something similar, although two variants might be required, one for instances and one for classes

@b3m2a1
Copy link
Author

b3m2a1 commented Oct 14, 2022

I was able to monkey patch this in like

def patch_pinfo():
    from IPython.core.oinspect import Inspector
    from IPython.core.display import display

    if not hasattr(Inspector, '_og_pinfo'):
        Inspector._og_pinfo = Inspector.pinfo

    def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0, enable_html_pager=True):
        if hasattr(obj, '_ipython_pinfo_'):
            display(obj._ipython_pinfo_())
        else:
            return Inspector._og_pinfo(self, obj, oname=oname, formatter=formatter, info=info, detail_level=detail_level, enable_html_pager=enable_html_pager)

    Inspector.pinfo = pinfo

and then by using a mixedmethod decorator that acts like a classmethod or a bound method I can add this to an object

    @mixedmethod
    def _ipython_pinfo_(cls):
        return jdoc(cls)

which allows me to do this

image

which is exactly the behavior I want. I'd obviously prefer a solution that doesn't involve monkey patching though.

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

No branches or pull requests

1 participant