manu
manu

Reputation: 1453

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?

Upvotes: 3

Views: 2141

Answers (2)

sasa
sasa

Reputation: 1

import tkinter as tk
from tkinterweb import HtmlFrame
import markdown2

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


converter = markdown2.Markdown(extras=["tables"])
html = converter.convert(markdown_text)


root = tk.Tk()
root.title("HTML Viewer")

html_frame = HtmlFrame(root)
html_frame.load_html(html)
html_frame.pack(fill=tk.BOTH, expand=True)

root.mainloop()

Upvotes: 0

Chris
Chris

Reputation: 136880

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))

Upvotes: 4

Related Questions