Skip to content

Commit 7f4d086

Browse files
author
Zerline
committed
A new method 'update_style' to let the user change the look and feel without modifying the structure.
1 parent b0b5a35 commit 7f4d086

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

examples/TossingDominos.ipynb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
"source": [
155155
"%%html\n",
156156
"<style>\n",
157+
".b0 {}\n",
157158
".b1 {background-color: green}\n",
158159
".b2 {background-color: blue}\n",
159160
".b3 {background-color: red}\n",
@@ -186,7 +187,9 @@
186187
"ORDER = 12\n",
187188
"g, m, md = figure(ORDER)\n",
188189
"w = GridViewWidget(g, cell_layout=smallblyt, \n",
189-
" cell_widget_classes=[Button0, Button1, Button2, Button3, Button4], \n",
190+
" cell_widget_classes=[Button0, Button1, Button2, Button3, Button4],\n",
191+
" css_classes=['b0', 'b1', 'b2', 'b3', 'b4'],\n",
192+
" css_class_index=make_cell_widget_class_index(md, ORDER),\n",
190193
" cell_widget_class_index=make_cell_widget_class_index(md, ORDER), \n",
191194
" blank_widget_class=BlankButton)\n",
192195
"w"
@@ -202,7 +205,7 @@
202205
"source": [
203206
"for i in range(2,ORDER+1):\n",
204207
" md = tossing(g, ORDER, i, md)\n",
205-
" w.draw(cell_widget_class_index=make_cell_widget_class_index(md, i))"
208+
" w.update_style(css_class_index=make_cell_widget_class_index(md, i))"
206209
]
207210
}
208211
],

sage_combinat_widgets/grid_view_editor.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424
import re, traitlets
2525
from six import add_metaclass
26+
from abc import abstractmethod
2627
from copy import copy
2728
from sage.misc.bindable_class import BindableClass
2829
from sage.all import SageObject
@@ -316,12 +317,20 @@ def reset_links(self):
316317
lnk.unlink()
317318
self.links = []
318319

320+
@abstractmethod
321+
def update_style(self):
322+
"""
323+
Update look and feel.
324+
"""
325+
return
326+
327+
@abstractmethod
319328
def draw(self, cast=None):
320329
r"""
321330
Build the visual representation
322331
and cdlink objects -- with cast function `cast`.
323332
"""
324-
pass
333+
return
325334

326335
def get_value(self):
327336
r"""

sage_combinat_widgets/grid_view_widget.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ class BlankCell(TextSingleton):
116116
"""
117117
displaytype = text_type
118118

119-
def __init__(self, layout=textcell_layout, **kws):
119+
def __init__(self, position=None, layout=textcell_layout, **kws):
120120
super(BlankCell, self).__init__()
121121
self.value = ''
122+
self.position = position
122123
self.layout = layout
123124
self.disabled = True
124125
self.add_class('blankcell')
@@ -265,9 +266,9 @@ class StyledPushButton(ButtonSingleton):
265266
disable = None
266267
css_class = None
267268
def __init__(self, content=None, position=None, layout=buttoncell_smaller_layout, description='', placeholder=None):
268-
super(StyledPushButton, self).__init__()
269-
self.layout=layout
270-
self.description=description
269+
super(StyledPushButton, self).__init__(layout=layout, description=description, placeholder=placeholder)
270+
self.content = content
271+
self.position = position
271272
if self.disable:
272273
self.disabled = True
273274
self.add_class('gridbutton')
@@ -318,6 +319,7 @@ class GridViewWidget(GridViewEditor, VBox, ValueWidget):
318319

319320
def __init__(self, obj, adapter=None, display_convention='en', cell_layout=None,
320321
cell_widget_classes=[TextCell], cell_widget_class_index=lambda x:0,
322+
css_classes = [], css_class_index=None,
321323
blank_widget_class=BlankCell, addable_widget_class=AddableTextCell):
322324
r"""
323325
Grid View Widget initialization.
@@ -365,6 +367,8 @@ def __init__(self, obj, adapter=None, display_convention='en', cell_layout=None,
365367
self.cell_layout = cell_layout
366368
self.cell_widget_classes = cell_widget_classes
367369
self.cell_widget_class_index = cell_widget_class_index
370+
self.css_classes = css_classes
371+
self.css_class_index = css_class_index or cell_widget_class_index or (lambda x:0)
368372
try:
369373
self.displaytype = cell_widget_classes[0].displaytype
370374
except:
@@ -449,6 +453,35 @@ def add_links(self):
449453
if child and hasattr(child, 'value') and traitname in self.traits():
450454
self.links.append(cdlink((child, 'value'), (self, traitname), self.cast))
451455

456+
def update_style(self, css_classes=None, css_class_index=None):
457+
r"""
458+
Update look and fell -- ie CSS classes.
459+
Therefore avoid redrawing if overall shape is unchanged.
460+
461+
TESTS ::
462+
463+
sage: from sage_combinat_widgets.grid_view_widget import *
464+
sage: from sage.graphs.generators.families import AztecDiamondGraph
465+
sage: az = AztecDiamondGraph(4)
466+
sage: w = GridViewWidget(az, cell_widget_classes=[ButtonCell], blank_widget_class=BlankButton)
467+
sage: w.children[1].children[3]._dom_classes
468+
('gridbutton',)
469+
sage: w.update_style(css_classes=['cl0', 'cl1', 'cl2', 'cl3'], css_class_index=lambda x:x[0]%4)
470+
sage: w.children[1].children[3]._dom_classes
471+
('gridbutton', 'cl1')
472+
"""
473+
if not css_classes:
474+
css_classes = self.css_classes
475+
if not css_class_index:
476+
css_class_index = self.cell_widget_class_index
477+
for row in self.children:
478+
for cell in row.children:
479+
if not hasattr(cell, 'position'):
480+
continue # Do we want to change blank cells' style?
481+
for cl in css_classes:
482+
cell.remove_class(cl)
483+
cell.add_class(css_classes[css_class_index(cell.position)])
484+
452485
def draw(self, cell_widget_classes=None, cell_widget_class_index=None,
453486
addable_widget_class=None, blank_widget_class=None):
454487
r"""

tests/test_update_style.ipynb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from sage_combinat_widgets.grid_view_widget import *\n",
10+
"from sage.graphs.generators.families import AztecDiamondGraph\n",
11+
"az = AztecDiamondGraph(4) "
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"w = GridViewWidget(az, cell_widget_classes=[ButtonCell], blank_widget_class=BlankButton)\n",
21+
"w"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"%%html\n",
31+
"<style>\n",
32+
".b0 {}\n",
33+
".b1 {background-color: green}\n",
34+
".b2 {background-color: blue}\n",
35+
".b3 {background-color: red}\n",
36+
".b4 {background-color: yellow}\n",
37+
"</style>"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"w.update_style(css_classes=['b0', 'b1', 'b2', 'b3', 'b4'], css_class_index=lambda x:(x[0]+x[1])%5)"
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "Python 3",
53+
"language": "python",
54+
"name": "python3"
55+
},
56+
"language_info": {
57+
"codemirror_mode": {
58+
"name": "ipython",
59+
"version": 3
60+
},
61+
"file_extension": ".py",
62+
"mimetype": "text/x-python",
63+
"name": "python",
64+
"nbconvert_exporter": "python",
65+
"pygments_lexer": "ipython3",
66+
"version": "3.7.3"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 2
71+
}

0 commit comments

Comments
 (0)