Skip to content

Commit caef083

Browse files
authored
Add support for Pandas style (#196)
1 parent bc7520e commit caef083

File tree

9 files changed

+374
-65
lines changed

9 files changed

+374
-65
lines changed

docs/_toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ root: quick_start
33
chapters:
44
- file: supported_editors
55
- file: advanced_parameters
6+
- file: pandas_style
67
- file: custom_css
78
- file: downsampling
89
- file: sample_dataframes

docs/advanced_parameters.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ with pd.option_context("display.float_format", "${:,.2f}".format):
244244
show(pd.Series([i * math.pi for i in range(1, 6)]))
245245
```
246246

247+
```{tip}
248+
ITables in version 1.6.0+ can also render
249+
[Pandas Style](https://pandas.pydata.org/docs/user_guide/style.html)
250+
objects as interactive datatables.
251+
252+
This way, you can easily add background color, and even
253+
tooltips to your dataframes, and still get them
254+
displayed using datatables.net - see our [example](pandas_style.md).
255+
```
256+
247257
## Javascript formatting
248258

249259
Numbers are formatted using Pandas, then are converted back to float to ensure they come in the right order when sorted.
@@ -342,6 +352,13 @@ function (td, cellData, rowData, row, col) {
342352
)
343353
```
344354

355+
```{tip}
356+
Since `itables==1.6.0`, you can also render
357+
[Pandas style](pandas_style.md) objects as interactive datatables -
358+
that might be a simpler alternative to the JavaScript callbacks
359+
documented here.
360+
```
361+
345362
## Column width
346363

347364
The [`columnDefs.width`](https://datatables.net/reference/option/columns.width) argument let you adjust the column widths.

docs/changelog.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
ITables ChangeLog
22
=================
33

4-
1.6.0-dev (2023-09-??)
4+
1.6.0 (2023-09-30)
55
------------------
66

7+
**Added**
8+
- We have added support for [Pandas style](https://pandas.pydata.org/docs/user_guide/style.html) ([#194](https://github.com/mwouts/itables/issues/194))
9+
710
**Fixed**
8-
- We do not generate timedeltas in the sample dataframes when using `pandas==2.1` as this fails ([pandas-#55080](https://github.com/pandas-dev/pandas/issues/55080))
11+
- We do not generate timedeltas in the sample dataframes when using `pandas==2.1` as this fails ([pandas-55080](https://github.com/pandas-dev/pandas/issues/55080))
912

1013

1114
1.5.4 (2023-08-18)

docs/pandas_style.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
jupytext:
3+
formats: md:myst
4+
text_representation:
5+
extension: .md
6+
format_name: myst
7+
format_version: 0.13
8+
jupytext_version: 1.14.5
9+
kernelspec:
10+
display_name: itables
11+
language: python
12+
name: itables
13+
---
14+
15+
# Pandas Style
16+
17+
Starting with `itables>=1.6.0`, ITables provides support for
18+
[Pandas Style](https://pandas.pydata.org/docs/user_guide/style.html).
19+
20+
```{code-cell}
21+
import pandas as pd
22+
import numpy as np
23+
from itables import init_notebook_mode
24+
25+
init_notebook_mode(all_interactive=True)
26+
```
27+
28+
```{code-cell}
29+
:tags: [remove-input]
30+
31+
import itables.options as opt
32+
33+
opt.lengthMenu = [5, 10, 20, 50, 100, 200, 500]
34+
```
35+
36+
This is the DataFrame that we are going to style:
37+
38+
```{code-cell}
39+
x = np.linspace(0, np.pi, 21)
40+
df = pd.DataFrame({"sin": np.sin(x), "cos": np.cos(x)}, index=pd.Index(x, name="alpha"))
41+
42+
df
43+
```
44+
45+
## Color
46+
47+
From now on we will display `df.style`
48+
(a Pandas `Styler` object) rather than our DataFrame `df`.
49+
50+
Let's start with a background gradient:
51+
52+
```{code-cell}
53+
s = df.style
54+
s.background_gradient(axis=None, cmap="YlOrRd")
55+
```
56+
57+
## Format
58+
59+
We can also choose how the data is formatted:
60+
61+
```{code-cell}
62+
s.format("{:.3f}")
63+
```
64+
65+
## Caption
66+
67+
```{code-cell}
68+
s.set_caption("A Pandas Styler object with background colors").set_table_styles(
69+
[{"selector": "caption", "props": "caption-side: bottom; font-size:1em;"}]
70+
)
71+
```
72+
73+
## Tooltips
74+
75+
```{code-cell}
76+
ttips = pd.DataFrame(
77+
{
78+
"sin": ["The sinus of {:.6f} is {:.6f}".format(t, np.sin(t)) for t in x],
79+
"cos": ["The cosinus of {:.6f} is {:.6f}".format(t, np.cos(t)) for t in x],
80+
},
81+
index=df.index,
82+
)
83+
s.set_tooltips(ttips).set_caption("With tooltips")
84+
```
85+
86+
```{note}
87+
Unlike Pandas or Polar DataFrames, `Styler` objects are rendered directly using their
88+
`to_html` method, rather than passing the underlying table data to the datatables.net
89+
library.
90+
91+
Because of this, the rendering of the table might differ slightly from the rendering of the
92+
corresponding DataFrame. In particular,
93+
- The downsampling is not available - please pay attention to the size of the table being rendered
94+
- Sorting of numbers will not work if the column contains NaNs.
95+
```

0 commit comments

Comments
 (0)