Thursday, February 10, 2011

Syntax-Highlighting Python Code in Blogger using Style Sheets (CSS)

I want nicely-formatted, syntax-highlighted Python code interspersed with regular text. So:

  • In Blogger, I edited my Template, which is a style sheet with fancy extra stuff.
  • I went to the Python website and used Google Chrome's Developer Tools to view the page's style sheets.
  • I copied a short snippet of HTML into my blog post, making sure to add the necessary style sheet entries into my Template.
Here's a test of that:

>>> # try to access an undefined variable 
... n 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
NameError: name 'n' is not defined 

So, how did it go?

It looks good! I'm excited. The next step is to import the entire Python style sheet, and find a tool that'll convert snippets of Python code (from a file, or straight from the interpreter) into HTML.

Yay!

1 comment: