Skip to content

Commit deda04c

Browse files
committed
improve regex logic to allow compound targets / remove row indexing
1 parent fde4e8e commit deda04c

File tree

3 files changed

+248
-89
lines changed

3 files changed

+248
-89
lines changed

examples/ConditionalFormatting.ipynb

Lines changed: 224 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,176 @@
77
"# Conditional formatting based on other column values"
88
]
99
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Compound cell values"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 26,
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"data": {
24+
"application/vnd.jupyter.widget-view+json": {
25+
"model_id": "84885f1b70774e11a322cfd4bbc9b13f",
26+
"version_major": 2,
27+
"version_minor": 0
28+
},
29+
"text/plain": [
30+
"DataGrid(base_column_size=150, default_renderer=TextRenderer(background_color=VegaExpr(value=\"cell.value[1] ==…"
31+
]
32+
},
33+
"metadata": {},
34+
"output_type": "display_data"
35+
}
36+
],
37+
"source": [
38+
"import pandas as pd\n",
39+
"from ipydatagrid import DataGrid, TextRenderer, VegaExpr\n",
40+
"\n",
41+
"df = pd.DataFrame({'column 1': [{'key':11}, ['berry', 'apple', 'cherry']],\n",
42+
" 'column 2': [['berry', 'berry', 'cherry'], {'key':10}]})\n",
43+
"\n",
44+
"renderer = TextRenderer(\n",
45+
" background_color=VegaExpr(\"cell.value[1] == 'berry' && cell.metadata.data['column 1']['key'] == 11 ? 'limegreen' : 'pink'\"))\n",
46+
"\n",
47+
"DataGrid(df, layout={'height':'100px'}, base_column_size=150, default_renderer=renderer)"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 27,
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"data": {
57+
"application/vnd.jupyter.widget-view+json": {
58+
"model_id": "fdccc87c437542ff9e37af623ea79794",
59+
"version_major": 2,
60+
"version_minor": 0
61+
},
62+
"text/plain": [
63+
"DataGrid(base_column_size=150, default_renderer=TextRenderer(background_color=VegaExpr(value=\"cell.value[1] ==…"
64+
]
65+
},
66+
"metadata": {},
67+
"output_type": "display_data"
68+
}
69+
],
70+
"source": [
71+
"import pandas as pd\n",
72+
"from ipydatagrid import DataGrid, TextRenderer, VegaExpr\n",
73+
"\n",
74+
"df = pd.DataFrame({'column 1': [{'key':{'nestedKey':11}}, ['berry', 'apple', 'cherry']],\n",
75+
" 'column 2': [['berry', 'berry', 'cherry'], {'key':10}]})\n",
76+
"\n",
77+
"renderer = TextRenderer(\n",
78+
" background_color=VegaExpr(\"cell.value[1] == 'berry' && \\\n",
79+
" cell.metadata.data['column 1']['key']['nestedKey'] == 11 ? 'magenta' : 'dodgerblue'\"))\n",
80+
"\n",
81+
"DataGrid(df, layout={'height':'100px'}, base_column_size=150, default_renderer=renderer)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 28,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"data": {
91+
"application/vnd.jupyter.widget-view+json": {
92+
"model_id": "",
93+
"version_major": 2,
94+
"version_minor": 0
95+
},
96+
"text/plain": [
97+
"DataGrid(base_column_size=150, default_renderer=TextRenderer(background_color=VegaExpr(value=\"cell.value[1] ==…"
98+
]
99+
},
100+
"metadata": {},
101+
"output_type": "display_data"
102+
}
103+
],
104+
"source": [
105+
"import pandas as pd\n",
106+
"from ipydatagrid import DataGrid, TextRenderer, VegaExpr\n",
107+
"\n",
108+
"df = pd.DataFrame({'column 1': [['one',['two']], ['berry', 'apple', 'cherry']],\n",
109+
" 'column 2': [['berry', 'berry', 'cherry'], ['one',['two']]]})\n",
110+
"\n",
111+
"renderer = TextRenderer(\n",
112+
" background_color=VegaExpr(\"cell.value[1] == 'berry' && \\\n",
113+
" cell.metadata.data['column 1'][1][0] == 'two' ? 'pink' : 'teal'\"))\n",
114+
"\n",
115+
"DataGrid(df, layout={'height':'100px'}, base_column_size=150, default_renderer=renderer)"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 37,
121+
"metadata": {},
122+
"outputs": [
123+
{
124+
"data": {
125+
"application/vnd.jupyter.widget-view+json": {
126+
"model_id": "89275c595316461eadbbb89797fa2cce",
127+
"version_major": 2,
128+
"version_minor": 0
129+
},
130+
"text/plain": [
131+
"DataGrid(base_column_size=150, default_renderer=TextRenderer(background_color=VegaExpr(value=\"cell.value[1] ==…"
132+
]
133+
},
134+
"metadata": {},
135+
"output_type": "display_data"
136+
}
137+
],
138+
"source": [
139+
"import pandas as pd\n",
140+
"from ipydatagrid import DataGrid, TextRenderer, VegaExpr\n",
141+
"\n",
142+
"df = pd.DataFrame({'column 1': [['one',['two']], ['berry', 'apple', 'cherry']],\n",
143+
" 'column 2': [['berry', 'berry', 'cherry'], ['one',['two']]]})\n",
144+
"\n",
145+
"renderer = TextRenderer(\n",
146+
" background_color=VegaExpr(\"cell.value[1] == 'berry' ? 'pink' : 'teal'\"))\n",
147+
"\n",
148+
"DataGrid(df, layout={'height':'100px'}, base_column_size=150, default_renderer=renderer)"
149+
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"metadata": {},
154+
"source": [
155+
"## Normal column names"
156+
]
157+
},
10158
{
11159
"cell_type": "code",
12-
"execution_count": null,
160+
"execution_count": 29,
13161
"metadata": {
14162
"scrolled": false
15163
},
16-
"outputs": [],
164+
"outputs": [
165+
{
166+
"data": {
167+
"application/vnd.jupyter.widget-view+json": {
168+
"model_id": "93307ccc70b0495cb6311f9c588a52b3",
169+
"version_major": 2,
170+
"version_minor": 0
171+
},
172+
"text/plain": [
173+
"DataGrid(base_column_size=300, base_row_size=30, default_renderer=TextRenderer(), header_renderer=None, layout…"
174+
]
175+
},
176+
"metadata": {},
177+
"output_type": "display_data"
178+
}
179+
],
17180
"source": [
18181
"# Imports\n",
19182
"import json\n",
@@ -33,29 +196,16 @@
33196
" 'Column 2': np.random.randn(n),\n",
34197
"})\n",
35198
"\n",
36-
"\n",
37-
"# Formatting the values in column 0 based on the value the cell in row 1, column 1 has\n",
38-
"def format_based_on_other_cell_and_column_value(cell):\n",
39-
" return 'magenta' if cell.column == 0 and cell.metadata.data[1][1] > 1.45 else 'red'\n",
40-
"\n",
41199
"# Formatting the values in column 1 based on the value of the silbing row in column 2\n",
42200
"def format_based_on_other_column(cell):\n",
43-
" return 'green' if cell.column == 1 and cell.metadata.data[2] > 0.0 else 'yellow'\n",
44-
"\n",
45-
"column0_formatting = TextRenderer(\n",
46-
" text_color='black',\n",
47-
" background_color=Expr(format_based_on_other_cell_and_column_value),\n",
48-
")\n",
49-
"\n",
201+
" return 'green' if cell.metadata.data['Column 1'] > 0.0 else 'yellow'\n",
50202
"\n",
51203
"column1_formatting = TextRenderer(\n",
52204
" text_color='black',\n",
53205
" background_color=Expr(format_based_on_other_column),\n",
54206
")\n",
55207
"\n",
56-
"\n",
57208
"renderers = {\n",
58-
" 'Column 0': column0_formatting,\n",
59209
" 'Column 1': column1_formatting,\n",
60210
"}\n",
61211
"\n",
@@ -73,9 +223,24 @@
73223
},
74224
{
75225
"cell_type": "code",
76-
"execution_count": null,
226+
"execution_count": 30,
77227
"metadata": {},
78-
"outputs": [],
228+
"outputs": [
229+
{
230+
"data": {
231+
"application/vnd.jupyter.widget-view+json": {
232+
"model_id": "6665f2c0fe7d40b4a2a7d136b5b858e1",
233+
"version_major": 2,
234+
"version_minor": 0
235+
},
236+
"text/plain": [
237+
"DataGrid(base_column_size=90, default_renderer=TextRenderer(background_color=Expr(value='(((cell.value > -0) &…"
238+
]
239+
},
240+
"metadata": {},
241+
"output_type": "display_data"
242+
}
243+
],
79244
"source": [
80245
"import ipydatagrid as ipg\n",
81246
"import pandas as pd\n",
@@ -90,7 +255,7 @@
90255
"\n",
91256
"# Formatting Factor B rows based on their siblings in the Factor C column\n",
92257
"def format_based_on_other_column(cell):\n",
93-
" return 'green' if cell.column == 1 and cell.metadata.data[2] > 0.0 else 'yellow'\n",
258+
" return 'green' if cell.value > -0 and cell.metadata.data[\"('Value Factors', 'Factor B')\"] > 0.0 else 'yellow'\n",
94259
"\n",
95260
"nested_grid = ipg.DataGrid(nested_df,\n",
96261
" base_column_size=90,\n",
@@ -104,60 +269,43 @@
104269
},
105270
{
106271
"cell_type": "code",
107-
"execution_count": null,
272+
"execution_count": 31,
108273
"metadata": {},
109-
"outputs": [],
274+
"outputs": [
275+
{
276+
"data": {
277+
"application/vnd.jupyter.widget-view+json": {
278+
"model_id": "a8d18e6910a74f3ea9cfa3dfb7ec21d5",
279+
"version_major": 2,
280+
"version_minor": 0
281+
},
282+
"text/plain": [
283+
"DataGrid(base_column_size=300, base_row_size=30, default_renderer=TextRenderer(), header_renderer=None, layout…"
284+
]
285+
},
286+
"metadata": {},
287+
"output_type": "display_data"
288+
}
289+
],
110290
"source": [
111291
"def format_based_on_other_column(cell):\n",
112-
" return 'green' if cell.column == 0 and cell.metadata.data[1] == \"Buy\" else 'red'\n",
292+
" return 'green' if cell.column == 0 and cell.metadata.data['Signal'] == \"Buy\" else 'red'\n",
113293
"\n",
114-
"column0_formatting = TextRenderer(\n",
115-
" text_color='black',\n",
294+
"\n",
295+
"signal_column_formatting = TextRenderer(\n",
296+
" text_color='white',\n",
116297
" background_color=Expr(format_based_on_other_column),\n",
117298
")\n",
118299
"\n",
119300
"renderers = {\n",
120-
" 'Stock': column0_formatting,\n",
301+
" 'Stock': signal_column_formatting,\n",
121302
"}\n",
122303
"\n",
123304
"grid = DataGrid(pd.DataFrame({\"Stock\":'A B C D'.split(), \"Signal\":['Buy', 'Sell', 'Buy', 'Sell']}), \n",
124305
" base_row_size=30, base_column_size=300, renderers=renderers, layout={'height':'150px'})\n",
125306
"grid"
126307
]
127308
},
128-
{
129-
"cell_type": "code",
130-
"execution_count": null,
131-
"metadata": {},
132-
"outputs": [],
133-
"source": [
134-
"def format_based_on_other_column(cell):\n",
135-
" return 'green' if cell.column == 0 and cell.metadata.data[2][1] == \"Really Important Cell\" else 'red'\n",
136-
"\n",
137-
"def format_really_important_cell(cell):\n",
138-
" return 'limegreen' if cell.row == 2 else 'white'\n",
139-
"\n",
140-
"column0_formatting = TextRenderer(\n",
141-
" text_color='black',\n",
142-
" background_color=Expr(format_based_on_other_column)\n",
143-
")\n",
144-
"\n",
145-
"column1_formatting = TextRenderer(\n",
146-
" text_color='black',\n",
147-
" background_color=Expr(format_really_important_cell)\n",
148-
")\n",
149-
"\n",
150-
"renderers = {\n",
151-
" 'Stock': column0_formatting,\n",
152-
" 'Sentiment': column1_formatting\n",
153-
"}\n",
154-
"\n",
155-
"grid = DataGrid(pd.DataFrame({\"Stock\":'A B C D'.split(), \n",
156-
" \"Sentiment\":['Not Important','Not Important', 'Really Important Cell', 'Not Important']}), \n",
157-
" base_row_size=30, base_column_size=300, renderers=renderers, layout={'height':'150px'})\n",
158-
"grid"
159-
]
160-
},
161309
{
162310
"cell_type": "markdown",
163311
"metadata": {},
@@ -167,9 +315,24 @@
167315
},
168316
{
169317
"cell_type": "code",
170-
"execution_count": null,
318+
"execution_count": 32,
171319
"metadata": {},
172-
"outputs": [],
320+
"outputs": [
321+
{
322+
"data": {
323+
"application/vnd.jupyter.widget-view+json": {
324+
"model_id": "6b70839957b74d79a79818c94ca8654c",
325+
"version_major": 2,
326+
"version_minor": 0
327+
},
328+
"text/plain": [
329+
"DataGrid(base_column_size=300, base_row_size=30, default_renderer=TextRenderer(), header_renderer=None, layout…"
330+
]
331+
},
332+
"metadata": {},
333+
"output_type": "display_data"
334+
}
335+
],
173336
"source": [
174337
"import json\n",
175338
"\n",
@@ -185,7 +348,7 @@
185348
"np.random.seed(0)\n",
186349
"\n",
187350
"def format_based_on_date(cell):\n",
188-
" return 'magenta' if cell.column == 0 and cell.metadata.data[2] >= '2020-10-21' else 'yellow'\n",
351+
" return 'magenta' if cell.column == 0 and cell.metadata.data['Dates'] >= '2020-10-21' else 'yellow'\n",
189352
"\n",
190353
"df = pd.DataFrame({\n",
191354
" 'Value 1': np.random.randn(n),\n",

src/core/viewbasedjsonmodel.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,9 @@ export class ViewBasedJSONModel extends MutableDataModel {
161161
if (region == 'body') {
162162
md.row = row;
163163
md.column = column;
164-
md.formattingInfo = 4;
165-
md.model = this;
166164
md.data = (row: number, column: number) => {
167-
return this.data('body', row, column);
165+
const columnIndex = this.columnNameToIndex(column.toString());
166+
return this.data('body', row, columnIndex);
168167
};
169168
}
170169
return md;

0 commit comments

Comments
 (0)