File size: 1,248 Bytes
d037030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import solara
import anywidget
import traitlets
import ipywidgets as widgets

class CounterWidget(anywidget.AnyWidget):
    _esm = """
        import confetti from "https://esm.sh/[email protected]"
        function render({ model, el }) {
          let count = () => model.get("value");
          let btn = document.createElement("button");
          btn.innerHTML = `count is ${count()}`;
          btn.classList.add("counter-btn");
          btn.addEventListener("click", () => {
            model.set("value", count() + 1);
            model.save_changes();
          });
          model.on("change:value", () => {
            btn.innerHTML = `count is ${count()}`;
            confetti({ angle: (45*(count()-1)) });
          });
          el.appendChild(btn);
        }
        export default { render };
    """
    _css = """
        .counter-btn {
            background:blue;
            padding:10px 50px;
        }
        .counter-btn:hover {
            background-color:green;
        }
    """
    value = traitlets.Int(0).tag(sync=True)

@solara.component
def Page():
    counter = CounterWidget()
    with solara.Column(style={"padding":"30px"}):
        solara.Markdown("#Anywidgets+Solara")
        counter.element()
Page()