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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Get to Know the Rich Console

00:00 The Rich Console. As you’ve already seen, Rich can override print(), giving extra functionality. But the preferred way to use Rich is to initialize a Console object and use it for printing.

00:26 Again, this will output Rich in bold and red. Another useful console method is .rule(), which creates a rule across the width of the console.

00:41 By default, Rich will use the full width of your current window, but you can create a console with a fixed width if you want to control the layout more closely.

00:53 Now, the same .rule() call leads to a narrower rule on screen. One way you’ll use Rich to make your game look better is by clearing the screen between guesses. You do this with console.clear().

01:10 Add the code seen on-screen. First, you need to import rich and initialize a Console object at the top of the code with a width of 40.

01:28 In the refresh_page() function, console.clear() will clear the screen, then console.rule() will print a headline at the top of the screen.

01:44 One neat feature of Rich is that you can add custom styles. As an example, you can add a style to warn the user that they’ve done something wrong. You do this by instantiating Theme and passing it to Console. This

02:08 adds warning as a new style that will display as red text on a yellow background. Here, the code is run in the standard Python REPL, as bpython doesn’t play well when Rich clears the console.

02:24 First, refresh_page() is tested, and you can see that it clears the screen before the Wyrdl headline is printed. Next, Look at me! is printed with the warning style, red text on a yellow background.

02:40 Now that you have some skills at working with Rich, in the next section of the course, you’ll start using Rich to color-code some of the game’s output.

Become a Member to join the conversation.