By default, itables (and many similar tools) treat your cell content as plain text and “escape” any HTML you might have in there. So if you have cells that contain Markdown formatting (for example, using bold or italic), they will show up literally rather than being rendered as formatted text.
There are generally two approaches to solve this:
1. Convert the Markdown to HTML First
The most common solution is to convert your Markdown strings into HTML (using Python’s markdown package or any similar library) and then tell itables not to escape HTML so that the resulting HTML is rendered.
For example:
import pandas as pd
import markdown
from itables import show
# Sample DataFrame with markdown strings
df = pd.DataFrame({
"text": [
"This is **bold** text",
"And here is _italic_ text",
"A [link](https://example.com)"
]
})
# Convert Markdown to HTML for each cell in the desired column(s)
df["text"] = df["text"].apply(lambda s: markdown.markdown(s) if isinstance(s, str) else s)
# Now display the DataFrame, making sure to disable escaping
show(df,
escape=False, # disable escaping so HTML is rendered
autoWidth=False,
lengthMenu=[1, 5, 10],
classes="compact",
showIndex=False)
In this example:
- We first convert the Markdown to HTML. Now the cell that was
"This is **bold** text" will become something like "<p>This is <strong>bold</strong> text</p>".
- We then call
show() with the parameter escape=False (if your version of itables supports it). This tells the underlying renderer not to escape HTML content and thus render your formatted text.
2. (If Available) Use a Renderer Option in itables
Some display libraries provide an option to customize the cell rendering (for example, by specifying DataTables column definitions) so that you can tell them “for this column, treat the cell content as HTML”. If itables offers such an option (check the latest documentation for your version), then you might configure it like this:
Note: This approach depends on whether itables exposes column configuration options. Often the simplest solution is simply to convert your markdown to HTML as shown above.
Summary
- Markdown to HTML conversion: Use a library such as
markdown to pre-convert your Markdown strings.
- Disable HTML escaping: When calling
show(), pass in the argument (e.g. escape=False) or other configuration that prevents HTML escaping so that the HTML is rendered in the notebook.
This combined approach should display your pandas DataFrame with the originally intended Markdown formatting rendered properly.