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

Time:12-30

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 >{{snippet.body}}</pre>
{% endpygmentify %}
...

Final rendered HTML:

<pre lang="python">
...
<span >import</span> <span >hello</span>
<span >print</span><span >(</span><span >'hey'</span><span >)</span>
<span >def</span> <span >test</span><span >():</span>
<span >print</span><span >(</span><span >'in the function'</span><span >)</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.

CodePudding user response:

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

{% pygmentify linenos='inline' %}
<pre >{{snippet.body}}</pre>
{% endpygmentify %}
  • Related