Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
holoviz
GitHub Repository: holoviz/panel
Path: blob/main/examples/how_to/custom/js/slideshow.py
2012 views
1
import param
2
3
import panel as pn
4
5
from panel.custom import JSComponent
6
7
pn.extension()
8
9
class JSSlideshow(JSComponent):
10
11
index = param.Integer(default=0)
12
13
_esm = """
14
export function render({ model }) {
15
const img = document.createElement('img')
16
img.addEventListener('click', () => { model.index += 1 })
17
const update = () => {
18
img.src = `https://picsum.photos/800/300?image=${model.index}`
19
}
20
model.watch(update, 'index')
21
update()
22
return img
23
}
24
"""
25
26
27
class PySlideshow(JSComponent):
28
29
index = param.Integer(default=0)
30
31
_esm = """
32
export function render({ model }) {
33
const img = document.createElement('img')
34
img.addEventListener('click', (event) => model.send_event('click', event))
35
const update = () => {
36
img.src = `https://picsum.photos/800/300?image=${model.index}`
37
}
38
model.watch(update, 'index')
39
update()
40
return img
41
}
42
"""
43
44
def _handle_click(self, event):
45
self.index += 1
46
47
48
pn.Row(
49
JSSlideshow(),
50
PySlideshow()
51
).servable()
52
53