Skip to content

Commit a7c39b0

Browse files
authored
a11y - Implement @smeragoel's table designs (#1757)
* Implement @smeragoel's data table designs * Working for nbsphinx (plus inside ipywidget) * Add outer and column borders to data table * Rename color vars * Extend data-table styles to all tables * Apply suggestions from code review
1 parent 907c596 commit a7c39b0

File tree

5 files changed

+131
-14
lines changed

5 files changed

+131
-14
lines changed

docs/examples/pydata.ipynb

+48-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
"\n",
99
"This theme has built-in support and special styling for several major visualization libraries in the PyData ecosystem.\n",
1010
"This ensures that the images and output generated by these libraries looks good for both light and dark modes.\n",
11-
"Below are examples of each that we use as a benchmark for reference.\n",
12-
"\n",
11+
"Below are examples of each that we use as a benchmark for reference."
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
1318
"## Pandas"
1419
]
1520
},
@@ -30,6 +35,46 @@
3035
"df"
3136
]
3237
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## IPyWidget"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"import pandas as pd\n",
52+
"import numpy as np\n",
53+
"import ipywidgets as widgets\n",
54+
"from IPython.display import display\n",
55+
"\n",
56+
"tab = widgets.Tab()\n",
57+
"\n",
58+
"descr_str = \"Hello\"\n",
59+
"\n",
60+
"title = widgets.HTML(descr_str)\n",
61+
"\n",
62+
"# create output widgets\n",
63+
"widget_images = widgets.Output()\n",
64+
"widget_annotations = widgets.Output()\n",
65+
"\n",
66+
"# render in output widgets\n",
67+
"with widget_images:\n",
68+
" display(pd.DataFrame(np.random.randn(10,10)))\n",
69+
"with widget_annotations:\n",
70+
" display(pd.DataFrame(np.random.randn(10,10)))\n",
71+
"\n",
72+
"tab.children = [widget_images, widget_annotations]\n",
73+
"tab.titles = [\"Images\", \"Annotations\"]\n",
74+
"\n",
75+
"display(widgets.VBox([title, tab]))"
76+
]
77+
},
3378
{
3479
"cell_type": "markdown",
3580
"metadata": {},
@@ -143,7 +188,7 @@
143188
},
144189
"language_info": {
145190
"name": "python",
146-
"version": "3.10.8"
191+
"version": "3.11.6"
147192
}
148193
},
149194
"nbformat": 4,

src/pydata_sphinx_theme/assets/styles/abstracts/_mixins.scss

+34
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,37 @@
2121
border-radius: 0.25rem;
2222
padding: 0.5rem;
2323
}
24+
25+
@mixin table-colors {
26+
color: var(--pst-color-table);
27+
border: 1px solid var(--pst-color-table-outer-border);
28+
29+
th,
30+
td {
31+
~ th,
32+
~ td {
33+
border-left: 1px solid var(--pst-color-table-inner-border);
34+
}
35+
}
36+
37+
thead {
38+
tr {
39+
background-color: var(--pst-color-table-heading-bg);
40+
border-bottom: 2px solid var(--pst-color-primary);
41+
}
42+
}
43+
44+
tbody {
45+
tr {
46+
&:nth-child(odd) {
47+
background-color: var(--pst-color-table-row-zebra-low-bg);
48+
}
49+
&:nth-child(even) {
50+
background-color: var(--pst-color-table-row-zebra-high-bg);
51+
}
52+
&:hover {
53+
background-color: var(--pst-color-table-row-hover-bg);
54+
}
55+
}
56+
}
57+
}

src/pydata_sphinx_theme/assets/styles/content/_tables.scss

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
// ensure table will fit in the article width and make them y-scrollable
66
table {
7+
@include table-colors;
8+
79
display: table;
810
overflow: auto;
911

@@ -46,6 +48,8 @@ td {
4648

4749
// override bootstrap table colors
4850
.table {
51+
@include table-colors;
52+
4953
--bs-table-bg: transparent; //background
5054
--bs-table-color: var(
5155
--pst-color-text-base

src/pydata_sphinx_theme/assets/styles/extensions/_notebooks.scss

+29-11
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ html .jp-RenderedHTMLCommon {
1717
}
1818
}
1919

20-
// Dark theme special-cases
21-
html[data-theme="dark"] .bd-content {
22-
.nboutput {
23-
.output_area.rendered_html {
24-
@include cell-output-background;
20+
.bd-content .nboutput {
21+
.output_area {
22+
&.rendered_html,
23+
.jp-RenderedHTMLCommon {
24+
// pandas
25+
table.dataframe {
26+
@include table-colors;
27+
}
2528
}
2629

27-
.output_area.stderr {
28-
background-color: var(--pst-color-danger);
30+
// Dark theme special-cases
31+
html[data-theme="dark"] & {
32+
&.rendered_html:not(:has(table.dataframe)),
33+
// ipywidgets
34+
.widget-subarea {
35+
@include cell-output-background;
36+
}
37+
38+
&.stderr {
39+
background-color: var(--pst-color-danger);
40+
}
2941
}
3042
}
3143
}
@@ -44,11 +56,17 @@ div.cell_output .output {
4456
overflow-x: auto;
4557
}
4658

47-
// Dark theme special-cases
48-
html[data-theme="dark"] .bd-content {
49-
div.cell_output {
59+
.bd-content div.cell_output {
60+
// pandas
61+
table.dataframe {
62+
@include table-colors;
63+
}
64+
65+
html[data-theme="dark"] & {
5066
img,
51-
.text_html {
67+
.text_html:not(:has(table.dataframe)),
68+
// ipywidgets
69+
.widget-subarea {
5270
@include cell-output-background;
5371
}
5472
}

src/pydata_sphinx_theme/assets/styles/variables/_color.scss

+16
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ $pst-semantic-colors: (
209209
"light": #f3cf95,
210210
"dark": #675c04,
211211
),
212+
"table": (
213+
"light": #{map-deep-get($color-palette, "foundation", "black")},
214+
"dark": #{map-deep-get($color-palette, "foundation", "white")},
215+
),
216+
"table-row-hover": (
217+
"bg-light": #{map-deep-get($color-palette, "violet", "300")},
218+
"bg-dark": #{map-deep-get($color-palette, "violet", "600")},
219+
),
220+
"table-inner-border": (
221+
"light": #{map-deep-get($color-palette, "gray", "200")},
222+
"dark": #364150,
223+
),
212224
// DEPTH COLORS - you can see the elevation colours and shades
213225
// in the Figma file https://www.figma.com/file/rUrrHGhUBBIAAjQ82x6pz9/PyData-Design-system---proposal-for-implementation-(2)?node-id=1492%3A922&t=sQeQZehkOzposYEg-1
214226
// background: color of the canvas / the furthest back layer
@@ -271,6 +283,10 @@ $pst-semantic-colors: (
271283
& {
272284
--pst-color-link: var(--pst-color-primary);
273285
--pst-color-link-hover: var(--pst-color-secondary);
286+
--pst-color-table-outer-border: var(--pst-color-surface);
287+
--pst-color-table-heading-bg: var(--pst-color-surface);
288+
--pst-color-table-row-zebra-high-bg: var(--pst-color-on-background);
289+
--pst-color-table-row-zebra-low-bg: var(--pst-color-surface);
274290
}
275291
// adapt to light/dark-specific content
276292
@if $mode == "light" {

0 commit comments

Comments
 (0)