Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Showing the line numbers in <pre> in Django project using Pygments

I’m using the Pygments package in my Django project. When I try to render the code snippet in my template, it renders the whole data as follows:

Template:
...
{% pygmentify %}
<pre class="{{snippet.lang}}">{{snippet.body}}</pre>
{% endpygmentify %}
...

Final rendered HTML:

<pre lang="python">
...
<span class="kn">import</span> <span class="nn">hello</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'hey'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'in the function'</span><span class="p">)</span>
...
</pre>

It actually works with no pain. The entire code block is being highlighted properly. The thing is that I want to show the line number as well. Should I style them or there is only a simple Pygments configuration needed?

Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you are using django-pygmentify, you can pass keyword arguments as indicated in their docs

{% pygmentify linenos='inline' %}
<pre class="{{snippet.lang}}">{{snippet.body}}</pre>
{% endpygmentify %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading