Python Posts

166 posts tagged with python

Rich 5.1.0 adds the ability to integrate highlighted pretty printing with the Python REPL. Here's how you would use it in a session:

Assuming you have installed Rich of course.

Now any data will be formatted and highlighted. Here's an example:

Rich will expand builtin containers until the output fits in to the width of the screen.

In addition to Python data structures, any object supporting the Rich Console Protocol will be displayed. Here's an example:

The Panel class is one of a number of renderables in Rich.

The formatting and highlighting really comes in to its own when you're working with JSON data. The following video shows an example of pretty printing data queried from a web service. In this instance, its cat facts.

Follow @willmcgugan for updates regarding Rich.

I recently tested Rich logging with a Django project and the results were good. Here's what it looks like:

A Django app with Rich logging

Note the file and line at the right hand of the terminal which shows were the log function was called. If your terminal supports hyperlinks, you can click that link to open the file!

For reference, here's what it looks like without Rich logging.

A Django app without Rich logging

This is the default RichHandler class in Rich. It would be possible to do a little more customization for Django logs. Maybe highlight 5xx in red, for example.

To add this to your Django project, pip install rich then configure the console to use "rich.logging.RichHandler" rather than StreamHandler.

Since my last post on Rich there have been a number of improvements. Here are the major highlights:

Progress bars now have an indeterminate state which displays a pulsing animation. This a fairly common thing from graphical UIs, here's what it looks like in the terminal:

To see it in action install Rich and run the following:

Or just watch this video.

You can use this new state to show when something is working, but before you know how many 'steps' are involved. For instance, when making a request over the internet, you won't know how many bytes you have to download until you get a response. Rather than show a progress bar at 0% you can show this pulse effect while waiting on the server. continue reading…

Since releasing Rich 1.0.0 I've discovered that terminals support hyperlinks. And I don't mean the helpful highlighting of URLs that most terminals do, I mean actual HTML-like hyperlinks that launch a browser.

This was news to me, as I've never seen this feature in the wild. I just had to add it to Rich. The easiest way to output hyperlinks is via console markup. Here's a simple example:

This will output the text "Visit my blog!" in the terminal where the word "blog" is a clickable link that opens the browser.

This is a relatively new (2017) addition to the spec, so it isn't supported on all terminal apps. If your terminal doesn't support hyperlinks then the link won't be clickable, but you will see the text as normal.

I'm happy to announce the release of version 1.0.0 of Rich - my Python library for fancy terminal rendering.

Rich is a project I started back in October 2019 when I was staying in Wuhan, a few weeks before it was locked down. Back then it was a toy project designed to pass the time while traveling. In the spirit of XKCD 927 I wanted to create a library that could display rich content in the terminal as effortlessly as you can with HTML. continue reading…

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. continue reading…

I've added progress bar support to Rich.

If you haven't seen my earlier posts on the subject, Rich is a terminal rendering framework for Python. It lets you render styled text and a whole bunch of other things (markdown, syntax, tables, tracebacks, etc.) to the terminal.

This latest addition to the lib renders progress bars with additional information such as percentage completed, time remaining, data transfer speed etc. It's highly configurable, so you can customize it to show whatever information you like. And since it's implemented as a Rich renderable, you can easily add color and formatting. Here's what it looks like:

continue reading…

One of my goals in writing Rich was to render really nice Python tracebacks. And now that feature has landed.

I've never found Python tracebacks to be a great debugging aid beyond telling me what the exception was, and where it occurred. In a recent update to Rich, I've tried to refresh the humble traceback to give enough context to diagnose errors before switching back to the editor.

Here's an example of a rich traceback:

Rich traceback on OSX continue reading…

Pretty log output

If you are a Python developer you may spend a large part of your day reading log output that looks like this:

Ugly log output

Front end tools like the Chrome developer console have far superior rendering for logs, but in the back-end we're stuck with an ancient technology that is older than most of the readers of this blog. I'm talking of course of the terminal which is likely to be the primary interface to back-end development until I retire.

Still I think we can do a little better. I wanted to use my terminal rendering library, Rich to render log output that is easier on the eye. The results are promising:

Pretty log output continue reading…

I was reading a post by Trey Hunner on why pathlib is great, where he makes the case that pathlib is a better choice than the standard library alternatives that preceded it. I wouldn't actually disagree with a word of it. He's entirely correct. You should probably be using pathlib were it fits.

Personally, however, I rarely use pathlib, because I find that for the most part, PyFilesystem is a better choice. I'd like to take some of the code examples from Trey's post and re-write them using PyFilesystem, just so we can compare.

The first example from Trey's post, creates a folder then moves a file into it. Here it is:

The code above is straightforward, and hides the gory platform details which is a major benefit of pathlib over os.path. continue reading…