-
-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathimage_button.py
More file actions
54 lines (47 loc) · 1.18 KB
/
image_button.py
File metadata and controls
54 lines (47 loc) · 1.18 KB
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
"""Image button widget with click tracking."""
import param
import panel as pn
from panel.custom import AnyWidgetComponent
class ImageButton(AnyWidgetComponent):
clicks = param.Integer(default=0)
image = param.String()
_esm = """
function render({ model, el }) {
const button = document.createElement('button');
button.id = 'button';
button.className = 'pn-container center-content';
const img = document.createElement('img');
img.id = 'image';
img.className = 'image-size';
img.src = model.get("image");
button.appendChild(img);
button.addEventListener('click', () => {
model.set("clicks", model.get("clicks")+1);
model.save_changes();
});
el.appendChild(button);
}
export default { render }
"""
_stylesheets = ["""
.pn-container {
height: 100%;
width: 100%;
}
.center-content {
display: flex;
align-items: center;
justify-content: center;
padding: 1em;
}
.image-size {
width: 100%;
max-height: 100%;
object-fit: contain;
}
"""]
component = ImageButton(
image="https://panel.holoviz.org/_static/logo_stacked.png",
styles={"border": "2px solid lightgray"},
width=400, height=200,
)