Rich is a Python library for writing rich text (with color and style) to the terminal. It lets you display advanced content such as tables, markdown, and syntax-highlighted code. Show
So, why is this useful? Well, if you're not using a tool like Rich, the output of your code on the terminal can be a little boring and difficult to understand. If you want to make it clearer and prettier, you probably want to use Rich – and you've come to the right place to learn how to do it. How to Install RichYou can install Rich with pip as:
To know what all Rich can do, you can type the following command in the terminal:
Now you can see that we can do quite a lot of things with Rich. Let's try a few of them out to see how they work. How to Rich print in PythonRich has the capability to highlight the output according to the datatype. We'll import the alternative 2 function from the Rich library which takes the same arguments as the built-in 2.To avoid confusion with the built-in 2 function, we'll import 2 from the 6 library as 7.
Output: Do you see how the different data types are highlighted with different colors? This can help us a lot while debugging. How to Rich inspect in PythonIf you use the built-in 8 function for viewing the documentation of a library, you'll see a boring output.
Output: Rich has an function which can generate a report on any Python object. It is a fantastic debug aid, and a good example of the output that Rich can generate.
Output: How to style your console with RichFor complete control over terminal formatting, Rich offers a class. Let's write a function to merge Python dictionaries.
Output: In the above example, we have used the 1 method that offers the same capabilities as print, but adds some features useful for debugging a running application.There are several other methods such as 2, 3, 4, 5, and so on. Learn more about them here.How to use Tree in RichRich has a class which can generate a tree view in the terminal. A tree view is a great way of presenting the contents of a filesystem or any other hierarchical data. Each branch of the tree can have a label which may be text or any other Rich renderable. Let's see an example by creating a family tree:
Output: Once we create an instance of the 6 class, we can use the 8 method to add branches to it. To create a complex tree, you just use the 8 method to add more branches to it. Notice the Brother and Sister branch in the above example.In the official documentation, we have a tree.py file that outputs the file structure using Tree. The output looks like this: How to display a progress bar using RichRich can show continuously updated information about the status of long-running tasks, file copies, and so forth. You can customize this information, too. By default, it provides a description of the 'task,' a progress bar, percentage complete, and anticipated time left. Multiple tasks are supported with a rich progress display, each with a bar and progress statistics. You can use this to keep track of several jobs that are being worked on in threads or processes. Let's first try the 0 method to create the progress bar.
Output: If we want to record the time when a particular task is finished executing, we can use 1 instead.
You can work directly with the Progress class if you need several tasks in the display or want to customize the columns in the progress display. After you've created a Progress object, use ( 2) to add task(s) and ( 3) to update progress.The Progress class is intended to be used as a context manager, automatically starting and stopping the progress display.
Output: How to display Rich Columns in PythonRich can render text or other Rich renderables in neat columns with the class. To use, construct a Columns instance with an iterable of renderables and print it to the Console. 0Output: How to display Rich tables in PythonRich’s class offers a variety of ways to render tabular data to the terminal. This class has 6 and 7 methods to add column and row respectively to the table instance created from the 5 class.Let's create a table for our todo list. This table will have three columns – S.No., Task, and Status. 1Output: Wrapping UpIn this tutorial, we learned how to use Rich to beautify the terminal. There are lots of other features that Rich supports. Learn more about them in the official documentation. Feel free to fork and play with the source code of this article here. Thanks for reading! Subscribe to my newsletterADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT Application Developer at Thoughtworks India If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started Bagaimana memulai python?Buka terminal CTRL + ALT + T.. Ketik python maka Anda akan masuk ke Python shell.. Tuliskan script Python Anda, contoh: print("Selamat datang di Python") . jika sudah tekan tombol ENTER , dan script Python akan dijalankan/eksekusi.. Untuk keluar dari Python shell ketik exit(). Apa itu struktur data di python?Struktur data primitif merupakan struktur data dasar pada python yang berisi nilai data murni, sederhana, dan berfungsi sebagai blok untuk memanipulasi data. Ada empat variabel pada struktur data primitif, yaitu integer (bilangan bulat), float, string, dan boolean.
Bagaimana cara membuat file baru di python?1.1 Membuat File Baru/Proyek Python Menggunakan PyCharm
Isi saja nama proyeknya “BelajarPython”. Lalu tekan Create. Selanjutnya, silahkan tambahkan file python dengan klik kanan pada direktori proyek, kemudian pilih New -> Python File. Setelah itu, berikan nama filenya dengan Latihan-1 lalu tekan Enter.
Append python buat apa?Append. Salah satu fitur dalam array python yang cukup sering digunakan adalah fungsi append. Fungsi append ini berguna untuk menambahkan nilai array pada urutan terakhir. Fungsi ini sedikit berbeda dengan fungsi insert, dimana fungsi insert bisa menambahkan nilai array pada posisi tertentu.
|