Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Engaging Users With Animation

00:01 Engaging users with animation, while what you’ve seen so far is useful, there’s much more to Rich than just developer tools. Its prime purpose is to help you create interesting and attractive interfaces for end users.

00:14 Animations can be a powerful asset in this aim. Rich’s animation tools are designed to make use of context managers. Here you’ll do a quick review of what context managers are and how they work.

00:27 If you’d like more depth, check out this real Python course after you finish this one. A context manager is a mechanism for keeping track of resources. You can use it in any situation where a resource such as a file, socket, or thread must be allocated to a task and then later returned to the system.

00:46 A context manager in Python is invoked with the with keyword and is active within the subsequent indented block. When the code execution leaves the block, the tear-down actions are invoked automatically.

01:00 In the case of Rich animations, the resources are the timers and variables for keeping track of changes in the display. The Rich Library provides context managers to handle these resources.

01:10 You, the programmer, can largely ignore the complexities and trust the context manager to do what’s right. Rich has a Status class that you can use to display the status of your program.

01:23 The recommended way to use it is as a context manager, create a file named rich_status.py to investigate how this works.

01:41 do_something_important() simulates a long-running process.

01:56 This line starts the context manager. You call console.status() with two parameters, the message to display and the animated spinner that appears along with the message.

02:06 While the context block is active, your call to do_something_important() happens within the context manager’s scope, so the message and the spinner remain visible until that function returns five seconds later.

02:24 Finally, you announce success.

02:38 The status animation disappears, making way for the cheery announcement and an emoji. The spinner parameter in this example rendered an image of the rotating planet, but how can you find out what other spinners you can use?

02:52 You can see the numerous spinner animations by using another handy module-level demo.

03:03 You’ll see you have a wide choice of geometric animations plus more pictorial ones, such as clock, smiley, and weather. Similarly, the sunglasses syntax seen on screen shows how you can incorporate static emojis into inline text using colon emoji names.

03:21 You can insert emoji wherever Rich renders text, such as in a log statement, a prompt, or a table. And of course, Rich has thousands more emojis that you can use in this way.

03:33 As with the spinner images, you can display the full catalog of emoji names and images using yet another command-line demo. There

03:45 are far too many emoji available to fit on a single screen, so you’ll need to do a lot of scrolling to see all of them. If you’d prefer an online reference, then you can find spinners and emojis cataloged in the Rich documentation.

04:02 Animated spinners are okay if the wait is short, but for a lengthier process, you’ll want to give the user some indication of how long they can expect to wait. For this case, Rich’s Progress class has you covered.

04:13 Instances of this class keep track of one or more asynchronous tasks while displaying their progress through an animated bar, percent completed display, and an estimated time to completion.

04:25 On screen, you can see how to code. This first, Progress is imported from rich.progress. Progress is then used as a context manager, and tasks are added to it with add_task.

04:41 Here, the totals are arbitrary.

05:04 The Progress class animates and highlights the display as tasks progress. In this example, the tasks advance at different speeds. In a real application, you could call the update method for a task such as a file download with the advance parameter calculated from the number of bytes downloaded.

05:24 As you’ve already seen, console markup can be used in most strings displayed by Rich, including the progress descriptions seen here. The progress indicator demo is complete and run in a normal way.

05:42 You can see the three tasks progressing at different rates. And note that Rich calculates the percentage and ETA for you automatically.

05:55 As with most Rich classes, there are plenty of ways to customize the details of what Progress displays, and you can check out the details in the Rich documentation.

06:09 In the next section of the course, you’ll look at another use case for Rich’s features, bringing Tables to Life.

Become a Member to join the conversation.