pyscript.web
A lightweight Pythonic interface to the DOM and HTML elements that helps you interact with web pages, making it easy to find, create, manipulate, and compose HTML elements from Python.
Highlights include:
Use the page object to find elements on the current page:
from pyscript import web
# Find by CSS selector (returns an ElementCollection).
divs = web.page.find("div")
buttons = web.page.find(".button-class")
# Get element by ID (returns single Element or None).
header = web.page["header-id"]
header = web.page["#header-id"] # the "#" prefix is optional.
# Access page structure.
web.page.body.append(some_element)
web.page.title = "New Page Title"
Create new elements and compose them together:
# Create simple elements.
div = web.div("Hello, World!")
paragraph = web.p("Some text", id="my-para", className="text-content")
# Compose elements together.
container = web.div(
web.h1("Title"),
web.p("First paragraph"),
web.p("Second paragraph"),
id="container"
)
# Add to the page.
web.page.body.append(container)
# Create with initial attributes.
link = web.a(
"Click me",
href="https://example.com",
target="_blank",
classes=["link", "external"]
)
Modify element content and attributes:
# Update content.
element.innerHTML = "<b>Bold text</b>"
element.textContent = "Plain text"
# Update attributes.
element.id = "new-id"
element.title = "Tooltip text"
# Bulk update with convenience method.
element.update(
classes=["active", "highlighted"],
style={"color": "red", "font-size": "16px"},
title="Updated tooltip"
)
An element's CSS classes behave like a Python set:
# Add and remove classes
element.classes.add("active")
element.classes.add("highlighted")
element.classes.remove("hidden")
# Check membership.
if "active" in element.classes:
print("Element is active")
# Clear all classes.
element.classes.clear()
# Discard (no error if missing).
element.classes.discard("maybe-not-there")
An element's styles behave like a Python dict:
# Set individual styles.
element.style["color"] = "red"
element.style["background-color"] = "#f0f0f0"
element.style["font-size"] = "16px"
# Remove a style.
del element.style["margin"]
# Check if style is set.
if "color" in element.style:
print(f"Color is {element.style['color']}")
Update multiple elements at once via an ElementCollection:
# Find multiple elements (returns an ElementCollection).
items = web.page.find(".list-item")
# Iterate over collection.
for item in items:
item.innerHTML = "Updated"
item.classes.add("processed")
# Bulk update all elements.
items.update_all(
innerHTML="Hello",
className="updated-item"
)
# Index and slice collections.
first = items[0]
subset = items[1:3]
# Get an element by ID within the collection.
special = items["special-id"]
# Find descendants within the collection.
subitems = items.find(".sub-item")
Manage select element options (also for datalist and optgroup):
# Get existing select.
select = web.page["my-select"]
# Add options.
select.options.add(value="1", html="Option 1")
select.options.add(value="2", html="Option 2", selected=True)
# Get selected option.
selected = select.options.selected
print(f"Selected: {selected.value}")
# Iterate over options.
for option in select.options:
print(f"{option.value}: {option.innerHTML}")
# Clear all options.
select.options.clear()
# Remove specific option by index.
select.options.remove(0)
Attach event handlers to elements:
from pyscript import when
button = web.button("Click me", id="my-button")
# Use the when decorator.
@when("click", button)
def handle_click(event):
print("Button clicked!")
# Or add directly to the event.
def another_handler(event):
print("Another handler")
button.on_click.add_listener(another_handler)
# Pass handler during creation.
button = web.button("Click", on_click=handle_click)
All Element instances provide direct access to the underlying DOM element
via attribute delegation:
# Most DOM methods are accessible directly.
element.scrollIntoView()
element.focus()
element.blur()
# But we do have a historic convenience method for scrolling into view.
element.show_me() # Calls scrollIntoView()
# Access the raw DOM element when needed for special cases.
dom_element = element._dom_element
The main entry point is the page object, which represents the current
document and provides access to common elements like page.body and methods
like page.find() for querying the DOM.
page = Page()
module-attribute
A reference to the current web page. An instance of the Page class.
CONTAINER_TAGS = ['a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'blockquote', 'body', 'button', 'caption', 'cite', 'code', 'colgroup', 'data', 'dd', 'del', 'details', 'dialog', 'div', 'dl', 'dt', 'em', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'html', 'i', 'iframe', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'menu', 'meta', 'meter', 'nav', 'object', 'ol', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 's', 'script', 'section', 'small', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'u', 'ul', 'var', 'wbr']
module-attribute
Container elements that can have children. Each becomes a class in the
pyscript.web namespace and corresponds to an HTML tag.
VOID_TAGS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'source', 'track']
module-attribute
Void elements that cannot have children. Each becomes a class in the
pyscript.web namespace and corresponds to an HTML tag.
Element
The base class for all HTML elements.
Provides a Pythonic interface to DOM elements with support for attributes, events, styles, classes, and DOM manipulation. It can create new elements or wrap existing DOM elements.
Elements are typically created using the tag-specific classes found
within this namespace (e.g. web.div, web.span, web.button):
from pyscript import web
# Create a simple div.
div = web.div("Hello, World!")
# Create with attributes.
link = web.a("Click me", href="https://example.com", target="_blank")
# Create with classes and styles.
button = web.button(
"Submit",
classes=["primary", "large"],
style={"background-color": "blue", "color": "white"},
id="submit-btn"
)
Info
Some elements have an underscore suffix in their class names (e.g.
select_, input_).
This is to avoid clashes with Python keywords. The underscore is removed when determining the actual HTML tag name.
Wrap existing DOM elements found on the page:
# Find and wrap an element by CSS selector.
existing = web.page.find(".my_class")[0]
# Or, better, just use direct ID lookup (with or without the
# leading '#').
existing = web.page["my-element"]
Element attributes are accessible as Python properties:
# Get attributes.
element_id = div.id
element_title = div.title
element_href = link.href
# Set attributes.
div.id = "new-id"
div.title = "Tooltip text"
link.href = "https://new-url.com"
# HTML content.
div.innerHTML = "<b>Bold text</b>"
div.textContent = "Plain text"
CSS classes are managed through a set-like interface:
# Add classes.
element.classes.add("active")
element.classes.add("highlighted")
# Remove classes.
element.classes.remove("inactive")
element.classes.discard("maybe-missing") # No error if absent
# Check membership.
if "active" in element.classes:
print("Element is active")
# Iterate over classes.
for cls in element.classes:
print(cls)
Explicit CSS styles are managed through a dict-like interface:
# Set styles using CSS property names (hyphenated).
element.style["color"] = "red"
element.style["background-color"] = "#f0f0f0"
element.style["font-size"] = "16px"
# Get styles.
color = element.style["color"]
# Remove styles.
del element.style["margin"]
Add, find, and navigate elements:
# Append children.
parent.append(child_element)
parent.append(child1, child2, child3) # Multiple at once
# Find descendants using CSS selectors.
buttons = parent.find("button")
items = parent.find(".item-class")
# Navigate the tree.
child = parent.children[0]
parent_elem = child.parent
# Access children by index or slice.
first_child = parent[0]
first_three = parent[0:3]
# Get a child explicitly by ID. Returns None if not found.
specific = parent["child-id"]
Attach event listeners to elements:
button = web.button("Click me")
# Use the @when decorator with event name.
from pyscript import when
@when("click", button)
def handle_click(event):
print("Clicked!")
# Or use the on_* event directly with @when.
@when(button.on_click)
def handle_click(event):
print("Also works!")
# Pass handlers during element creation.
button = web.button("Click", on_click=handle_click)
Update multiple properties at once:
element.update(
classes=["active", "highlighted"],
style={"color": "red", "font-size": "20px"},
id="updated-id",
title="New tooltip"
)
Warning
Some HTML attributes clash with Python keywords and use trailing underscores.
Use for_ instead of for, and class_ instead of class.
# The 'for' attribute (on labels)
label = web.label("Username", for_="username-input")
# The 'class' attribute (although 'classes' is preferred)
div.class_ = "my-class"
Create copies of elements:
Access the underlying DOM element when needed:
# Most DOM properties and methods are accessible directly.
element.focus()
element.scrollIntoView()
bounding_rect = element.getBoundingClientRect()
# Or access the raw DOM element.
dom_element = element._dom_element
Source code in pyscript/web.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | |
children
property
Return this element's children as an ElementCollection.
classes
property
style
property
Return the element's CSS styles as a dict-like Style object.
Access using dict-style syntax with standard
CSS property names (hyphenated).
parent
property
Return this element's parent Element, or None.
get_tag_name()
classmethod
Get the HTML tag name for this class.
Classes ending with underscore (e.g. input_) have it removed to get
the actual HTML tag name.
register_element_classes(element_classes)
classmethod
Register Element subclasses for tag-based lookup.
Source code in pyscript/web.py
unregister_element_classes(element_classes)
classmethod
Unregister Element subclasses from tag-based lookup.
Source code in pyscript/web.py
wrap_dom_element(dom_element)
classmethod
Wrap a DOM element in the appropriate Element subclass.
Looks up the subclass by tag name. Unknown tags use the base Element
class.
Source code in pyscript/web.py
__init__(dom_element=None, classes=None, style=None, **kwargs)
Create or wrap a DOM element.
If dom_element is None, this creates a new element. Otherwise wraps
the provided DOM element. The **kwargs can include HTML attributes
and event handlers (names starting with on_).
Source code in pyscript/web.py
__eq__(obj)
__getitem__(key)
Get an item within this element.
Behaviour depends on the key type:
- Integer: returns the child at that index.
- Slice: returns a collection of children in that slice.
- String: looks up an element by id (with or without '#' prefix).
element[0] # First child.
element[1:3] # Second and third children.
element["my-id"] # Element with id="my-id" (or None).
element["#my-id"] # Same as above (# is optional).
Source code in pyscript/web.py
__getattr__(name)
Get an attribute from the element.
Attributes starting with on_ return Event instances. Other
attributes are retrieved from the underlying DOM element.
Source code in pyscript/web.py
__setattr__(name, value)
Set an attribute on the element.
Private attributes (starting with _) are set on the Python object.
Public attributes are set on the underlying DOM element. Attributes
starting with on_ are treated as events.
Source code in pyscript/web.py
get_event(name)
Get an Event instance for the specified event name.
Event names must start with on_ (e.g. on_click). Creates and
caches Event instances that are triggered when the DOM event fires.
Source code in pyscript/web.py
append(*items)
Append items to this element's children.
Accepts Element instances, ElementCollection instances, lists,
tuples, raw DOM elements, and NodeLists.
Source code in pyscript/web.py
clone(clone_id=None)
Clone this element and its underlying DOM element.
Optionally assign a new id to the clone.
Source code in pyscript/web.py
find(selector)
Find all descendant elements matching the
CSS selector.
Returns an ElementCollection (possibly empty).
element.find("div") # All div descendants.
element.find(".my-class") # All elements with class.
element.find("#my-id") # Element with id (as collection).
element.find("div.my-class") # All divs with class.
Source code in pyscript/web.py
show_me()
update(classes=None, style=None, **kwargs)
Update this element's classes, style, and attributes
(via arbitrary **kwargs).
Convenience method for bulk updates.
Source code in pyscript/web.py
ContainerElement
Bases: Element
Base class for elements that can contain other elements.
Extends Element with convenient child handling during initialization.
from pyscript import web
# Create with child elements as arguments.
div = web.div(
web.h1("Title"),
web.p("Paragraph 1"),
web.p("Paragraph 2")
)
# Or use the children keyword argument.
div = web.div(children=[web.p("Child 1"), web.p("Child 2")])
# Mix elements and HTML strings.
div = web.div(
web.h1("Title"),
"<p>HTML content</p>",
web.button("Click me")
)
# Iterate over children.
for child in div:
print(child.innerHTML)
Source code in pyscript/web.py
__init__(*args, children=None, dom_element=None, style=None, classes=None, **kwargs)
Create a container element with optional children.
Children can be passed as positional *args or via the children
keyword argument. String children are inserted as unescaped HTML. The
style, classes, and **kwargs are passed to the base Element
initializer.
Source code in pyscript/web.py
ElementCollection
A collection of Element instances with list-like operations.
Supports iteration, indexing, slicing, and finding descendants.
For bulk operations, iterate over the collection explicitly or use
the update_all method.
# Get a collection of elements.
items = web.page.find(".item")
# Access by index.
first = items[0]
last = items[-1]
# Slice the collection.
subset = items[1:3]
# Look up a specific element by id (returns None if not found).
specific = items["item-id"]
# Iterate over elements.
for item in items:
item.innerHTML = "Updated"
item.classes.add("processed")
# Bulk update all contained elements.
items.update_all(innerHTML="Hello", className="updated")
# Find matches within the collection.
buttons = items.find("button")
# Get the count.
count = len(items)
Source code in pyscript/web.py
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 | |
elements
property
Return the underlying list of elements.
wrap_dom_elements(dom_elements)
classmethod
Wrap an iterable of DOM elements in an ElementCollection.
__eq__(obj)
__getitem__(key)
Get items from the collection.
Behaviour depends on the key type:
- Integer: returns the element at that index.
- Slice: returns a new collection with elements in that slice.
- String: looks up an element by id (with or without '#' prefix).
collection[0] # First element.
collection[1:3] # New collection with 2nd and 3rd elements.
collection["my-id"] # Element with id="my-id" (or None).
collection["#my-id"] # Same as above (# is optional).
Source code in pyscript/web.py
find(selector)
Find all descendants matching the
CSS selector.
Searches within all elements in the collection.
collection.find("div") # All div descendants.
collection.find(".my-class") # All elements with class.
collection.find("#my-id") # Element with id (as collection).
Source code in pyscript/web.py
update_all(**kwargs)
Explicitly update all elements with the given attributes.
Source code in pyscript/web.py
Classes
Bases: set
Behaves like a Python set with changes automatically reflected in the
element's classList.
# Add and remove classes.
element.classes.add("active")
element.classes.remove("inactive")
element.classes.discard("maybe-missing") # No error if absent.
# Check membership.
if "active" in element.classes:
print("Element is active")
# Clear all classes.
element.classes.clear()
# Iterate over classes.
for cls in element.classes:
print(cls)
Source code in pyscript/web.py
__init__(element)
add(class_name)
remove(class_name)
discard(class_name)
Style
Bases: dict
Behaves like a Python dict with changes automatically reflected in the
element's style attribute.
# Set and get styles using CSS property names (hyphenated).
element.style["color"] = "red"
element.style["background-color"] = "#f0f0f0"
element.style["font-size"] = "16px"
# Get a style value.
color = element.style["color"]
# Remove a style.
del element.style["margin"]
# Check if a style is set.
if "color" in element.style:
print(f"Color is {element.style['color']}")
Source code in pyscript/web.py
__init__(element)
__setitem__(key, value)
HasOptions
Mixin for elements with options (datalist, optgroup, select).
Provides an options property that returns an Options instance. Used
in conjunction with the Options class.
# Get a select element and work with its options.
select = web.page["my-select"]
# Add options.
select.options.add(value="1", html="Option 1")
select.options.add(value="2", html="Option 2", selected=True)
# Get the selected option.
selected = select.options.selected
# Iterate over options.
for option in select.options:
print(f"{option.value}: {option.innerHTML}")
# Clear all options.
select.options.clear()
Source code in pyscript/web.py
options
property
Return this element's options as an Options instance.
Options
Interface to the options of a datalist, optgroup, or select element.
Supports adding, removing, and accessing option elements. Used in
conjunction with the HasOptions mixin.
# Add options to a select element.
select.options.add(value="1", html="Option 1")
select.options.add(value="2", html="Option 2", selected=True)
# Insert option at specific position.
select.options.add(value="1.5", html="Option 1.5", before=1)
# Access options by index.
first_option = select.options[0]
# Get the selected option.
selected = select.options.selected
print(f"Selected: {selected.value}")
# Iterate over all options.
for option in select.options:
print(option.innerHTML)
# Remove option by index.
select.options.remove(0)
# Clear all options.
select.options.clear()
Source code in pyscript/web.py
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |
options
property
Return the list of option elements.
selected
property
Return the currently selected option.
add(value=None, html=None, text=None, before=None, **kwargs)
Add a new option to the element.
Can specify value, html content, and text. The before parameter can
be an Element or index to insert before. The **kwargs are additional
arbitrary attributes for the new option element.
Source code in pyscript/web.py
clear()
Page
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.
from pyscript import web
# Access page structure.
web.page.html # The <html> element.
web.page.head # The <head> element.
web.page.body # The <body> element.
# Get and set page title.
web.page.title = "New Title"
print(web.page.title)
# Find elements by CSS selector.
divs = web.page.find("div")
items = web.page.find(".item-class")
# Look up element by id.
element = web.page["my-id"]
element = web.page["#my-id"] # The "#" prefix is optional.
# Append to the body (shortcut for page.body.append).
web.page.append(web.div("Hello"))
Source code in pyscript/web.py
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 | |
title
property
writable
Get the page title.
__getitem__(key)
Look up an element by id.
The '#' prefix is optional and will be stripped if present. Returns None if no element with that id exists.
Source code in pyscript/web.py
append(*items)
find(selector)
Find all elements matching the
CSS selector.
Returns an ElementCollection of matching elements.
page.find("div") # All divs on the page
page.find(".my-class") # All elements with class
page.find("#my-id") # Element with id (as collection)
page.find("div.my-class") # All divs with class
Source code in pyscript/web.py
canvas
Bases: ContainerElement
A bespoke HTML canvas element with Pythonic drawing and download capabilities.
Source code in pyscript/web.py
download(filename='snapped.png')
Download the canvas content as an image file.
Creates a temporary download link and triggers it.
Source code in pyscript/web.py
draw(what, width=None, height=None)
Draw a 2d image source (what) onto the canvas. Optionally scale to
width and height.
Accepts canvas image sources: HTMLImageElement, SVGImageElement,
HTMLVideoElement, HTMLCanvasElement, ImageBitmap,
OffscreenCanvas, or VideoFrame.
Source code in pyscript/web.py
video
Bases: ContainerElement
A bespoke HTML video element with Pythonic snapshot capability (to render an image to a canvas).
Source code in pyscript/web.py
snap(to=None, width=None, height=None)
Capture a video frame to a canvas element. Optionally scale to
width and height.
If no canvas is provided, this will create one. The to parameter can be a canvas Element, raw DOM canvas, or CSS selector string.