CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 03:39:15 GMT
server: Fly/6f91d33b9d (2025-10-08)
content-type: text/html; charset=utf-8
content-encoding: gzip
via: 2 fly.io, 2 fly.io
fly-request-id: 01K7B80JZ5JDJRJGNGN58T6QP0-bom
Syntax highlighting Python console examples with GFM | Simon Willison’s TILs
Syntax highlighting Python console examples with GFM
It turns out GitHub Flavored Markdown can apply syntax highlighting to Python console examples, like this one:
>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
The trick is to use the following:
```pycon
>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
```
I figured out the pycon
code by scanning through the languages.yml file for linguist, the library GitHub use for their syntax highlighting.
While writing this TIL I also learned how to embed triple-backticks in a code block - you surround the block with more-than-three backticks (thanks to this tip):
````
```pycon
>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
```
````
Related
- datasette Syntax highlighted code examples in Datasette - 2023-07-01
- markdown Rendering Markdown with the GitHub Markdown API - 2020-08-22
- markdown Useful Markdown extensions in Python - 2021-04-03
- sphinx Format code examples in documentation with blacken-docs - 2022-04-24
- llms Expanding ChatGPT Code Interpreter with Python packages, Deno and Lua - 2023-04-30
- sphinx literalinclude with markers for showing code in documentation - 2024-01-10
- python Debugging a Click application using pdb - 2020-09-03
- gpt3 GPT-4 for API design research - 2023-04-06
- python Using cog to update --help in a Markdown README file - 2021-11-18
- github-actions GitHub Actions job summaries - 2022-05-17
Created 2021-01-18T17:21:26-08:00 · Edit