The Wayback Machine - https://web.archive.org/web/20230302181824/https://github.com/Textualize/rich/pull/1918
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

Fixed force_terminal on Jupyter #1918

Closed
wants to merge 4 commits into from

Conversation

verdimrc
Copy link

@verdimrc verdimrc commented Feb 4, 2022

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

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

Please describe your changes here. If this fixes a bug, please link to the issue, if possible.

Fixes behavior after running rich.pretty.install() Jupyter Lab:

  • Do not show <Figure size 432x288 with 1 Axes> on Matplotlib figures
  • With force_terminal=True. and force_jupyter=None (or True), pandas dataframe must still be rendered once (i.e., the html repr).

Screenshot below (from this notebook) compares the four behaviors: (1) vanilla Python, (2 and 3) rich, and (4) this PR.

screenshot

- prevent pandas dataframes getting rendered twice (string and html)
- suppress matplotlib "<Figure ...>"
@willmcgugan
Copy link
Collaborator

Why would you force_terminal in Jupyter lab?

Please don't ask me to run something in Jupyter when you can paste a screenshot.

@verdimrc
Copy link
Author

verdimrc commented Feb 20, 2022

Thank you for taking a look. Added a screenshot to the PR description, with markers on the behaviors that this PR tries to fix.

@willmcgugan
Copy link
Collaborator

Why would you force_terminal in Jupyter lab?

@codecov-commenter
Copy link

codecov-commenter commented Feb 27, 2022

Codecov Report

Merging #1918 (9cf8499) into master (e839bfb) will decrease coverage by 0.02%.
The diff coverage is 98.49%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1918      +/-   ##
==========================================
- Coverage   99.82%   99.80%   -0.03%     
==========================================
  Files          71       71              
  Lines        6943     7057     +114     
==========================================
+ Hits         6931     7043     +112     
- Misses         12       14       +2     
Flag Coverage Δ
unittests 99.80% <98.49%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
rich/default_styles.py 100.00% <ø> (ø)
rich/markdown.py 100.00% <ø> (ø)
rich/pretty.py 99.43% <97.61%> (-0.57%) ⬇️
rich/syntax.py 99.27% <97.72%> (-0.34%) ⬇️
rich/__main__.py 100.00% <100.00%> (ø)
rich/_inspect.py 100.00% <100.00%> (ø)
rich/color.py 100.00% <100.00%> (ø)
rich/console.py 100.00% <100.00%> (ø)
rich/progress.py 98.43% <100.00%> (+0.31%) ⬆️
rich/segment.py 99.34% <100.00%> (+0.05%) ⬆️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 880693d...9cf8499. Read the comment docs.

@willmcgugan
Copy link
Collaborator

I'm going to need an answer to my question, and some code I can cut and paste (or a notebook).

@verdimrc
Copy link
Author

verdimrc commented May 4, 2022

Hi, I thought the quoted question was posted accidentally, my apology.

The motivation to let Jupyter display cell outputs as colored ASCII rather than HTML are:

  1. to use the same font as the code cells. Otherwise, HTML output will be displayed with a noticeably different font.
  2. to automatically wrap long ASCII texts (which is how Jupyter behaves without rich). Otherwise, long HTML output will need horizontal scrolling, which makes rich notebook to behave differently than stock Jupyter.

Below are the Python fragments to demonstrate the effect of this PR. You'll need to pip install pandas to run this example in Jupyter.

This fragment shows the effect on Jupyter "display_data".

import pandas as pd
import rich
import rich.pretty

# Stuffs to display
d = {"A" * 200, "B" * 200}
df = pd.DataFrame({"a": range(3), "b": range(3)})

# Render "display_data" as colored text.
rich.reconfigure(force_terminal=True)
rich.pretty.install()
display(d, df, df["a"])

# Jupyter may surpress certain "display_data". For example, the semicolon makes Jupyter hide the
# "<AxesSubplot:...>" message.
df.plot();

Here's the side-by-side comparison:

patched

One thing to also note is this PR changes only the ipython display hook, and makes no attempt to change the Console behavior. In the next example, the Jupyter "stream_output" still defaults to HTML, and will have to be specifically instructed for colored ASCII.

# By default, render "stream_output" (e.g., stdout) as HTML.
rich.get_console().out("a", d)

# However, "stream_output" can be force rendered as colored text.
rich.console.Console(force_terminal=True, force_jupyter=False).out("a", d)
tdung
tdung approved these changes May 5, 2022
@willmcgugan willmcgugan mentioned this pull request Jun 14, 2022
@olivierphi
Copy link
Contributor

olivierphi commented Jun 28, 2022

@verdimrc Note that in order to automatically wrap long ASCII texts it seems that we just have to specify a fold "overflow" to Rich, without having to use the force_* hammers: i.e. with rich.pretty.install(overflow="fold") the text is wrapped when the Jupyter notebook displays it 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants