13

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!

2

2 Answers 2

6

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>
5
  • 1
    @aadev151 Glad I could be of help. Please upvote/accept the answer, helps with your rep too :)
    – Parth Shah
    Commented Jul 11, 2020 at 17:32
  • is this still working? I get 404 when I try to see the js source script
    – Rotail
    Commented Nov 14, 2022 at 15:19
  • 1
    Replace the value for src with the gist URL you wish to display
    – Parth Shah
    Commented Nov 15, 2022 at 6:35
  • Awesome! It works now but does not show up with python color highlights and colors (the script is all black and flat).. Is there any way I can get that to work?
    – Rotail
    Commented Nov 18, 2022 at 22:21
  • I learned when I update .txt to .py, the color code is there but it shows up in a vertical format. a = 2 print ( a * 2 ) --> url of my page using gist: saeedmirshekari.com/blog/test
    – Rotail
    Commented Nov 18, 2022 at 22:25
5

I use prism.js

It looks Like this, it has many features

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.