9

What are the options to display python code in HTML pages?

I do not want to execute python code in the page, but only produce HTML/CSS pages displaying nicely formatted (with e.g., syntax highlighting) python code snippets as done, for instance, by stackoverflow.

Basically I would like a tool that takes a python file as input and generate an HTML/CSS code.

asked May 26, 2020 at 19:20
4
  • 2
    You can use either <pre> or <code> tags. Commented May 26, 2020 at 19:23
  • 1
    For syntax highlighting, what you're looking for is called a code prettifier. There's a lot of options for that available online, look into it and see if one fits your needs (Stack Overflow doesn't allow recommendation questions unfortunately). Commented May 26, 2020 at 19:24
  • @EternalHour : thanks for your answer. I did not know of theses tags Commented May 27, 2020 at 7:49
  • @john-montgomery : thank you. I did not know it was called like that (code prettifier). I have found a couple of tools that seem nice. Commented May 27, 2020 at 7:54

2 Answers 2

22

Update: I just discovered highlightjs.org, which supports over 189 languages and 91 styles, and way easier to use. There is a code sample below:

<head>
 <link rel="stylesheet"
 href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css">
 <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
 <script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
 <pre><code class="python">def fib(n):
 a, b = 0, 1
 while a < n:
 print(a, end=' ')
 a, b = b, a+b
 print()
 fib(1000)</code></pre>
</body>


Origina Post:

Try codemirror.net. More info click here and here from Stack Overflow

Or like @EternalHour said, you can use <pre>Your code here</pre> <code>Your code here</code>

sevan
3,9743 gold badges19 silver badges30 bronze badges
answered May 26, 2020 at 23:05
Sign up to request clarification or add additional context in comments.

Comments

2

you can use pygments for styling python codes in your html pages.

answered Apr 11, 2022 at 8:44

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.