The itables module in Python allows you to render interactive tables (using DataTables.js) inside Jupyter notebooks. However, it doesn’t have built-in APIs specifically for styling grid lines between cells or adding alternating cell-specific styles directly from Python.
That said, the appearance of tables (like adding lines, alternating colors, etc.) can be customized using CSS, and itables supports applying your custom CSS.
Here’s how you can add lines between cells or alternate cell background colors using custom CSS in itables.
✅ 1. Basic Setup of itables
If you haven’t installed itables yet:
pip install itables
Then enable it:
from itables import init_notebook_mode
init_notebook_mode(all_interactive=True)
✅ 2. Add Styling (Grid Lines or Cell Color Alternation)
To add grid lines or alternate cell colors, you can define your own CSS and inject it into the Jupyter Notebook.
▶️ Example: Set Grid Lines Between Cells
from IPython.core.display import HTML
HTML('''
<style>
.dataTable tbody td {
border: 1px solid #ccc !important; /* grid lines */
padding: 4px;
}
</style>
''')
This makes each cell have a light gray border, looking like grid lines.
▶️ Example: Alternate Cell Background Colors
HTML('''
<style>
.dataTable tbody tr:nth-child(even) td {
background-color: #f9f9f9 !important; /* light gray */
}
.dataTable tbody tr:nth-child(odd) td {
background-color: #ffffff !important; /* white */
}
</style>
''')
This alternates row colors to visually separate them — very helpful for readability.
✅ 3. Show a Table with itables
import pandas as pd
from itables import show
df = pd.DataFrame({
"A": range(5),
"B": ["foo", "bar", "baz", "qux", "quux"]
})
show(df)
This will now render the interactive table with custom styling applied.
✅ Advanced: Cell-Based Color Customization (Per-Cell)
For true per-cell custom styling, you'd need to bypass itables and use either plain HTML or combine with qgrid, IPython.display.HTML, or similar, because DataTables.js primarily supports row-based rendering through its API.
✅ Summary
| Feature | Can Do in itables? | Method |
|---|
| Grid lines between cells | ✅ Yes | CSS override |
| Alternate row shading | ✅ Yes | CSS using :nth-child() |
| Per-cell styling | ⚠️ Limited | Requires custom rendering |
Would you like an example of fully customized per-cell color rendering using only HTML or another library like qgrid or plotly?