Python Rich - The BEST way to add Colors, Emojis, Tables and More...

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thanks /u/DevOps-Journey! Great video.

I’m the author of Rich, if anyone has any questions.

πŸ‘οΈŽ︎ 9 πŸ‘€οΈŽ︎ u/willm πŸ“…οΈŽ︎ Feb 02 2021 πŸ—«︎ replies

Hey Everyone! Long time Python user here and I only recently came across the module "Rich".

Honestly I don't know how I made it this far without the module. Not only is it really cool for making your output look a lot better with colored formatting but the features like "inspect" really help you dive into Python objects and help the Learning Process.

I made the video to help those unfamiliar with the Rich module get started. Please let me know if there is any major feature that I missed!

The official module repo is here if you're just interested in that:

https://github.com/willmcgugan/rich

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/DevOps-Journey πŸ“…οΈŽ︎ Feb 01 2021 πŸ—«︎ replies

I rly love to use it

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/iiMoe πŸ“…οΈŽ︎ Feb 02 2021 πŸ—«︎ replies

Probably because it's fairly new? Is it not still in some beta testing? Either way, it's great. Denver to turn on console emulation if u want to use it within Pycharm

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/SpaceZZ πŸ“…οΈŽ︎ Feb 01 2021 πŸ—«︎ replies

Link to your YouTube channel?

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/foodwithmyketchup πŸ“…οΈŽ︎ Feb 02 2021 πŸ—«︎ replies
Captions
what's up everyone today we're going to have a look at python rich which is my favorite module in python this module is so powerful that i use it in every single script that i develop colors formatting progress bars logging and errors emojis all these features will bring your python game to the next level as a developer and the best part is all these features are easily implemented with just a few lines of code once you learn them it's just a matter of copy and pasting the code into your scripts in this tutorial i'm going to show you everything you need to know to get started with rich and start implementing these features into your own code let's go ahead and get started okay so the first thing you want to do when you're getting started with rich is install the module so we'll go pip install rich and this is going to go out and install the rich module once you do that we can clear the screen here and then we'll just go python dash m rich and this is going to call the modules help so we'll go through here and i'll go ahead and explain these things so this gives us all the options that are available through the rich module so we can see that it has support for all different types of colors here you have a color palette you can basically pick any of these colors and use them in the terminal you have the styles so you can do bold text dim text italic underline reverse and even blinking text it looks like there's options for word wrap and justifying it and this gives you an idea what that would look like the next thing is it supports asian languages we have uh i think cantonese japanese and korean and then we have uh markup which supports emojis so you can do emojis in your programs this next one's pretty cool it has support for tables and you can add a bunch of color to your tables this one over here santax highlighting it actually looks pretty cool when you implement it but i haven't really found a reason to do it i'll go ahead and show what it looks like further on in the video the next thing is it has support for markdown so you can use markdown in your strings and when you print it out it's going to have all the markdown elements within it at the bottom here it says it has progress bars logging handlers and tracebacks so the progress bars are really cool the logging handler is great so i'll show that as well and the tracebacks really help you out when you're troubleshooting python scripts makes your errors look a lot more readable all right so let's get started with this module i'm going to hop into a python console and the first thing i'm going to do is i'm going to go from rich import print and this is the rich modules version of print and you're going to be able to add colors when you print and when you print out data types it's going to look so much better so let's go ahead and get started here let's do a print and then hello world and let's redo this here and let's add a color so we'll say blue to both sides and print it out and you can see that's how easy it is to add color if we wanted to add red we just put red here and you can see how easy it is to add color to your print statements now if you want to do an emoji we can rerun this print and the way to do emojis is with these colons and then put in the emoji name so we'll do vampire and uh let's just check another one let's do poop not sure what you would use that for but maybe it's a good in a log statement or something now the next thing i'm going to print out is uh let's do locals so this command basically prints out all your local variables within python you can see i printed out here just normally and there's no highlighting so it's a little tough to read but if i go print locals using the rich print module you can see that it sort of does it like a pretty print style and it also adds in the color so it's much easier to read now you can do this with strings lists and dictionaries as well basically it's going to take any python object print it out nicely and spice it up with some colors to make it that much easier to read now if i want to take this a step further i don't want to type the print statement i just want to type in a variable name and i want it to print nicely to the python console all i need to do to do that is import the pretty function of rich so let's go from rich import pretty and then we'll go pretty dot install and now whenever i type in a variable or just a python object it's going to automatically print it out in this nice format so i love doing this when i'm troubleshooting in the python idle console and whenever i print things it'll print it out in this nice format and it makes troubleshooting so much easier all right so another important feature of the rich module is the inspect function and i use this all the time so basically everything in python is an object and those objects have methods on it and just attributes about it so if we use this inspect function we can learn a lot about the object so i'm going to do a from rich import inspect and now i can go inspect and let's just put in a string here so i'll go asdf and you need to type it correctly that's a mistake i make all the time but that looks good so it sort of prints it out nicely it gives the string there it says there's 33 attributes that aren't shown and just tells you that's a string if you want to show those attributes you go methods equal true and you can see all the string methods so you can see that you can turn that string into a title you can change it to all uppercase you can check what the string starts with you can strip it so basically it just tells you all the string methods so you don't have to go out to google and check it okay so the next thing i'm going to show here is the console print statement and this is really similar to the print statement that i showed earlier but this is actually the correct way to print to the console it's got more features in it it word wraps automatically and whenever you need to print to the console this is the actual way that you should be printing to it so to implement it go from rich.console import console and then create your own console object so i'll call mine console and i'll go equals console parentheses and now i have a console object to work with so i'm going to go ahead and paste in some code here so basically it's going to print hello world and the way we style it is we just specify the style and go we'll go bold red so it's going to print in bold red now if i were to take this and print it out the same way that i did before that still works as another option now the other cool feature with console is instead of doing like a console print you can do a console log so let's change it from print and just go log and you can see that it adds a nice little timestamp there so this is a nice way to get a color timestamp without having to use the full-blown python logging module now if you are using the python logging module rich provides a logging handler for it so i have a new python terminal open here and i'm going to paste in some code and you can see that this is using the official python logging module and all we're doing is adding the rich handler to it so all this does is take the python logging module and enhances it with color so we can see the statement here log dot info hello world and when we print it out we get some nice color to it and this has all your logging type messages so we could change this to critical and it comes out highlighted red you can do a debug and it'll probably be green yeah that looks green and then you do a warning and it comes out red so with really just one line of code it brings a lot of life into your basic logging module now if you're not familiar with what the logging module is in python i have a full video on it so go ahead and check that out i'll put a link up at the top okay so tables are actually pretty self-explanatory you can go on to the github and just grab the code from there basically all you need to do is create this table object uh add a style to it then start adding columns and then rows and then you should get something that looks like this so i'm going to take this code and copy it and throw it into my python console and you can see that it's that easy to create a table and if i wanted to put my own information all i would have to do is go in and tweak these parameters so it should be good for you guys as well to just grab the code put it in and change the elements that need to be changed and then add your own styling to it okay so this next thing is really cool here we can actually add progress bars to our application and it's really simple to do i'm going to paste in some code here and just a quick look at the code at the top we're just and just a quick look at our code here at the top we're importing the sleep function and from rich we're importing track and then at the bottom here we have a basic for loop using a range so it's basically counting one to ten it's going to sleep for one second and then it's going to tell us the progress so i'm going to go ahead and run this loop here and you can see it's counting up to 10 and as it goes it increments this progress bar so you could do this with any type of loop within your application and it's going to let your end users know how close it is to finishing that loop so a really cool and simple feature to implement on your next application so another really cool thing that's similar to progress bars is the status spinner so i'm going to paste in some code here and this code is very similar to what we ran for our progress bar but in this one we're using console.status and we're performing a while loop and for each iteration in that while loop it's going to show a status icon spinning so this loop counts up to 10 so let's go ahead and run it here and you can see that says working on tasks and it's just spinning around as it counts up so this is a very cool way to indicate to your users that your python script is working and they just need to sit tight until your script is finished all right so the last thing i'm going to go over here is error tracebacks and this is actually one of my favorite features of the rich module i know you're all great programmers but i'm sure you run into errors when you're programming with python and sometimes those errors that come out of python aren't that easy to read so what rich does is it takes those errors and it just sort of makes them look a lot better and more easy to read and diagnose what the problem is so i'm going to go ahead and go one divided by zero and this is going to generate an error and this is what an error looks like in python by default so not much to it and not that easy on the eyes the tracebacks can get especially complicated as your code gets more advanced and this is where a module like rich can really help us out so it's just two lines of code here we're first gonna import the rich traceback module and then we're just gonna type install and this is gonna set up your python environment to give good trace packs so let's go ahead and divide by zero again and you can see how much better that looks i use this for all my production applications and it really makes troubleshooting so much easier anyways that's all i have for this video if you enjoyed it please go ahead and hit that like button for me and if you're interested in learning more about python and devops or just it in general go ahead and check out the other videos on my channel thanks so much for watching and i hope to see you all in the next video
Info
Channel: DevOps Journey
Views: 8,787
Rating: undefined out of 5
Keywords: python rich module, python rich text, python rich tutorial, python colors, python colorama, python color text, python progress bars, python logging, python logging handlers, python logging advanced tutorial, python tutorial, python learning, python learning tutorial, python colorama tutorial, python colorama install
Id: JrGFQp9njas
Channel Id: undefined
Length: 12min 8sec (728 seconds)
Published: Mon Feb 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.