Advanced parameters#
The itables
package is a wrapper for the Javascript datatables.net library, which has a great documentation, a huge collection of examples, and a useful forum.
Below we give a few examples of how the datatables.net examples can be ported to Python with itables
.
As always, we initialize the itables
library with
from itables import init_notebook_mode, show
init_notebook_mode(all_interactive=True)
Then we create two sample dataframes:
import pandas as pd
from itables.sample_dfs import get_countries
df_small = pd.DataFrame({"a": [2, 1]})
df = get_countries()
Position and width#
The default value for the table CSS is table-layout:auto;width:auto;margin:auto;caption-side:bottom
.
Without width:auto
, tables with few columns still take the full notebook width in Jupyter.
Using margin:auto
makes non-wide tables centered in Jupyter.
You can change the CSS used for a single table with e.g.
show(df_small, style="table-layout:auto;width:50%;float:right")
a |
---|
Loading... (need help?) |
or you can also change it for all tables by changing itables.options.style
:
import itables.options as opt
opt.style = "table-layout:auto;width:auto"
Theme#
Select how your table looks like with the classes
argument (defaults to "display nowrap"
) of the show
function, or by changing itables.options.classes
.
Add "compact"
if you want a denser table:
show(df, classes="display nowrap compact")
region | country | capital | longitude | latitude | flag | |
---|---|---|---|---|---|---|
code | ||||||
Loading... (need help?) |
Remove "nowrap"
if you want the cell content to be wrapped:
show(df, classes="display")
region | country | capital | longitude | latitude | flag | |
---|---|---|---|---|---|---|
code | ||||||
Loading... (need help?) |
More options like "cell-border"
are available:
show(df, classes="display nowrap cell-border")
region | country | capital | longitude | latitude | flag | |
---|---|---|---|---|---|---|
code | ||||||
Loading... (need help?) |
Caption#
You can set additional tags
on the table like e.g. a caption:
show(df, "Countries from the World Bank Database")
region | country | capital | longitude | latitude | flag | |
---|---|---|---|---|---|---|
code | ||||||
Loading... (need help?) |
The caption appears at the bottom of the table by default. This is governed by caption-side:bottom
in the style
option which you can change. You can also override the location of the caption in the caption tag itself:
show(
df,
tags='<caption style="caption-side: top">Countries from the World Bank Database</caption>',
)
region | country | capital | longitude | latitude | flag | |
---|---|---|---|---|---|---|
code | ||||||
Loading... (need help?) |