Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Markdown tables to HTML using markdown2

I'm trying to (programmatically) convert Markdown tables to HTML using markdown2, but it's not working. I take it should because markdown2 is supposed to be a

complete implementation of Markdown in Python

A minimal non-working example (meant to be run in a Jupyter notebook to properly display the output):

import markdown2
import IPython.display

markdown_text = '''
| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | 1600 |
| col 2 is      | centered      |   12 |
| zebra stripes | are neat      |    1 |
'''

display(IPython.display.Markdown(markdown_text))

converter = markdown2.Markdown()
html = converter.convert(markdown_text)

display(IPython.display.HTML(html))

Any clue?

like image 358
manu Avatar asked Feb 05 '26 04:02

manu


1 Answers

Tables aren't part of the original Markdown specification. Try enabling the relevant extra:

import markdown2
import IPython.display

markdown_text = '''
| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | 1600 |
| col 2 is      | centered      |   12 |
| zebra stripes | are neat      |    1 |
'''

display(IPython.display.Markdown(markdown_text))

converter = markdown2.Markdown(extras=["tables"])  # <-- here
html = converter.convert(markdown_text)

display(IPython.display.HTML(html))
like image 142
Chris Avatar answered Feb 07 '26 20:02

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!