Skip to content

Commit 7cbda63

Browse files
authored
Merge pull request #40 from pyscript/fpliger/pydom_initial_docs
Add Pydom Docs
2 parents ad4f7f1 + 2d5b0a5 commit 7cbda63

File tree

1 file changed

+173
-5
lines changed

1 file changed

+173
-5
lines changed

docs/user-guide/dom.md

Lines changed: 173 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,185 @@ equivalent values: `["hello", 1, 2, 3]`.
7171

7272
## PyDom
7373

74-
The built-in Python module `pydom` wraps many (although not all) the features
75-
available via the FFI in an idiomatically Pythonic manner.
74+
The Standard Web APIs are massive and not always very user-friendly. `PyDom` is a
75+
Python modue that exposes the power of the web with an easy and idiomatic Pythonic
76+
interface on top.
7677

78+
While the [FFI](#ffi) interface described above focuses on giving full access to
79+
the entire Standard Web APIs, `pydom` focuses on providing a small, intuitive and yet
80+
powerful API that priotirizes common use cases fist. For this reason, it's first
81+
layer is simple and intuitive (but limited to the most common use cases), but `pydom`
82+
also provides a secondary layer that can be used to directly use full FFI interface
83+
of a specific element.
7784

78-
The PyDom API is extensively described and demonstrated
79-
[on this PyScript page](https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html).
85+
It does not aim to replace the regular Web [Javascript] API nor to be as wide and offer
86+
feature parity. On the contrary, it's intentionally small and focused on the most popular
87+
use cases while still providing [a backdoor] access to the full JS API.
88+
89+
`Pydom` draws inspiration from popular Python APIs/Libraries known to be friendly and
90+
easy to learn, and other successful projects related the web as well (for isntance,
91+
`JQuery` was a good source of inspiration).
8092

8193
!!! warning
8294

8395
PyDom is currently a work in progress.
8496

8597
We welcome feedback and suggestions.
8698

87-
**TODO Fabio to finish**
99+
100+
### Core Concepts
101+
102+
`Pydom` builds on topic of very few and simple core concepts:
103+
104+
* __`Element`:__ any component that is part of a web page. This is a rough abstraction of an
105+
[HTMLElement](https://developer.mozilla.org/en-US/docs/Glossary/Element). In general,
106+
`pydom` elements always map to an underlying `HTML` `Element` in a we page
107+
* __`ElementCollection`:__ a collection of one or more `Elements`. It is a rough abstraction
108+
of a [HTMLCollection](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection).
109+
* __Querying:__ a method to query elements on a page based on a
110+
[selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors). Pydom supports
111+
standard HTML DOM query selectors to [locate DOM elements](https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors) as other native `JavaScript` methods like
112+
`querySelector` or `querySelectorAll`.
113+
114+
Following, we'll look into each one of these aspects a bit more in detail.
115+
116+
### Element
117+
118+
`pydom` `Element` is simply just an abstraction of a tranditional `Element` in a web page.
119+
Every `Element` always maps to an underlying `JavaScript` `Element` in a web page. These 2
120+
elements are always in sync and any change of state in one is reflect into the other.
121+
122+
#### Creating a new element
123+
124+
New elements can be created by using the `pydom.create` method and passing the type of element
125+
being crated. Here's an example of what it looks like:
126+
127+
(To execute and explore the following code, click on the "load" button. The result will be
128+
conveniently displayed in the box on the below of the code example)
129+
130+
```python
131+
from pyweb import pydom
132+
133+
# Creating an element directly from pydom creates an unbounded element.
134+
new_div = pydom.create("div")
135+
136+
# Creating an element from another element automatically creates that element
137+
# as a child of the original element
138+
new_p = new_div.create("p", classes=["code-description"], html="Ciao PyScripters!")
139+
140+
# elements can be appended to any other element on the page
141+
pydom['#element-creation-example'][0].append(new_div)
142+
```
143+
144+
<div>
145+
<h5>Result will go here</h5>
146+
<div id="pydom-element-createion-example"></div>
147+
</div>
148+
149+
150+
For more details about `pydom.create` please refer to its reference documentation.
151+
152+
#### Setting the content of an element
153+
154+
The Element interface offers 2 main ways to set an element content: the `html` and the
155+
`content` attributes:
156+
157+
* `content`: sets the `innerHTML` field via the PyScript `display` function. This takes care
158+
of properly rendering the object being passed based on the object mimetype. So, for instance,
159+
if the objects is an image, it'll be properly rendered on the element
160+
* `html`: directly sets the `innerHTML` field of the underlying element without attemnpting
161+
any conversion.
162+
163+
In general, we suggest using `content` directly as it'll take care of most use cases without
164+
requiring any extra logic from the user.
165+
166+
#### Changing the element style
167+
168+
Elements have a `style` attribute that can be used to change the element style rules.
169+
The style attribyte can be used as a dictionary and, to set a style rule for the element,
170+
simply set the correct key on the `.style` attribute. For instance, the following
171+
code changes the background color of the element just created in the example above:
172+
173+
```python
174+
new_p.style["background-color"] = "yellow"
175+
```
176+
177+
to remove a specific style key, simply use the `pop` method as you'd to to remove
178+
a key from a dictionary:
179+
180+
```python
181+
new_p.style.pop("background-color")
182+
```
183+
184+
In addition to the dictionary interface to explicitly set CSS rules, the `style` attribute
185+
also offers a convenient `visible` property that can be use show/hide an element.
186+
187+
```python
188+
new_p.style.visible = False
189+
```
190+
191+
#### Other useful aspects of the Element API
192+
193+
* `append`: method to append a new child to the element.
194+
* `children`: list of the children of the element.
195+
* `value`: allows to set the `value` attribute of an element.
196+
* `clone`: method that creates a clone of the element. NODE: The clone elements will not be
197+
attached to any element.
198+
* `show_me`: method to scroll the page to where the element is placed.
199+
200+
201+
### ElementCollection
202+
203+
Element Collections represent a collection of elements typically returned from a query. For instance:
204+
205+
```python
206+
paragraphs = pydom['p']
207+
```
208+
209+
In the example above, `paragraphs` is an `ElementCollection` that maps to all `p` elements in the page.
210+
211+
As any collections, `ElementCollection` can be used to iterate over a collection of elements or to pick
212+
specific elements or slices of elements in the collection. For instance:
213+
214+
```python
215+
for element in paragraphs:
216+
display(element.html)
217+
218+
# let's now display only the last 2 elements
219+
for element in paragraphs[-2:]:
220+
display(element.html)
221+
```
222+
223+
#### Interacting with an ElementCollection
224+
225+
Besides from allowing operations as an iterable object, `ElementCollection` objects also offer a few
226+
convenient methods to directly interact with th elements in the collection. For instance, it's possible
227+
to ask for specific attributes of the elements in the collection directly:
228+
229+
```python
230+
display(paragraphs.html)
231+
```
232+
233+
The example above displays a list with the value of the `html` attribute for all the elements in the
234+
`paragraphs` collection.
235+
236+
The same way we can read attributes, we can also set an attribute directly in the collection. For instance,
237+
you can directly set the html content of all the elements in the collection
238+
239+
```python
240+
# This will change the text of all H1 elements in the page
241+
pydom['h1'].html = "That's cool :)"
242+
```
243+
244+
or perhaps change their style
245+
246+
```
247+
paragraphs.style['background-color'] = 'lightyellow'
248+
```
249+
250+
`ElementCollection` currently support the following attributes:
251+
252+
* `style`: just like in `Element`, this proxy attribute can be used to change the style of the elements in
253+
a collection by setting the proper CSS rules, using `style` with the same API as a dictionary.
254+
* `html`: allows to change the `html` attribute on all the elements of a collection.
255+
* `value`: allows to change the `value` attribute on all the elements of a collection.

0 commit comments

Comments
 (0)