March 30, 2020 will

Rich adds support for Jupyter Notebooks

I recently added experimental support for Jupyter Notebooks to Rich. Here's an example of the kind of output it can generate:

This is the Rich test card you can generate in the terminal with python -m rich.

Part 2 of the Rich test card.

Rich is a library for fancy formatting to the terminal, where-as Jupyter supports HTML natively. So why integrate one with the other? I suspect it may be useful to render something like a table in the terminal which also displays nicely in Jupyter.

Jupyter does convert ANSI codes for style / colour etc in to HTML, which is nice. But it didn't work with Jupyter initially, because Jupyter replaces stdout with a file object that captures output, and that that object doesn't declare itself as a terminal capable device. It also broke tables and any other formatting that assumes a fix grid, because the browser wraps output which caused misalignment of box characters. Fortunately I was able to workaround both those issues.

You can try this out by doing from rich.jupyter import print within Jupyter. This will give the print function a few super-powers:

from rich.jupyter import print
print("Hello, [green bold]World![/] :ninja:")

Now you can insert colour and style in to your output. Rich will pretty print Python data structures with syntax highlighting, and you can render of the renderables supported by Rich.

I'm not sure how useful this is to anyone. I had barely used Jupyter before Sunday afternoon, but if it is useful please tweet @willmcgugan and let me know. I'm not sure if its worth making this an official part of the Rich API, or just leave it as a novelty I hacked up to avoid boredom while self-distancing.

Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
gravatar
jeclevenger

Looks good, I use jupyter day to day and would definitely like to try it out when available on pypi

gravatar
Will McGugan

It’s on Pypi now. Would be interested in feedback.

gravatar
David Staub

This looks awesome! I work from a Jupyter notebook 90% of the time and would particularly appreciate support for the progress bar and traceback functionality.

gravatar
Daniel

This is such a great feature, I work from Jupyter Lab environments mostly. So this support is great to see thank you.