Represents the current web page. Provides access to the document's `html`, `head`, and `body` elements, plus convenience methods for finding elements and appending to the body. ```python from pyscript import web # Access page structure. web.page.html # The el
| 1369 | |
| 1370 | |
| 1371 | class Page: |
| 1372 | """ |
| 1373 | Represents the current web page. |
| 1374 | |
| 1375 | Provides access to the document's `html`, `head`, and `body` elements, |
| 1376 | plus convenience methods for finding elements and appending to the body. |
| 1377 | |
| 1378 | ```python |
| 1379 | from pyscript import web |
| 1380 | |
| 1381 | |
| 1382 | # Access page structure. |
| 1383 | web.page.html # The <html> element. |
| 1384 | web.page.head # The <head> element. |
| 1385 | web.page.body # The <body> element. |
| 1386 | |
| 1387 | # Get and set page title. |
| 1388 | web.page.title = "New Title" |
| 1389 | print(web.page.title) |
| 1390 | |
| 1391 | # Find elements by CSS selector. |
| 1392 | divs = web.page.find("div") |
| 1393 | items = web.page.find(".item-class") |
| 1394 | |
| 1395 | # Look up element by id. |
| 1396 | element = web.page["my-id"] |
| 1397 | element = web.page["#my-id"] # The "#" prefix is optional. |
| 1398 | |
| 1399 | # Append to the body (shortcut for page.body.append). |
| 1400 | web.page.append(web.div("Hello")) |
| 1401 | ``` |
| 1402 | """ |
| 1403 | |
| 1404 | def __init__(self): |
| 1405 | self.html = Element.wrap_dom_element(document.documentElement) |
| 1406 | self.body = Element.wrap_dom_element(document.body) |
| 1407 | self.head = Element.wrap_dom_element(document.head) |
| 1408 | |
| 1409 | def __getitem__(self, key): |
| 1410 | """ |
| 1411 | Look up an element by id. |
| 1412 | |
| 1413 | The '#' prefix is optional and will be stripped if present. |
| 1414 | Returns None if no element with that id exists. |
| 1415 | |
| 1416 | ```python |
| 1417 | page["my-id"] # Element with id="my-id" (or None) |
| 1418 | page["#my-id"] # Same as above (# is optional) |
| 1419 | ``` |
| 1420 | """ |
| 1421 | return _find_by_id(document, key) |
| 1422 | |
| 1423 | @property |
| 1424 | def title(self): |
| 1425 | """ |
| 1426 | Get the page `title`. |
| 1427 | """ |
| 1428 | return document.title |