Rendering Markdown file
We can even render markdown content in the terminal with a simple markdown extension in the rich library in python. Here we will be using a simple markdown file to demonstrate the working of the rich markdown rendering. The source code sample is provided in the documentation and here we need to make a few changes to make it work.
Python3
from rich.console import Console from rich.markdown import Markdown console = Console() with open ( "sample.md" ) as readme: markdown = Markdown(readme.read()) console. print (markdown) |
A sample markdown file has been provided below:
sample.md
# Sample markdown file Text with some paragraph 2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists look like: * this one * that one * the other one > Block quotes ------------ Here's a numbered list: 1. first item 2. second item 3. third item ```python n = 5 a = "GFG" for i in range(0, 3): print("Something") ``` ### An h3 header ### Tables can look like this: size material color ---- ------------ ------------ 9 leather brown 10 hemp canvas natural 11 glass transparent
Output:
Here, we will instantiate the console class since the output will be on the terminal/console. Thereafter we can open the markdown file in the local repository and parse it in the Markdown function that will render the contents and after reading the content of the file we can print the rendered content in the console.
Installing and Using Rich Package in Python
In this article, We are going to learn how to install and use rich packages in Python.
RIch is a python package for creating some awesome terminal formatting and logging. It has several features and functions that can make your application look nicer and even add a new look to your CLI application. We will be understanding the process of installing and basic usage of the RICH package in Python in this article.