Skip to content

SvgElement

The SvgElement class serves as the base for all SVG elements. It wraps an ElementTree element, provides a registry for specialized classes, and offers methods for managing attributes, children, and searching within the SVG tree.

pydreamplet.core.SVG

SvgElement (self, tag, **kwargs)

Initializes a new SVG element with the specified tag and attributes.

Parameters

  • tag (str): The SVG tag name.
  • **kwargs: Additional attributes for the element.
elem = SvgElement("rect", fill="red")
print(elem)  # Outputs the XML representation of the element.

register

SvgElement.register(tag: str, subclass: type) -> None

Registers a specialized subclass for a given SVG tag. This is needed only for registration the SVG element classes, created by the user. Probably you will not need to us it.

from_element

SvgElement.from_element(element: ET.Element)

Creates an instance from an ElementTree element, using the registered subclass if available.

import xml.etree.ElementTree as ET
elem = ET.Element("rect")
svg_elem = SvgElement.from_element(elem)

attrs

attrs(self, attributes: dict) -> SvgElement

Sets multiple attributes on the element.

drop_shadow = SvgElement("feDropShadow")
drop_shadow.attrs({
    "id": "shadow",
    "dx": "0.2",
    "dy": "0.4",
    "stdDeviation": "0.2",
})
print(drop_shadow)  # <feDropShadow xmlns="http://www.w3.org/2000/svg" id="shadow" dx="0.2" dy="0.4" stdDeviation="0.2" />

append

append(self, *children) -> SvgElement

Appends aone or more child elements to the current element. Returns self, allowing method chaining.

remove

remove(self, *children) -> SvgElement

Removes one or more child elements from the current element. If a child was wrapped, it removes its underlying element.

svg = SVG(800, 600, width="400px", height="300px")
g1 = G()
g2 = G()
svg.append(g1, g2)
g1.append(Circle())
g2.append(Rect())

print(svg)
# <svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px" viewBox="0 0 800 600"><g><circle /></g><g><rect /></g></svg>

svg.remove(g1)
print(svg)
# <svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px" viewBox="0 0 800 600"><g><rect /></g></svg>

to_string

to_string(self, pretty_print: bool = True) -> str

Returns the SVG element as a string. If pretty_print is set to True, the output is formatted with indentation for improved readability (using Python’s built-in ET.indent available from Python 3.9 onward).

find and find_all

find(self, tag: str, nested: bool = False, id: Optional[str] = None)
find_all(self, tag: str, nested: bool = False, class_name: Optional[str] = None)

Searches for child elements by tag. If nested is True, the search is recursive.

For find, if an id is provided, only the element with that matching id will be returned. For find_all, if a class_name is provided, only elements with that matching class attribute will be returned.

copy

copy(self) -> SvgElement

Creates and returns a deep copy of the current SVG element. The new instance is a complete duplicate with its own separate copy of the underlying ElementTree element (and its subtree). This ensures that subsequent modifications to the copy do not affect the original element.

For example:

original = SvgElement("rect", x=10, y=20, width=100, height=50)
duplicate = original.copy()
duplicate.x = 30  # This change will not affect the original element.

Using copy() is especially useful when you need to duplicate elements and then modify them independently in your SVG structure.