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
Inject display into builtins #10596
Inject display into builtins #10596
Conversation
|
IPython/core/display.py
Outdated
|
||
>>> del plain.type_printers[int] | ||
>>> display(3+6) | ||
9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so great to have these examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are also in the Custom Display logic notebook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally minor nitpick, but it'd be even more clear if the display calls both used the same expression inside...
Make both display(3+6)
for example, so the top one is even more clear about how it's doing the formatting...
@@ -618,6 +619,7 @@ def init_builtins(self): | |||
# removing on exit or representing the existence of more than one | |||
# IPython at a time. | |||
builtin_mod.__dict__['__IPYTHON__'] = True | |||
builtin_mod.__dict__['display'] = display |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
away we go!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that pixiedust should fallback on ipython's display
Makes sense, I'll enter a Github issue in PixieDust repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I'll enter a Github issue in PixieDust repo
Thanks !
Or, if we can figure out a way to use IPython machinery for pixeldust (as we already have dispatch per object types) that would be great. Feel free to crosslink here or ping me.
I was recently alerted to the fact that pixiedust exposes The extra thing most of these other displayers do though is hook the display for data frames (spark and pandas) and Spark GraphFrames. |
/cc @DTAIEB |
Where is it injected ? in user_ns or builtins ? AFAICT that will lead to user having:
Or whatever object is given to it. I think that pixiedust should fallback on ipython's display. Registering formatter should allow to have rich representation in both case which should make the experience more uniform for users. The other things is that with this PR IPython's display is restored as soon as you delete any things that shadows it:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks sweet, Matthias! With the display_update stuff, I think display
is an even more powerful feature now that should available more prominently.
IPython/core/display.py
Outdated
|
||
>>> del plain.type_printers[int] | ||
>>> display(3+6) | ||
9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally minor nitpick, but it'd be even more clear if the display calls both used the same expression inside...
Make both display(3+6)
for example, so the top one is even more clear about how it's doing the formatting...
IPython/core/display.py
Outdated
You can refer to the documentation on IPython display formatters in order to | ||
register custom formatters for already existing types. | ||
|
||
since IPython 5.4 and 6.1 display is automatically made availlable to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" Since IPython 5.4 and 6.1 , " (capital S, comma after 6.1)
IPython/core/display.py
Outdated
|
||
since IPython 5.4 and 6.1 display is automatically made availlable to the | ||
user without import. If you are using display in a document that might be | ||
used in a pure python context or with older version of IPython use the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comma after IPython
""" | ||
raw = kwargs.pop('raw', False) | ||
if transient is None: | ||
transient = {} | ||
if display_id: | ||
if display_id == True: | ||
if display_id is True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice bug fix! display_id=1
would have always generated a new ID for this.
IPython/core/displayhook.py
Outdated
@@ -16,6 +16,8 @@ | |||
from traitlets import Instance, Float | |||
from warnings import warn | |||
|
|||
from IPython.display import display |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this import is here. Could you document that if it needs to stay here?
Excellent! |
Great! @ivanov since you mention the update display stuff, do you think Minor technical detail: should this go in |
It can, I did that originally, it's more complicated if we want to re-inject it when user delete it/shadow it, but doable. I believe This is still a relatively heavy usability change so I'm probing what other are thinking. I'd like @fperez input on this one as well (not on the code itself, but happy to get review) but wether it's ok to have |
4e5fd23
to
b3c4dfc
Compare
I agree that we shouldn't surface update_display. We can still use update = True on display anyways. |
On the When in user_ns_hidden it happily accept to delete it but the object will reapear:
If in builtins, il will complain that object can't be found if trying to delete it:
The user_ns_hidden also have the case that the object reapears only after each cell execution so cutting cell may change behavior. For example NameError vs NoError
We can also not reinject it, but I believe it is the wrong solution. |
Or, we can change the exec for (you can't ChainMap global, exec refuses), that should avoid a lot of the hidden_ns dance we do. |
display is an underutilized feature of IPython which is relatively hard to access to. You need to explicitly import from `IPython.display`, which not only is long to type, but users get confused if they should use `IPython.display` or `IPython.display.display` also many tutorials tell users to import from IPython.core.display which is not right. So inject that directly in builtin module – so that user can overwrite and delete without issues – and document display a lot more.
Rebased, updated, tested, documented. |
There is one (longstanding) issue that calling display in a pure Python terminal break the repl by instantiating an InteractiveShell (which has crazy side effects like nuking your namespace and setting sys.ps1), I'm unsure it's a blocker for this PR. |
But that will only happen if you start a plain Python shell after starting IPython in the same process, right? I don't think that comes up very often. |
Not even, it break if you just call display in a pure python repl. We can fix that, the only question if what to do. If Interactiveshell._instance is None fallback on print ? Even if raw=True ?
But that's another issue. |
@meeseeksdev backport |
Oops, something went wrong applying the patch... Please have a look at my logs. |
Merge pull request ipython#10596 from Carreau/display-builtin Inject display into builtins
Backport PR #10596 on branch 5.x
Oh, I see, you mean if code explicitly imports it. That's not affected by injecting display into builtins when IPython runs, though. |
Yes, sorry the missing datapoint is: |
Ah, that makes sense. Yeah, that's tricky. |
display is an underutilized feature of IPython which is relatively hard
to access to. You need to explicitly import from
IPython.display
,which not only is long to type, but users get confused if they should
use
IPython.display
orIPython.display.display
also many tutorialstell users to import from IPython.core.display which is not right.
So inject that directly in builtin module – so that user can overwrite
and delete without issues – and document display a lot more.
cc @rgbkrk, @ivanov