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

_ipython_display_ cannot distinguish between IPython.display.display_{pretty,html,...} #13022

Open
boeddeker opened this issue Jun 16, 2021 · 2 comments

Comments

@boeddeker
Copy link
Contributor

Story

I tried to implement a class, which decides itself, which _repr_*_ it wants to use, because it is too expensive to calculate all _repr_*_ (xflr6/graphviz#138) and the default ordering is not good for that class.

It looks like this is not compatible with the idea of display, because the front end should decide.
But there is a workaround, the _ipython_display_ method.
When it is defined, the class can decide how to display.

Problem

The _ipython_display_ cannot distinguish between display and display_*.
So it always displays the same things, independent, if it was triggered by display_pretty or display_html.

Idea 1:

How about adding an argument, that indicate which format should be used and make a similar statement as _repr_mimebundle_:

The method should take keyword arguments include and exclude, though it is not required to respect them.

I am not sure, how difficult it is to implement this, because the code looks complicated.
To avoid a breaking change, a check of the signature would be necessary and then call it with zero or one argument.

Idea 2:

Ignore _ipython_display_ in display_*.

Example

from IPython.display import display, display_pretty, display_svg, display_html

class Foo:
    
    def _repr_pretty_(self, pp, cycle):
        return pp.text('pretty')
    
    def _repr_html_(self):
        return 'html'

class Bar:
    def _ipython_display_(self):
        display(Foo())

display(Foo())  # -> html
display_pretty(Foo())  # -> pretty
display_html(Foo())  # -> html

display(Bar())  # -> html
display_pretty(Bar())  # -> html
display_html(Bar())  # -> html
@Carreau
Copy link
Member

Carreau commented Jun 22, 2021

It should not be too difficult to pass extra arguments, problem is that it might break previous user of IPython display if they don't accept *arg, **kwargs. We do rely on https://pypi.org/project/backcall/ already so we might be able to use that, or inspect the function signature.

You mention _repr_mimebundle_, any reason this does not work ?

@boeddeker
Copy link
Contributor Author

Now, where you mention _repr_mimebundle_, I checked it again and yes, it works for the situation I had in mind.

Nevertheless, in my mind, it would be better, when _ipython_display_ could also distinguish between display and display_*.

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

2 participants