Skip to content

Commit 6c5212f

Browse files
committed
add list examples
found a bug in the layout though...
1 parent e2bba80 commit 6c5212f

File tree

6 files changed

+162
-37
lines changed

6 files changed

+162
-37
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def ArtistList():
6+
artist_to_add, set_artist_to_add = use_state("")
7+
artists, set_artists = use_state([])
8+
9+
def handle_change(event):
10+
set_artist_to_add(event["target"]["value"])
11+
12+
def handle_click(event):
13+
if artist_to_add not in artists:
14+
set_artists([*artists, artist_to_add])
15+
set_artist_to_add("")
16+
17+
return html.div(
18+
html.h1("Inspiring sculptors:"),
19+
html.input({"value": artist_to_add, "onChange": handle_change}),
20+
html.button({"onClick": handle_click}, "add"),
21+
html.ul([html.li(name, key=name) for name in artists]),
22+
)
23+
24+
25+
run(ArtistList)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def ArtistList():
6+
artists, set_artists = use_state(
7+
["Marta Colvin Andrade", "Lamidi Olonade Fakeye", "Louise Nevelson"]
8+
)
9+
10+
def handle_sort_click(event):
11+
set(list(sorted(set_artists)))
12+
13+
def handle_reverse_click(event):
14+
set(list(reversed(set_artists)))
15+
16+
return html.div(
17+
html.h1("Inspiring sculptors:"),
18+
html.button({"onClick": handle_sort_click}, "sort"),
19+
html.button({"onClick": handle_reverse_click}, "reverse"),
20+
html.ul([html.li(name, key=name) for name in artists]),
21+
)
22+
23+
24+
run(ArtistList)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def ArtistList():
6+
artist_to_add, set_artist_to_add = use_state("")
7+
artists, set_artists = use_state(
8+
["Marta Colvin Andrade", "Lamidi Olonade Fakeye", "Louise Nevelson"]
9+
)
10+
11+
def handle_change(event):
12+
set_artist_to_add(event["target"]["value"])
13+
14+
def handle_add_click(event):
15+
if artist_to_add not in artists:
16+
set_artists([*artists, artist_to_add])
17+
set_artist_to_add("")
18+
19+
def make_handle_delete_click(index):
20+
def handle_click(event):
21+
set_artists(artists[:index] + artists[index + 1 :])
22+
23+
return handle_click
24+
25+
return html.div(
26+
html.h1("Inspiring sculptors:"),
27+
html.input({"value": artist_to_add, "onChange": handle_change}),
28+
html.button({"onClick": handle_add_click}, "add"),
29+
html.ul(
30+
[
31+
html.li(
32+
name,
33+
html.button({"onClick": make_handle_delete_click(index)}, "delete"),
34+
key=name,
35+
)
36+
for index, name in enumerate(artists)
37+
]
38+
),
39+
)
40+
41+
42+
run(ArtistList)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def CounterList():
6+
counters, set_counters = use_state([0, 0, 0])
7+
8+
def make_increment_click_handler(index):
9+
def handle_click(event):
10+
new_value = counters[index] + 1
11+
set_counters(counters[:index] + [new_value] + counters[index + 1 :])
12+
13+
return handle_click
14+
15+
return html.ul(
16+
[
17+
html.li(
18+
count,
19+
html.button({"onClick": make_increment_click_handler(index)}, "+1"),
20+
key=index,
21+
)
22+
for index, count in enumerate(counters)
23+
]
24+
)
25+
26+
27+
run(CounterList)

docs/source/adding-interactivity/dangers-of-mutability/index.rst

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,6 @@ Working with Lists
254254

255255
Below are some ways to update lists without mutating them:
256256

257-
.. card:: Replacing Items
258-
:link: replacing-list-items
259-
:link-type: ref
260-
261-
Avoid using item or slice assignment. Instead try the strategies below:
262-
263-
.. code-block::
264-
265-
l[:index] + [value] + l[index + 1:]
266-
267-
l[:start] + values + l[end + 1:]
268-
269257
.. card:: Inserting Items
270258
:link: inserting-list-items
271259
:link-type: ref
@@ -293,6 +281,17 @@ Below are some ways to update lists without mutating them:
293281
294282
l[:index - 1] + l[index:]
295283
284+
.. card:: Replacing Items
285+
:link: replacing-list-items
286+
:link-type: ref
287+
288+
Avoid using item or slice assignment. Instead try the strategies below:
289+
290+
.. code-block::
291+
292+
l[:index] + [value] + l[index + 1:]
293+
294+
l[:start] + values + l[end + 1:]
296295
297296
..card:: Re-ordering Items
298297
:link: re-ordering-list-items
@@ -308,30 +307,6 @@ Below are some ways to update lists without mutating them:
308307
list(reversed(l))
309308
310309
311-
.. _replacing-list-items:
312-
313-
Replacing List Items
314-
....................
315-
316-
.. grid:: 1 1 1 2
317-
318-
.. grid-item-card:: :bdg-danger:`Avoid`
319-
320-
.. code-block::
321-
322-
l[index] = value
323-
324-
l[start:end] = values
325-
326-
.. grid-item-card:: :bdg-info:`Prefer`
327-
328-
.. code-block::
329-
330-
l[:index] + [value] + l[index + 1:]
331-
332-
l[:start] + values + l[end + 1:]
333-
334-
335310
.. _inserting-list-items:
336311

337312
Inserting List Items
@@ -361,6 +336,8 @@ Inserting List Items
361336
362337
l[:index] + [value] + l[index:]
363338
339+
.. idom:: _examples/list_insert
340+
364341

365342
.. _removing-list-items:
366343

@@ -383,6 +360,34 @@ Removing List Items
383360
384361
l[:index - 1] + l[index:]
385362
363+
.. idom:: _examples/list_remove
364+
365+
366+
.. _replacing-list-items:
367+
368+
Replacing List Items
369+
....................
370+
371+
.. grid:: 1 1 1 2
372+
373+
.. grid-item-card:: :bdg-danger:`Avoid`
374+
375+
.. code-block::
376+
377+
l[index] = value
378+
379+
l[start:end] = values
380+
381+
.. grid-item-card:: :bdg-info:`Prefer`
382+
383+
.. code-block::
384+
385+
l[:index] + [value] + l[index + 1:]
386+
387+
l[:start] + values + l[end + 1:]
388+
389+
.. idom:: _examples/list_replace
390+
386391

387392
.. _re-ordering-list-items:
388393

@@ -407,6 +412,8 @@ Re-ordering List Items
407412
408413
list(reversed(l))
409414
415+
.. idom:: _examples/list_re_order
416+
410417

411418
Working with Sets
412419
-----------------

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def docs(session: Session) -> None:
116116
"html",
117117
"docs/source",
118118
"docs/build",
119-
env=get_idom_script_env(),
119+
env={**os.environ, **get_idom_script_env()},
120120
)
121121

122122

0 commit comments

Comments
 (0)