@@ -6,13 +6,15 @@ with its backing data and logic are distributed between a client and server
6
6
respectively. With IDOM, both these tasks are centralized in a single place. This is
7
7
done by allowing HTML interfaces to be constructed in Python. Take a look at the two
8
8
code examples below. The one on the left shows how to make a basic title and todo list
9
- using standard HTML while the one of the right uses IDOM in Python:
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:
10
11
11
12
.. grid :: 2
12
13
:margin: 0
13
14
:padding: 0
14
15
15
16
.. grid-item ::
17
+ :columns: 6
16
18
17
19
.. code-block :: html
18
20
@@ -23,6 +25,7 @@ using standard HTML while the one of the right uses IDOM in Python:
23
25
</ul >
24
26
25
27
.. grid-item ::
28
+ :columns: 6
26
29
27
30
.. testcode ::
28
31
@@ -34,12 +37,25 @@ using standard HTML while the one of the right uses IDOM in Python:
34
37
html.li("Share it with the world!"),
35
38
)
36
39
40
+ .. grid-item-card ::
41
+ :columns: 12
42
+
43
+ .. raw :: html
44
+
45
+ <div style =" width : 50% ; margin : auto ;" >
46
+ <h2 style =" margin-top : 0px !important ;" >My Todo List</h2 >
47
+ <ul >
48
+ <li >Build a cool new app</li >
49
+ <li >Share it with the world!</li >
50
+ </ul >
51
+ </div >
52
+
37
53
What this shows is that you can recreate the same HTML layouts with IDOM using functions
38
54
from the :mod: `idom.html ` module. These function share the same names as their
39
55
corresponding HTML tags. For example, the ``<h1/> `` element above has a similarly named
40
56
:func: `~idom.html.h1 ` function. With that said, while the code above looks similar, it's
41
57
not very useful because we haven't captured the results from these function calls in a
42
- variable. To do this we need to wraps up layout above into a single
58
+ variable. To do this we need to wrap up the layout above into a single
43
59
:func: `~idom.html.div ` and assign it to a variable:
44
60
45
61
.. testcode ::
@@ -52,44 +68,51 @@ variable. To do this we need to wraps up layout above into a single
52
68
),
53
69
)
54
70
55
- Having done this we can inspect what is contained in our new ``layout `` variable. As it
56
- turns out, it holds a dictionary. Printing it produces the following output:
71
+
72
+ Adding HTML Attributes
73
+ ----------------------
74
+
75
+ That's all well and good, but there's more to HTML than just text. What if we wanted to
76
+ display an image? In HTMl we'd use the `<img/> ` element and add attributes to it order
77
+ to specify a URL to its ``src `` and use some ``style `` to modify and position it:
78
+
79
+ .. code-block :: html
80
+
81
+ <img
82
+ src =" https://picsum.photos/500/300"
83
+ style =" width : 70% ; margin-left : 15% ;"
84
+ />
85
+
86
+ In IDOM we add these attributes to elements using dictionaries. There are some notable
87
+ differences though. The biggest being the fact that all names in IDOM use ``camelCase ``
88
+ instead of dash-sepearted words. For example, ``margin-left `` becomes ``marginLeft ``.
89
+ Additionally, instead of specifying ``style `` using a string, we use a dictionary:
57
90
58
91
.. testcode ::
59
92
60
- print(layout)
61
-
62
- .. testoutput ::
63
- :options: +NORMALIZE_WHITESPACE
64
-
65
- {
66
- 'tagName': 'div',
67
- 'children': [
68
- {
69
- 'tagName': 'h1',
70
- 'children': ['My Todo List']
71
- },
72
- {
73
- 'tagName': 'ul',
74
- 'children': [
75
- {'tagName': 'li', 'children': ['Build a cool new app']},
76
- {'tagName': 'li', 'children': ['Share it with the world!']}
77
- ]
78
- }
79
- ]
80
- }
81
-
82
- This may look complicated, but let's take a moment to consider what's going on here. We
83
- have a series of nested dictionaries that, in some way, represents the HTML structure
84
- given above. If we look at their contents we should see a common form. Each has a
85
- ``tagName `` key which contains, as the name would suggest, the tag name of an HTML
86
- element. Then within the ``children `` key is a list that either contains strings or
87
- other dictionaries that represent HTML elements.
88
-
89
- What we're seeing here is called a "virtual document object model" or :ref: `VDOM `. This
90
- is just a fancy way of saying we have a representation of the document object model or
91
- `DOM
92
- <https://en.wikipedia.org/wiki/Document_Object_Model#:~:text=The%20Document%20Object%20Model%20(DOM,document%20with%20a%20logical%20tree.&text=Nodes%20can%20have%20event%20handlers%20attached%20to%20them.> `__
93
- that is not the actual DOM. We'll talk more about this concept :ref: `in the future
94
- <Communication Scheme>`, but for now, just understand that in IDOM, we represent the
95
- HTML document object model using dictionaries that we call VDOM.
93
+ html.img(
94
+ {
95
+ "src": "https://picsum.photos/500/300",
96
+ "style": {"width": "70%", "marginLeft": "15%"},
97
+ }
98
+ )
99
+
100
+ .. raw :: html
101
+
102
+ <img
103
+ src =" https://picsum.photos/500/300"
104
+ style =" width : 70% ; margin-left : 15% ;"
105
+ />
106
+
107
+
108
+ ----------
109
+
110
+
111
+ .. card ::
112
+ :link: /understanding-idom/representing-html
113
+ :link-type: doc
114
+
115
+ :octicon: `book ` Read More
116
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
117
+
118
+ Dive into the data structures IDOM uses to represent HTML
0 commit comments