Skip to content

Commit a25b637

Browse files
committed
Update pyscript.web docs.
1 parent f5c94e0 commit a25b637

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

docs/api.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,29 +413,34 @@ store = await storage("my-data-store", storage_class=MyStorage)
413413

414414
### `pyscript.web`
415415

416-
TODO: Use `display(element)` not `element.display()`.
417-
418416
The classes and references in this namespace provide a Pythonic way to interact
419417
with the DOM. An explanation for how to idiomatically use this API can be found
420418
[in the user guide](../user-guide/dom/#pyscriptweb)
421419

422-
#### `pyscript.web.dom`
420+
#### `pyscript.web.page`
423421

424-
This object has two attributes and a single method:
422+
This object represents a web page. It has four attributes and two methods:
425423

424+
* `html` - a reference to a Python object representing the [document's html](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html) root element.
426425
* `head` - a reference to a Python object representing the [document's head](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head).
427426
* `body` - a reference to a Python object representing the [document's body](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body).
427+
* `title` - the page's [title](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title) (usually displayed in the browser's title bar or a page's tab.
428428
* `find` - a method that takes a single [selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors)
429429
argument and returns a collection of Python objects representing the matching
430430
elements.
431+
* `append` - a shortcut for `page.body.append` (to add new elements to the
432+
page).
433+
434+
You may also shortcut the `find` method by enclosing a CSS selector in square
435+
brackets: `page["#my-thing"]`.
431436

432437
These are provided as a convenience so you have several simple and obvious
433-
options for accessing the content of the page (DOM).
438+
options for accessing and changing the content of the page.
434439

435440
All the Python objects returned by these attributes and method are instances of
436-
classes defined in the `pyscript.web.elements` namespace.
441+
classes relating to HTML elements defined in the `pyscript.web` namespace.
437442

438-
#### `pyscript.web.elements.*`
443+
#### `pyscript.web.*`
439444

440445
There are many classes in this namespace. Each is a one-to-one mapping of any
441446
HTML element name to a Python class representing the HTML element of that

docs/user-guide/dom.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,39 +99,45 @@ There are three core concepts to remember:
9999
The `find` API uses exactly the [same queries](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Locating_DOM_elements_using_selectors)
100100
as those used by native browser methods like `qurerySelector` or
101101
`querySelectorAll`.
102-
* Use classes in the `pyscript.web.elements` namespace to create and organise
102+
* Use classes in the `pyscript.web` namespace to create and organise
103103
new elements on the web page.
104104
* Collections of elements allow you to access and change attributes en-mass.
105105
Such collections are returned from `find` queries and are also used for the
106106
[children](https://developer.mozilla.org/en-US/docs/Web/API/Element/children)
107107
of an element.
108108

109-
You have several options for accessing the content of the page (i.e. the DOM),
110-
and these can be found in the `pyscript.web.dom` object. The `head` and `body`
111-
attributes reference the page's head and body. Furthermore, the `find` method
112-
can be used to return collections of elements matching your CSS query. Finally,
113-
all elements have a `find` method that searches within their children for
114-
elements matching your CSS query.
109+
You have several options for accessing the content of the page, and these are
110+
all found in the `pyscript.web.page` object. The `html`, `head` and `body`
111+
attributes reference the page's top-level html, head and body. As a convenience
112+
the `page`'s `title` attribute can be used to get and set the web page's title
113+
(usually shown in the browser's tab). The `append` method is a shortcut for
114+
adding content to the page's `body`. Whereas, the `find` method is used to
115+
return collections of elements matching a CSS query. You may also shortcut
116+
`find` via a CSS query in square brackets. Finally, all elements have a `find`
117+
method that searches within their children for elements matching your CSS
118+
query.
115119

116120
```python
117-
from pyscript.web import dom
121+
from pyscript.web import page
118122

119123

120124
# Print all the child elements of the document's head.
121-
print(dom.head.children)
125+
print(page.head.children)
122126
# Find all the paragraphs in the DOM.
123-
paragraphs = dom.find("p")
127+
paragraphs = page.find("p")
128+
# Or use square brackets.
129+
paragraphs = page["p"]
124130
```
125131

126132
The object returned from a query, or used as a reference to an element's
127133
children is iterable:
128134

129135
```python
130-
from pyscript.web import dom
136+
from pyscript.web import page
131137

132138

133139
# Get all the paragraphs in the DOM.
134-
paragraphs = dom.find("p")
140+
paragraphs = page["p"]
135141

136142
# Print the inner html of each paragraph.
137143
for p in paragraphs:
@@ -141,38 +147,36 @@ for p in paragraphs:
141147
Alternatively, it is also indexable / sliceable:
142148

143149
```python
144-
from pyscript.web import dom
150+
from pyscript.web import page
145151

146152

147153
# Get an ElementCollection of all the paragraphs in the DOM
148-
paragraphs = dom.find("p")
154+
paragraphs = page["p"]
149155

150156
# Only the final two paragraphs.
151157
for p in paragraphs[-2:]:
152158
print(p.html)
153159
```
154160

155-
It also makes available the following read and writable attributes related to
156-
all contained elements:
161+
You have access to all the standard attributes related to HTML elements (for
162+
example, the `innerHTML` or `value`), along with a couple of convenient ways
163+
to interact with classes and CSS styles:
157164

158165
* `classes` - the list of classes associated with the elements.
159-
* `innerHTML` - the innerHTML of each element.
160166
* `style` - a dictionary like object for interacting with CSS style rules.
161-
* `value` - the `value` attribute associated with each element.
162167

163168
For example, to continue the example above, `paragraphs.innerHTML` will return
164169
a list of all the values of the `innerHTML` attribute on each contained
165170
element. Alternatively, set an attribute for all elements contained in the
166171
collection like this: `paragraphs.style["background-color"] = "blue"`.
167172

168-
It's possible to create new elements to add to the DOM:
173+
It's possible to create new elements to add to the page:
169174

170175
```python
171-
from pyscript.web import dom
172-
from pyscript.web.elements import *
176+
from pyscript.web import page, div, select, option, button, span, br
173177

174178

175-
dom.body.append(
179+
page.append(
176180
div(
177181
div("Hello!", classes="a-css-class", id="hello"),
178182
select(
@@ -206,7 +210,7 @@ dom.body.append(
206210
```
207211

208212
This example demonstrates a declaritive way to add elements to the body of the
209-
DOM. Notice how the first (unnamed) arguments to an element are its children.
213+
page. Notice how the first (unnamed) arguments to an element are its children.
210214
The named arguments (such as `id`, `classes` and `style`) refer to attributes
211215
of the underlying HTML element. If you'd rather be explicit about the children
212216
of an element, you can always pass in a list of such elements as the named
@@ -216,9 +220,7 @@ Of course, you can achieve similar results in an imperative style of
216220
programming:
217221

218222
```python
219-
from pyscript.web import dom
220-
from pyscript.web.elements import *
221-
223+
from pyscript.web import page, div, p
222224

223225

224226
my_div = div()
@@ -238,10 +240,10 @@ element references from `pyscript.web`:
238240

239241
```python
240242
from pyscript import when
241-
from pyscript.web import dom
243+
from pyscript.web import page
242244

243245

244-
btn = dom.find("#my-button")
246+
btn = page["#my-button"]
245247

246248

247249
@when("click", btn)

0 commit comments

Comments
 (0)