[go: nahoru, domu]

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

rich.pretty.install does not work for IPython #3013

Closed
zhengyu-yang opened this issue Jul 1, 2023 · 2 comments · Fixed by #3065
Closed

rich.pretty.install does not work for IPython #3013

zhengyu-yang opened this issue Jul 1, 2023 · 2 comments · Fixed by #3065

Comments

@zhengyu-yang
Copy link

Version:

Python 3.8.13
IPython 8.12.2

It seems that get_ipython is not in globals when executed in pretty.py, causing the rich text formatter not being installed. It is actually in globals()['__builtins__']. I suggest just use try to replace this check. The problem happens here:

if "get_ipython" in globals():

The fix is:

  try:
      ip = get_ipython()  # type: ignore[name-defined]
      from IPython.core.formatters import BaseFormatter

      class RichFormatter(BaseFormatter):  # type: ignore[misc]
          pprint: bool = True

          def __call__(self, value: Any) -> Any:
              if self.pprint:
                  return _ipy_display_hook(
                      value,
                      console=get_console(),
                      overflow=overflow,
                      indent_guides=indent_guides,
                      max_length=max_length,
                      max_string=max_string,
                      max_depth=max_depth,
                      expand_all=expand_all,
                  )
              else:
                  return repr(value)

      # replace plain text formatter with rich formatter
      rich_formatter = RichFormatter()
      ip.display_formatter.formatters["text/plain"] = rich_formatter
  except NameError:
      sys.displayhook = display_hook
@github-actions
Copy link
github-actions bot commented Jul 1, 2023

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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 a pull request may close this issue.

1 participant