
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import solara
import traitlets
from time import sleep
from anywidget import AnyWidget
class CustomWidget(AnyWidget):
_esm = "view.js"
color = traitlets.Unicode(allow_none=True, default_value=None).tag(sync=True)
@traitlets.observe("color")
def callback(self, change):
print("Change:", change)
self.color = None
@solara.component
def Page():
el = CustomWidget.element()
def change_color(delay=None):
if delay is not None:
sleep(delay)
el_widget = solara.get_widget(el)
el_widget.color = "limegreen"
el_widget.send({"color": "limegreen"})
solara.use_effect(change_color)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Model {
constructor(model) {
this.model = model;
console.log("MADE MODEL", this.model.get("color"));
}
initialize() {
this.model.on("change:color", () => {console.log("CHANGE:COLOR@MODEL", this.model.get("color"))});
this.model.on("msg:custom", (content) => {console.log("MSG:CUSTOM@MODEL", content.color)});
}
defaults() {
return {
color: undefined,
}
}
get(key) {
return this.model.get(key)
}
on(eventName, callback) {
this.model.on(eventName, callback);
}
}
class View {
constructor(model, el) {
this.model = model
this.el = el
console.log("MADE VIEW", this.model.get("color"));
}
render(){
this.button = document.createElement("button");
this.button.innerHTML = `count is whatever`;
this.changeColor();
this.model.on("change:color", () => {
console.log("CHANGE:COLOR@VIEW", this.model.get("color"));
this.changeColor();
});
this.model.on("msg:custom", (content) => {
console.log("MSG:CUSTOM@VIEW", content.color);
this.changeColor(content.color)
})
this.el.classList.add("counter-widget");
this.el.appendChild(this.button);
}
remove() {}
changeColor(newColor) {
const color = newColor || this.model.get("color");
if (color){
this.button.style.backgroundColor = color;
}
}
}
export default () => {
let model;
return {
initialize(ctx) {
model = new Model(ctx.model);
model.initialize();
},
render({ el }) {
const view = new View(model, el);
view.render()
return () => view.remove();
}
}
}