Skip to content

Commit 2a54b34

Browse files
authored
reorganize creating-interfaces + add info on fragments (#685)
1 parent fdad4fd commit 2a54b34

17 files changed

+102
-21
lines changed

docs/source/creating-interfaces/html-with-idom.rst renamed to docs/source/creating-interfaces/html-with-idom/index.rst

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
HTML With IDOM
22
==============
33

4-
In a typical Python-base web application the resonsibility of defining the view along
4+
In a typical Python-base web application the responsibility of defining the view along
55
with its backing data and logic are distributed between a client and server
66
respectively. With IDOM, both these tasks are centralized in a single place. This is
77
done by allowing HTML interfaces to be constructed in Python. Take a look at the two
8-
code examples below. The one on the left shows how to make a basic title and todo list
9-
using standard HTML, the one of the right uses IDOM in Python, and below is a view of
10-
what the HTML would look like if displayed:
8+
code examples below. The first one shows how to make a basic title and todo list using
9+
standard HTML, the second uses IDOM in Python, and below is a view of what the HTML
10+
would look like if displayed:
1111

1212
.. grid:: 1 1 2 2
1313
:margin: 0
1414
:padding: 0
1515

1616
.. grid-item::
17-
:columns: 6
1817

1918
.. code-block:: html
2019

@@ -25,7 +24,6 @@ what the HTML would look like if displayed:
2524
</ul>
2625

2726
.. grid-item::
28-
:columns: 6
2927

3028
.. testcode::
3129

@@ -86,7 +84,7 @@ to specify a URL to its ``src`` and use some ``style`` to modify and position it
8684

8785
In IDOM we add these attributes to elements using dictionaries. There are some notable
8886
differences though. The biggest being the fact that all names in IDOM use ``camelCase``
89-
instead of dash-sepearted words. For example, ``margin-left`` becomes ``marginLeft``.
87+
instead of dash-separated words. For example, ``margin-left`` becomes ``marginLeft``.
9088
Additionally, instead of specifying ``style`` using a string, we use a dictionary:
9189

9290
.. testcode::

docs/source/creating-interfaces/index.rst

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Creating Interfaces
44
.. toctree::
55
:hidden:
66

7-
html-with-idom
8-
your-first-components
9-
rendering-data
7+
html-with-idom/index
8+
your-first-components/index
9+
rendering-data/index
1010

1111
.. dropdown:: :octicon:`bookmark-fill;2em` What You'll Learn
1212
:color: info
@@ -16,20 +16,20 @@ Creating Interfaces
1616
.. grid:: 1 2 2 2
1717

1818
.. grid-item-card:: :octicon:`code-square` HTML with IDOM
19-
:link: html-with-idom
19+
:link: html-with-idom/index
2020
:link-type: doc
2121

2222
Construct HTML layouts from the basic units of user interface functionality.
2323

2424
.. grid-item-card:: :octicon:`package` Your First Components
25-
:link: your-first-components
25+
:link: your-first-components/index
2626
:link-type: doc
2727

2828
Define reusable building blocks that it easier to construct complex
2929
interfaces.
3030

3131
.. grid-item-card:: :octicon:`database` Rendering Data
32-
:link: rendering-data
32+
:link: rendering-data/index
3333
:link-type: doc
3434

3535
Use data to organize and render HTML elements and components.
@@ -75,7 +75,7 @@ To recreate the same thing in IDOM you would write:
7575
)
7676
7777
.. card::
78-
:link: html-with-idom
78+
:link: html-with-idom/index
7979
:link-type: doc
8080

8181
:octicon:`book` Read More
@@ -94,10 +94,10 @@ create them we need to add an ``@component`` `decorator
9494
<https://realpython.com/primer-on-python-decorators/>`__. To see what this looks like in
9595
practice we'll quickly make a ``Photo`` component:
9696

97-
.. idom:: _examples/simple_photo
97+
.. idom:: your-first-components/_examples/simple_photo
9898

9999
.. card::
100-
:link: your-first-components
100+
:link: your-first-components/index
101101
:link-type: doc
102102

103103
:octicon:`book` Read More
@@ -116,10 +116,10 @@ from data in this way must be orgnized with :ref:`"keys" <Organizing Items With
116116
One case where we might want to do this is if items in a todo list come from a list of
117117
data that we want to sort and filter:
118118

119-
.. idom:: _examples/todo_list_with_keys
119+
.. idom:: rendering-data/_examples/todo_list_with_keys
120120

121121
.. card::
122-
:link: rendering-data
122+
:link: rendering-data/index
123123
:link-type: doc
124124

125125
:octicon:`book` Read More

docs/source/creating-interfaces/_examples/parametrized_photos.py renamed to docs/source/creating-interfaces/your-first-components/_examples/parametrized_photos.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
def Photo(alt_text, image_id):
66
return html.img(
77
{
8-
"src": f"https://picsum.photos/id/{image_id}/500/300",
8+
"src": f"https://picsum.photos/id/{image_id}/500/200",
99
"style": {"width": "50%"},
1010
"alt": alt_text,
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from idom import component, html, run
2+
3+
4+
@component
5+
def MyTodoList():
6+
return html.div(
7+
html.h1("My Todo List"),
8+
html.img({"src": "https://picsum.photos/id/0/500/300"}),
9+
html.ul(html.li("The first thing I need to do is...")),
10+
)
11+
12+
13+
run(MyTodoList)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from idom import component, html, run
2+
3+
4+
@component
5+
def MyTodoList():
6+
return html._(
7+
html.h1("My Todo List"),
8+
html.img({"src": "https://picsum.photos/id/0/500/200"}),
9+
html.ul(html.li("The first thing I need to do is...")),
10+
)
11+
12+
13+
run(MyTodoList)

docs/source/creating-interfaces/your-first-components.rst renamed to docs/source/creating-interfaces/your-first-components/index.rst

+57
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,63 @@ component once and use it wherever and however you need to:
4242
.. idom:: _examples/nested_photos
4343

4444

45+
Return a Single Root Element
46+
----------------------------
47+
48+
Components must return a "single root element". That one root element may have children,
49+
but you cannot for example, return a list of element from a component and expect it to
50+
be rendered correctly. If you want to return multiple elements you must wrap them in
51+
something like a :func:`html.div <idom.html.div>`:
52+
53+
.. idom:: _examples/wrap_in_div
54+
55+
If don't want to add an extra ``div`` you can use a "fragment" instead with the
56+
:func:`html._ <idom.html._>` function:
57+
58+
.. idom:: _examples/wrap_in_fragment
59+
60+
Fragments allow you to group elements together without leaving any trace in the UI. For
61+
example, the first code sample written with IDOM will produce the second HTML code
62+
block:
63+
64+
.. grid:: 1 2 2 2
65+
:margin: 0
66+
:padding: 0
67+
68+
.. grid-item::
69+
70+
.. testcode::
71+
72+
from idom import html
73+
74+
html.ul(
75+
html._(
76+
html.li("Group 1 Item 1"),
77+
html.li("Group 1 Item 2"),
78+
html.li("Group 1 Item 3"),
79+
),
80+
html._(
81+
html.li("Group 2 Item 1"),
82+
html.li("Group 2 Item 2"),
83+
html.li("Group 2 Item 3"),
84+
)
85+
)
86+
87+
.. grid-item::
88+
89+
.. code-block:: html
90+
91+
<ul>
92+
<li>Group 1 Item 1</li>
93+
<li>Group 1 Item 2</li>
94+
<li>Group 1 Item 3</li>
95+
<li>Group 2 Item 1</li>
96+
<li>Group 2 Item 2</li>
97+
<li>Group 2 Item 3</li>
98+
</ul>
99+
100+
101+
45102
Parametrizing Components
46103
------------------------
47104

docs/source/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ combine these elements into reusable :ref:`"components" <your first components>`
123123
sections that follow you'll learn how these UI elements are created and organized into
124124
components. Then, you'll use this knowledge to create interfaces from raw data:
125125

126-
.. idom:: creating-interfaces/_examples/todo_list_with_keys
126+
.. idom:: creating-interfaces/rendering-data/_examples/todo_list_with_keys
127127

128128
.. card::
129129
:link: creating-interfaces/index

tests/test_core/test_hooks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,5 +1161,5 @@ def SetStateDuringRender():
11611161
assert render_count.current == 2
11621162

11631163
# there should be no more renders to perform
1164-
with pytest.raises(asyncio.exceptions.TimeoutError):
1164+
with pytest.raises(asyncio.TimeoutError):
11651165
await asyncio.wait_for(layout.render(), timeout=0.1)

0 commit comments

Comments
 (0)