Syntax highlighting using Pygments
Often, as coders, we want to be able to display code in a web page, and while it is a skill to read that code without syntax highlighting, a few colors can make the reading experience much more pleasant.
As is always the way with Python, there is a module already available that is able to do that for you, and of course it can be installed easily by the following command:
pip install Pygments
Note
Pygments only works with the known sections of code. So, if you want to display code snippets, we can do that. If, however, you want to highlight inline sections of the code, we either follow the next section on Markdown, or we need to use some online Javascript such as highlight.js
.
To create code snippets, we need to first create a new blueprint. Let's create a directory called snippets
, then an __init__.py
file, followed by a blueprint.py
file with the following code:
from flask import Blueprint, request, render_template, redirect, url_for from helpers import object_list...