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.

gravatar
SD

This is fantastic. I used rich for python in the console before, so the ability to use it here is great. I use most features in both my professional work and personal work. The syntax highlighting out-of-the-box is extremely useful and really increases my productivity when printing for debugging/logging purposes in Jupyter notebooks. Emojis are cool too, I always through them in for a bit of fun but they make important parts of code easier to see too (could always use colour but that's not as cool). I've noticed it's a bit janky with Google Colaboratory notebooks but it hasn't stopped me using it.

gravatar
SD

throw*