Skip to content

Commit 06403ac

Browse files
committed
finish off your firt component section
1 parent 4c6abe9 commit 06403ac

20 files changed

+173
-556
lines changed
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
2+
3+
4+
@component
5+
def Item(name, done):
6+
if done:
7+
return html.li(name, " ✔")
8+
else:
9+
return html.li(name)
10+
11+
12+
@component
13+
def TodoList():
14+
return html.section(
15+
html.h1("My Todo List"),
16+
html.ul(
17+
Item("Find a cool problem to solve", done=True),
18+
Item("Build an app to solve it", done=True),
19+
Item("Share that app with the world!", done=False),
20+
),
21+
)
22+
23+
24+
run(TodoList)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from idom import component, html, run
2+
3+
4+
@component
5+
def Item(name, done):
6+
return html.li(name, "" if done else " ✔")
7+
8+
9+
@component
10+
def TodoList():
11+
return html.section(
12+
html.h1("My Todo List"),
13+
html.ul(
14+
Item("Find a cool problem to solve", done=True),
15+
Item("Build an app to solve it", done=True),
16+
Item("Share that app with the world!", done=False),
17+
),
18+
)
19+
20+
21+
run(TodoList)

docs/source/_examples/creating_interfaces/nested_photos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
def Photo():
66
return html.img(
77
{
8-
"src": "https://i.pinimg.com/564x/d6/96/c4/d696c4d3db31609c1abb05c52f305ec1.jpg",
8+
"src": "https://picsum.photos/id/274/500/300",
99
"style": {"width": "30%"},
1010
"alt": "Ray Charles",
1111
}
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
2+
3+
4+
@component
5+
def Photo(alt_text, image_id):
6+
return html.img(
7+
{
8+
"src": f"https://picsum.photos/id/{image_id}/500/300",
9+
"style": {"width": "50%"},
10+
"alt": alt_text,
11+
}
12+
)
13+
14+
15+
@component
16+
def Gallery():
17+
return html.section(
18+
html.h1("Photo Gallery"),
19+
Photo("Landscape", image_id=830),
20+
Photo("City", image_id=274),
21+
Photo("Puppy", image_id=237),
22+
)
23+
24+
25+
run(Gallery)

docs/source/_examples/creating_interfaces/simple_photo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
def Photo():
66
return html.img(
77
{
8-
"src": "https://i.pinimg.com/564x/d6/96/c4/d696c4d3db31609c1abb05c52f305ec1.jpg",
9-
"style": {"width": "30%"},
10-
"alt": "Ray Charles",
8+
"src": "https://picsum.photos/id/237/500/300",
9+
"style": {"width": "50%"},
10+
"alt": "Puppy",
1111
}
1212
)
1313

docs/source/_examples/creating_interfaces/static_todo_list.py

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from idom import component, html, run
2+
3+
4+
@component
5+
def Item(name, done):
6+
return html.li(name)
7+
8+
9+
@component
10+
def TodoList():
11+
return html.section(
12+
html.h1("My Todo List"),
13+
html.ul(
14+
Item("Find a cool problem to solve", done=True),
15+
Item("Build an app to solve it", done=True),
16+
Item("Share that app with the world!", done=False),
17+
),
18+
)
19+
20+
21+
run(TodoList)

docs/source/creating-interfaces/conditional-rendering.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/source/creating-interfaces/html-with-idom.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ to specify a URL to its ``src`` and use some ``style`` to modify and position it
7979
.. code-block:: html
8080

8181
<img
82-
src="https://i.pinimg.com/564x/29/66/4d/29664d24bc793085a7671d7fc02a33f4.jpg"
82+
src="https://picsum.photos/id/237/500/300"
8383
style="width: 50%; margin-left: 25%;"
84-
alt="Martha Argerich"
84+
alt="Billie Holiday"
8585
/>
8686

8787
In IDOM we add these attributes to elements using dictionaries. There are some notable
@@ -93,18 +93,18 @@ Additionally, instead of specifying ``style`` using a string, we use a dictionar
9393

9494
html.img(
9595
{
96-
"src": "https://i.pinimg.com/564x/29/66/4d/29664d24bc793085a7671d7fc02a33f4.jpg
96+
"src": "https://picsum.photos/id/237/500/300",
9797
"style": {"width": "50%", "marginLeft": "25%"},
98-
"alt": "Martha Argerich",
98+
"alt": "Billie Holiday",
9999
}
100100
)
101101

102102
.. raw:: html
103103

104104
<img
105-
src="https://i.pinimg.com/564x/29/66/4d/29664d24bc793085a7671d7fc02a33f4.jpg"
105+
src="https://picsum.photos/id/237/500/300"
106106
style="width: 50%; margin-left: 25%;"
107-
alt="Martha Argerich"
107+
alt="Billie Holiday"
108108
/>
109109

110110

docs/source/creating-interfaces/index.rst

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Creating Interfaces
66

77
html-with-idom
88
your-first-components
9-
parametrizing-components
10-
conditional-rendering
119
dynamic-element-children
1210

1311
.. dropdown:: :octicon:`bookmark-fill;2em` What You'll Learn
@@ -21,35 +19,19 @@ Creating Interfaces
2119
:link: html-with-idom
2220
:link-type: doc
2321

24-
Learn how to construct HTML layouts with IDOM and the underlying data
25-
structure we use to represent them.
22+
Learn how to construct HTML layouts with IDOM.
2623

2724
.. grid-item-card:: :octicon:`package` Your First Components
2825
:link: your-first-components
2926
:link-type: doc
3027

31-
Discover what components are and why they're one of IDOM's foundational
32-
concepts.
33-
34-
.. grid-item-card:: :octicon:`plug` Parametrizing Components
35-
:link: parametrizing-components
36-
:link-type: doc
37-
38-
Leverage the reusability of components by passing them arguments
39-
40-
.. grid-item-card:: :octicon:`code-square` Conditional Rendering
41-
:link: conditional-rendering
42-
:link-type: doc
43-
44-
Use what you've learned so far to render display different views depending
45-
on a what a component's inputs are.
28+
Discover how to define and use components.
4629

4730
.. grid-item-card:: :octicon:`versions` Dynamic Element Children
4831
:link: dynamic-element-children
4932
:link-type: doc
5033

51-
Understand how to correctly render using lists of child elements that
52-
may change in length or order
34+
Correctly render lists of child elements that may change in length or order.
5335

5436
IDOM is a Python package for making user interfaces (UI). These interfaces are built
5537
from small elements of functionality like buttons text and images. IDOM allows you to
@@ -112,36 +94,9 @@ create them we need to add an ``@component`` `decorator
11294
<https://realpython.com/primer-on-python-decorators/>`__. To see what this looks like in
11395
practice we'll put the todo list HTML from above into a component:
11496

115-
.. example:: creating_interfaces.static_todo_list
97+
.. example:: creating_interfaces.simple_photo
11698
:activate-result:
11799

118-
If you explore a little bit on your own you'll find that, when called, functions which
119-
are decorated in this way don't return what you might initially expect:
120-
121-
.. testsetup::
122-
123-
from idom import ComponentType, component, html
124-
125-
@idom.component
126-
def App():
127-
# doesn't matter what we return here
128-
return ...
129-
130-
.. testcode::
131-
132-
from idom import ComponentType
133-
134-
assert isinstance(App(), ComponentType)
135-
136-
.. card::
137-
:link: html-with-idom
138-
:link-type: doc
139-
140-
:octicon:`book` Read More
141-
^^^^^^^^^^^^^^^^^^^^^^^^^
142-
143-
Discover what components are and why they're one of IDOM's foundational concepts.
144-
145100

146101
Section 3: Parametrizing Components
147102
-----------------------------------

docs/source/creating-interfaces/parametrizing-components.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/source/creating-interfaces/your-first-components.rst

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ components can then be reused throughout your application.
1111
Defining a Component
1212
--------------------
1313

14-
At their core, components are just a normal Python functions that return HTML. There's
15-
only two special things which we'll concer ourselves with there:
16-
17-
- We need to add a ``@component`` `decorator
18-
<https://realpython.com/primer-on-python-decorators/>`__
19-
to component function.
20-
21-
- By convention, we name component functions like classes - with ``CamelCase``.
22-
23-
So for example, if we wanted write and then :ref:`display <Running IDOM>` a ``Photo``
14+
At their core, components are just normal Python functions that return HTML. To define a
15+
component you just need to add a ``@component`` `decorator
16+
<https://realpython.com/primer-on-python-decorators/>`__ to a function. Then, by
17+
convention, we name component functions like classes - with ``CamelCase``. So for
18+
example, if we wanted to write and then :ref:`display <Running IDOM>` a ``Photo``
2419
component we might write:
2520

2621
.. example:: creating_interfaces.simple_photo
@@ -40,7 +35,49 @@ component we might write:
4035
Using a Component
4136
-----------------
4237

43-
Having defined our ``Photo`` component we can now nest it inside of other components:
38+
Having defined our ``Photo`` component we can now nest it inside of other components. We
39+
can define a "parent" ``Gallery`` component that returns one or more ``Profile``
40+
components. This is part of what makes components so powerful - you can define a
41+
component once and use it wherever and however you need to:
4442

4543
.. example:: creating_interfaces.nested_photos
4644
:activate-result:
45+
46+
47+
Parametrizing Components
48+
------------------------
49+
50+
Since components are just regular functions, you can add parameters to them. This allows
51+
parent components to pass information to child components. Where standard HTML elements
52+
are parametrized by dictionaries, since components behave like typical functions you can
53+
give them positional and keyword arguments as you would normally:
54+
55+
.. example:: creating_interfaces.parametrized_photos
56+
:activate-result:
57+
58+
59+
Conditional Rendering
60+
---------------------
61+
62+
Your components will often need to display different things depending on different
63+
conditions. Let's imagine that we had a basic todo list where only some of the items
64+
have been completed. Below we have a basic implementation for such a list except that
65+
the ``Item`` component doesn't change based on whether it's ``done``:
66+
67+
.. example:: creating_interfaces.todo_list
68+
:activate-result:
69+
70+
Let's imagine that we want to add a ✔ to the items which have been marked ``done=True``.
71+
One way to do this might be to write an ``if`` statement where we return one ``li``
72+
element if the item is ``done`` and a different one if it's not:
73+
74+
.. example:: creating_interfaces.bad_conditional_todo_list
75+
:activate-result:
76+
77+
As you can see this accomplishes our goal! However, notice how similar ``html.li(name, "
78+
✔")`` and ``html.li(name)`` are. While in this case it isn't especially harmful, we
79+
could make our code a little easier to read and maintain by using an "inline" ``if``
80+
statement.
81+
82+
.. example:: creating_interfaces.good_conditional_todo_list
83+
:activate-result:

docs/source/managing-state/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Managing State
44
.. toctree::
55
:hidden:
66

7+
keeping-components-pure
78
logical-flow-of-state
89
structuring-your-state
910
shared-component-state
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Keeping Components Pure
2+
=======================
3+
4+
Under construction :)

0 commit comments

Comments
 (0)