Skip to content

Commit e414fcd

Browse files
authored
Merge pull request #215 from PokkeFe/issue-202-interactive
SpanArray HTML rendering interactivity and view instance tools
2 parents 3aa3f65 + 8db368c commit e414fcd

File tree

3 files changed

+286
-58
lines changed

3 files changed

+286
-58
lines changed

text_extensions_for_pandas/jupyter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ def pretty_print_html(column: Union["SpanArray", "TokenSpanArray"],
150150
show_offset_string = 'true' if show_offsets else 'false'
151151

152152
return textwrap.dedent(f"""
153+
<style class="span-array-css">
154+
{textwrap.indent(style_text, ' ')}
155+
</style>
153156
<script>
154157
{{
155158
{textwrap.indent(script_text, ' ')}
156159
}}
157160
</script>
158-
<style>
159-
{textwrap.indent(style_text, ' ')}
160-
</style>
161161
<div class="span-array">
162162
If you're reading this message, your notebook viewer does not support Javascript execution. Try pasting the URL into a service like nbviewer.
163163
</div>

text_extensions_for_pandas/resources/span_array.css

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,33 @@
11
.span-array {
2-
--thead-background-color: var(--jp-layout-color4, inherit);
3-
--thead-text-color: var(--jp-ui-font-color4);
4-
--tbody-background-color-1: var(--jp-layout-color2, inherit);
5-
--tbody-background-color-2: var(--jp-layout-color1, inherit);
6-
--tbody-background-color-hover: var(--jp-layout-color3, inherit);
7-
--tbody-background-color-disabled: var(--jp-layout-color4, inherit);
2+
--thead-background-color: var(--jp-layout-color1, inherit);
3+
--thead-text-color: var(--jp-ui-font-color1, inherit);
4+
--tbody-background-color-1: var(--jp-layout-color1, inherit);
5+
--tbody-background-color-2: var(--jp-layout-color2, inherit);
6+
--tbody-background-color-hover: var(--jp-rendermime-table-row-hover-background, var(--jp-layout-color3, inherit));
7+
--tbody-background-color-disabled: var(--jp-layout-color4, #ccccd1);
88
--tbody-text-color: var(--jp-ui-font-color0, inherit);
9+
--tbody-text-color-disabled: var(--jp-ui-inverse-font-color0, #b3b3b9);
910
--table-font-family: var(--jp-content-font-family, var(--fallback-font-family, inherit));
1011

12+
--table-control-background: rgba(0, 0, 0, 0.2);
13+
--table-control-color: var(--jp-ui-font-color0);
14+
--table-control-border: 1px solid rgba(0, 0, 0, 0.8);
15+
--table-control-border-radius: 0.5em;
16+
1117
--root-highlight: #a0c4ff;
1218
--nested-highlight: #ffadad;
1319
--hover-highlight: #ffd6a5;
1420

1521
--inverted-background-color: #0B525B;
1622
--inverted-text-color: rgb(243, 243, 243);
17-
--paragraph-border-color: transparent;
23+
--paragraph-border-color: var(--jp-layout-color2, inherit);
1824

1925
--fallback-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
2026
}
2127

22-
/* These 2 CSS block sets variable overrides for JupyterLab's default themes */
23-
body[data-jp-theme-name="JupyterLab Dark"] .span-array {
24-
--thead-background-color: #144552;
25-
--thead-text-color: whitesmoke;
26-
--tbody-background-color-1: #0B525B;
27-
--tbody-background-color-2: #065A60;
28-
--tbody-background-color-hover: #197072;
29-
--tbody-background-color-disabled: #486669;
30-
--tbody-text-color: rgb(243, 243, 243);
31-
--paragraph-border-color: #1b1b1b;
32-
}
33-
34-
body[data-jp-theme-name="JupyterLab Light"] .span-array {
35-
--thead-background-color: #0d2025;
36-
--thead-text-color: whitesmoke;
37-
--tbody-background-color-1: #f0f0f0;
38-
--tbody-background-color-2: #ececec;
39-
--tbody-background-color-hover: #6fd9db;
40-
--tbody-background-color-disabled: #9c9c9c;
41-
--tbody-text-color: rgb(0, 0, 0);
42-
--paragraph-border-color: #f1f1f1;
43-
}
44-
4528
/* Table of span offsets */
4629
.span-array>.document>table {
4730
table-layout: auto;
48-
border-radius: 1em 1em 0 0;
4931
overflow: hidden;
5032
width: 100%;
5133
border-collapse: collapse;
@@ -64,7 +46,6 @@ body[data-jp-theme-name="JupyterLab Light"] .span-array {
6446

6547
.span-array>.document>table>tbody>tr:nth-child(2n+1) {
6648
background-color: var(--tbody-background-color-1);
67-
color: var(--tbody-text-color);
6849
}
6950

7051
.span-array>.document>table>tbody>tr:nth-child(2n) {
@@ -73,17 +54,74 @@ body[data-jp-theme-name="JupyterLab Light"] .span-array {
7354

7455
.span-array>.document>table td {
7556
padding: 0.5em;
76-
color: var(--tbody-text-color)
57+
color: var(--tbody-text-color);
7758
}
7859

7960
.span-array>.document>table tr>td:last-child, .span-array>.document>table tr>th:last-child {
8061
text-align: right;
62+
width: 100%;
8163
}
8264

8365
.span-array>.document>table tr>td:not(tr>td:last-child), .span-array>.document>table tr>th:not(tr>th:last-child) {
8466
text-align: left;
8567
}
8668

69+
.span-array>.document>table tr.hover:nth-child(n), .span-array>.document>table tr.highlighted:nth-child(n) {
70+
background-color: var(--tbody-background-color-hover);
71+
}
72+
73+
.span-array>.document>table tr.disabled:nth-child(n), .span-array>.document>table tr.disabled.hover:nth-child(n) {
74+
background-color: var(--tbody-background-color-disabled);
75+
}
76+
77+
.span-array>.document>table tr.disabled:nth-child(n)>td, .span-array>.document>table tr.disabled.hover:nth-child(n)>td {
78+
color: var(--tbody-text-color-disabled);
79+
}
80+
81+
/* Table control buttons */
82+
83+
.span-array>.document>table td:first-child {
84+
vertical-align: center;
85+
}
86+
87+
.span-array>.document>table td:first-child>div.sa-table-controls {
88+
display: flex;
89+
flex-direction: row;
90+
}
91+
92+
.span-array>.document>table td:first-child button {
93+
background-color: var(--table-control-background);
94+
color: var(--table-control-color);
95+
border: var(--table-control-border);
96+
border-right: none;
97+
border-radius: 0;
98+
cursor: pointer;
99+
}
100+
101+
.span-array>.document>table td:first-child button:first-child {
102+
border-radius: var(--table-control-border-radius) 0 0 var(--table-control-border-radius);
103+
}
104+
105+
.span-array>.document>table td:first-child button:last-child {
106+
border-radius: 0 var(--table-control-border-radius) var(--table-control-border-radius) 0;
107+
border-right: var(--table-control-border);
108+
}
109+
110+
.span-array>.document>table td:first-child button[data-control="visibility"]:hover {
111+
background-color: var(--root-highlight);
112+
color: black;
113+
}
114+
115+
.span-array>.document>table tr:not(tr.disabled) td:first-child button[data-control="highlight"]:hover {
116+
background-color: var(--hover-highlight);
117+
color: black;
118+
}
119+
120+
.span-array>.document>table tr.highlighted:not(tr.disabled) td:first-child button[data-control="highlight"] {
121+
background-color: var(--hover-highlight);
122+
color: black;
123+
}
124+
87125
/* Styling for spans within document context */
88126
.span-array>.document>p {
89127
border:1px solid var(--paragraph-border-color);
@@ -103,6 +141,7 @@ body[data-jp-theme-light="false"].span-array>.document>p {
103141
.span-array>.document>p mark {
104142
padding: 0.4em 0.4em;
105143
border-radius: 0.35em;
144+
cursor: pointer;
106145
}
107146

108147
.span-array>.document>p mark {
@@ -125,4 +164,9 @@ body[data-jp-theme-light="false"].span-array>.document>p {
125164
font-variant-caps: all-small-caps;
126165
margin-left: 8px;
127166
text-transform: uppercase;
167+
}
168+
169+
.span-array>.document>p mark.hover, .span-array.span-array>.document>p mark>mark.hover, .span-array>.document>p mark.complex-set.hover, .span-array>.document>p mark.highlighted, .span-array>.document>p mark.complex-set.highlighted, .span-array.span-array>.document>p mark>mark.highlighted {
170+
background: none;
171+
background-color: var(--hover-highlight);
128172
}

0 commit comments

Comments
 (0)