Python Pygments library for Code Syntax Highlighting | Python Syntax Highlighting | Pygments

Python Pygments library for Code Syntax Highlighting | Python Syntax Highlighting | Pygments

Hello Friends! I hope you are doing well, May god peace be upon you.

In this article, we will learn small Python library Pygments for highlighting the code in your applications, I think it will be very interesting.

The Pygments library for the Python programming language is very useful, if you make a programming site or just some program where you need to output code, then you will definitely need it,

Installing the Python library Pygments:

Installation of the library occurs through the PIP rocket manager, For this purpose, it is necessary to register the command in the terminal:

pip install Pygments

Now you can work with it.

Basics of Python library Pygments:

Working with it is very easy, you just need to connect the library to your file and use several components, let’s first talk about what these components are:

  • Lexer breaks the string into tokens or source fragments that have a token type that determines what the text represents semantically (for example, a keyword, string, or comment). There is a lexer for each language or markup format that is supported in Pygments.
  • Tokens can be passed through filters that typically change token types or text fragments, such as all uppercase keywords.
  • The formatter takes a stream of tokens and writes it to an output file in a format such as HTML, LaTeX, or RTF.
  • The output defines a style on how to highlight all the different types of tokens. He compares them to attributes such as “red and fat.”

These are the main four components that you will use for work:

Now let’s show you one great example for clarity, this will be extremely simple:

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
code = 'print "Hello World"'
print(highlight(code, PythonLexer(), HtmlFormatter()))

As you can see at the beginning, we connect the Pygments library itself, after that, we additionally connect the lexer and formatting modules, and the latter creates a line in which we write a small and output formatted code.

To do this, we use the highlight() function, the first parameter is to use a string with code, the second is the lexer function, in our case for Python, and the third is to use the function for formatting, in our case for HTML.

Let’s see what we got in the terminal:

<div class="highlight">
<pre><span class="k">print</span> <span class="s">&quot;Hello World&quot;</span></pre>

That is, we have an HTML markup that you can use for example in Django templates if you use it, or just in HTML templates, here as you need.

Of course, this is not all, and you can thus do code highlighting for absolutely any language, this library supports an extremely large number of programming languages, and you will find everything you need.

you can also learn more in the documentation more about how to work with it and highlight which languages it supports.

I hope You have Learned Something from here.

Related Posts