-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathmain.py
54 lines (41 loc) · 1.37 KB
/
main.py
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
import json
from pathlib import Path
from idom import component, hooks, html, run
HERE = Path(__file__)
DATA_PATH = HERE.parent / "data.json"
sculpture_data = json.loads(DATA_PATH.read_text())
@component
def Gallery():
index, set_index = hooks.use_state(0)
show_more, set_show_more = hooks.use_state(False)
def handle_next_click(event):
set_index(index + 1)
def handle_more_click(event):
set_show_more(not show_more)
bounded_index = index % len(sculpture_data)
sculpture = sculpture_data[bounded_index]
alt = sculpture["alt"]
artist = sculpture["artist"]
description = sculpture["description"]
name = sculpture["name"]
url = sculpture["url"]
return html.div(
html.button("Next", on_click=handle_next_click),
html.h2(name, " by ", artist),
html.p(f"({bounded_index + 1} or {len(sculpture_data)})"),
html.img(src=url, alt=alt, style={"height": "200px"}),
html.div(
html.button(
f"{('Show' if show_more else 'Hide')} details",
on_click=handle_more_click,
),
(html.p(description) if show_more else ""),
),
)
@component
def App():
return html.div(
html.section(Gallery(), style={"width": "50%", "float": "left"}),
html.section(Gallery(), style={"width": "50%", "float": "left"}),
)
run(App)