Skip to content

Commit cbcecff

Browse files
committed
finish up responding to events section
1 parent 2d1a9d1 commit cbcecff

18 files changed

+387
-94
lines changed

docs/examples.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ def capture_component(component_constructor):
4141

4242
idom.run = capture_component
4343
try:
44-
code = compile(file.read_text(), str(file.absolute()), "exec")
45-
exec(code, {"print": capture_print})
44+
code = compile(file.read_text(), str(file), "exec")
45+
exec(
46+
code,
47+
{
48+
"print": capture_print,
49+
"__file__": str(file),
50+
"__name__": file.stem,
51+
},
52+
)
4653
except Exception:
47-
return _make_error_display()
54+
return _make_error_display(format_exc())
4855
finally:
4956
idom.run = RUN_IDOM
5057

@@ -110,9 +117,9 @@ def ExampleDidNotRun():
110117
return ExampleDidNotRun
111118

112119

113-
def _make_error_display():
120+
def _make_error_display(message):
114121
@idom.component
115122
def ShowError():
116-
return idom.html.pre(format_exc())
123+
return idom.html.pre(message)
117124

118125
return ShowError
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
3+
from idom import component, html, run
4+
5+
6+
@component
7+
def ButtonWithDelay(message, delay):
8+
async def handle_event(event):
9+
await asyncio.sleep(delay)
10+
print(message)
11+
12+
return html.button({"onClick": handle_event}, message)
13+
14+
15+
@component
16+
def App():
17+
return html.div(
18+
ButtonWithDelay("print 3 seconds later", delay=3),
19+
ButtonWithDelay("print immediately", delay=0),
20+
)
21+
22+
23+
run(App)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from idom import component, html, run
2+
3+
4+
@component
5+
def Button(display_text, on_click):
6+
return html.button({"onClick": on_click}, display_text)
7+
8+
9+
@component
10+
def PlayButton(movie_name):
11+
def handle_click(event):
12+
print(f"Playing {movie_name}")
13+
14+
return Button(f"Play {movie_name}", on_click=handle_click)
15+
16+
17+
@component
18+
def FastForwardButton():
19+
def handle_click(event):
20+
print("Skipping ahead")
21+
22+
return Button("Fast forward", on_click=handle_click)
23+
24+
25+
@component
26+
def App():
27+
return html.div(
28+
PlayButton("Buena Vista Social Club"),
29+
FastForwardButton(),
30+
)
31+
32+
33+
run(App)

docs/source/_examples/adding_interactivity/button_prints_event.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def Button():
66
def handle_event(event):
77
print(event)
88

9-
return html.button({"onClick": handle_event}, "Print event to terminal")
9+
return html.button({"onClick": handle_event}, "Click me!")
1010

1111

1212
run(Button)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from idom import component, html, run
2+
3+
4+
@component
5+
def PrintButton(display_text, message_text):
6+
def handle_event(event):
7+
print(message_text)
8+
9+
return html.button({"onClick": handle_event}, display_text)
10+
11+
12+
@component
13+
def App():
14+
return html.div(
15+
PrintButton("Play", "Playing"),
16+
PrintButton("Pause", "Paused"),
17+
)
18+
19+
20+
run(App)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from idom import component, event, html, run
2+
3+
4+
@component
5+
def DoNotChangePages():
6+
return html.div(
7+
html.p("Normally clicking this link would take you to a new page"),
8+
html.a(
9+
{
10+
"onClick": event(lambda event: None, prevent_default=True),
11+
"href": "https://google.com",
12+
},
13+
"https://google.com",
14+
),
15+
)
16+
17+
18+
run(DoNotChangePages)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from idom import component, event, hooks, html, run
2+
3+
4+
@component
5+
def DivInDiv():
6+
stop_propagatation, set_stop_propagatation = hooks.use_state(True)
7+
inner_count, set_inner_count = hooks.use_state(0)
8+
outer_count, set_outer_count = hooks.use_state(0)
9+
10+
div_in_div = html.div(
11+
{
12+
"onClick": lambda event: set_outer_count(outer_count + 1),
13+
"style": {"height": "100px", "width": "100px", "backgroundColor": "red"},
14+
},
15+
html.div(
16+
{
17+
"onClick": event(
18+
lambda event: set_inner_count(inner_count + 1),
19+
stop_propagation=stop_propagatation,
20+
),
21+
"style": {"height": "50px", "width": "50px", "backgroundColor": "blue"},
22+
},
23+
),
24+
)
25+
26+
return html.div(
27+
html.button(
28+
{"onClick": lambda event: set_stop_propagatation(not stop_propagatation)},
29+
"Toggle Propogation",
30+
),
31+
html.pre(f"Will propagate: {not stop_propagatation}"),
32+
html.pre(f"Inner click count: {inner_count}"),
33+
html.pre(f"Outer click count: {outer_count}"),
34+
div_in_div,
35+
)
36+
37+
38+
run(DivInDiv)

docs/source/_examples/prevent_default_event_actions.py

-18
This file was deleted.

docs/source/_examples/stop_event_propagation.py

-38
This file was deleted.
+27-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
from pathlib import Path
22

3-
import idom
3+
from idom import component, run, web
44

55

66
file = Path(__file__).parent / "super_simple_chart.js"
7-
ssc = idom.web.module_from_file("super-simple-chart", file, fallback="⌛")
8-
SuperSimpleChart = idom.web.export(ssc, "SuperSimpleChart")
7+
ssc = web.module_from_file("super-simple-chart", file, fallback="⌛")
8+
SuperSimpleChart = web.export(ssc, "SuperSimpleChart")
99

10-
idom.run(
11-
idom.component(
12-
lambda: SuperSimpleChart(
13-
{
14-
"data": [
15-
{"x": 1, "y": 2},
16-
{"x": 2, "y": 4},
17-
{"x": 3, "y": 7},
18-
{"x": 4, "y": 3},
19-
{"x": 5, "y": 5},
20-
{"x": 6, "y": 9},
21-
{"x": 7, "y": 6},
22-
],
23-
"height": 300,
24-
"width": 500,
25-
"color": "royalblue",
26-
"lineWidth": 4,
27-
"axisColor": "silver",
28-
}
29-
)
10+
11+
@component
12+
def App():
13+
print(__name__)
14+
return SuperSimpleChart(
15+
{
16+
"data": [
17+
{"x": 1, "y": 2},
18+
{"x": 2, "y": 4},
19+
{"x": 3, "y": 7},
20+
{"x": 4, "y": 3},
21+
{"x": 5, "y": 5},
22+
{"x": 6, "y": 9},
23+
{"x": 7, "y": 6},
24+
],
25+
"height": 300,
26+
"width": 500,
27+
"color": "royalblue",
28+
"lineWidth": 4,
29+
"axisColor": "silver",
30+
}
3031
)
31-
)
32+
33+
34+
run(App)

0 commit comments

Comments
 (0)