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

pretty.install() fix for some objects with _repr_*_ #1804

Merged
merged 12 commits into from Jan 7, 2022

Conversation

darrenburns
Copy link
Member

Type of changes

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

The issue is that Rich assumes that if a _repr_*_ method exists on an object, then notebook will definitely render that object.

However, notebook only supports a subset of mimetypes, and even in the case of supported mimetypes, some libraries (e.g. pandas) support toggling off functionality, which results in the _repr_*_ for that mimetype returning None.

For example, pd.Series has a _repr_latex_ method, but by default the config for it ("display.latex.repr") is switched off and so pd.Series._repr_latex_ returns None.

We need something a little finer-grained and more aware of the notebook capabilities.

For all of the mimetypes that Jupyter notebook can display, we check if the corresponding _repr_*_ special method exists and ensure it doesn't return None. If it returns None, Rich will print it, otherwise the notebook will render it.

For all of the mimetypes that Jupyter notebook can display,
we check if the method exists and ensure it doesn't return None.
If this is the case, we delegate rendering to the notebook. Otherwise,
Rich will be used to render.

https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
@willmcgugan
Copy link
Collaborator

LGTM - Could you add that to CHANGELOG.md

@willmcgugan
Copy link
Collaborator

👍

@willmcgugan willmcgugan merged commit a6e1d7b into master Jan 7, 2022
@xoolive
Copy link

xoolive commented Jan 7, 2022

So cool, many thanks to you both.
However, on my side, the pd.Series now appears (:heart_on_fire:) but is not pretty printed. That's really not a big deal (better than printing nothing) but just saying... :man_shrugging:

@willmcgugan
Copy link
Collaborator

That's likely because pd.Series has support for advanced rendering in Jupyter. We don't want to use Rich's printing if the object already knows how to format itself.

@xoolive
Copy link

xoolive commented Jan 7, 2022

Fine with me, thanks anyway!

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.

[BUG] repr fails for pd.Series in Jupyter with pretty.install()
3 participants