Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nicely formatted python code in html files [duplicate]

Tags:

html

css

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.

like image 677
qouify Avatar asked May 09 '26 20:05

qouify


2 Answers

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>

like image 56
kiwirafe Avatar answered May 11 '26 12:05

kiwirafe


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

like image 42
Javad Nikbakht Avatar answered May 11 '26 11:05

Javad Nikbakht