Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show syntax-highlighted Python code in a HTML-page?

Tags:

python

html

css

Is it somehow possible to show syntax-highlighted python code in a webpage?

I found this:

<pre class="brush: python">
    # python code here
</pre>

However, it shows all the code in black. I want import to be orange, strings to be green.

Is it possible to do this?

Thank you!

like image 480
aadev151 Avatar asked Jul 11 '20 06:07

aadev151


People also ask

Can I embed python code in HTML?

The new PyScript project lets you embed Python programs directly in HTML pages and execute them within the browser without any server-based requirements.

How do I display code snippets in HTML?

HTML provides many methods for text-formatting but <code> tag is displayed with fixed letter size, font, and spacing. Some points about <code> tag: It is mainly used to display the code snippet into the web browser. This tag styles its element to match the computer's default text format.


Video Answer


1 Answers

If you wish to only display code, python in this case, consider using Github gist.

You can then embed it using the 'embed' option on the top right corner. It will give you a script tag that you can copy and add to your webpage like so:

<script src="https://gist.github.com/username/a39a422ebdff6e732753b90573100b16.js"></script>
like image 183
Parth Shah Avatar answered Oct 21 '22 15:10

Parth Shah