SVG
The SVG
class represents the root SVG element. It manages the viewbox, provides properties for dimensions, and includes methods for displaying and saving the SVG.
Info
This class inherits from SvgElement
.
pydreamplet.core.SVG
Initializes a new SVG element with the specified viewbox. The viewbox can be provided as a tuple of 2 (width, height) or 4 (min-x, min-y, width, height) numbers.
Parameters
viewbox
(tuple or list): Dimensions for the SVG.**kwargs
: Additional attributes for the SVG element.
svg = SVG(800, 600)
print(svg) # <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 600" width="800px" height="600px" />
You can change the default values of width and height passing **kwargs
.
svg = SVG(800, 600, width="400px", height="300px")
print(svg) # <svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px" viewBox="0 0 800 600" />
It does not need to be done during initialization. You can set any attribute of SvgElement using **kwargs
.
from_element
Creates an SVG instance from an ElementTree element.
from_file
from importlib.resources import files
from pydreamplet import SVG, resources
svg = SVG.from_file(files(resources) / "hummingbird.svg").attrs(
{"width": 96, "height": 84}
)
svg.find("path").fill = "darkgreen"
Creates an SVG instance by parsing an SVG file.
w
and h
Getter: Returns the width (w) and height (h) of the SVG based on its viewBox
.
Warning
Remember, based on viewBox
. Do not confuse these properties with width
and height
attributes of the SVG element.
import pydreamplet as dp
svg = dp.SVG(300, 300)
svg.width = "600px"
svg.height = "600px"
print(f"svg viewBox is {svg.viewBox}") # Outputs svg viewBox is 0 0 300 300
print(f"svg.w is {svg.w}, svg.h is {svg.h}") # Outputs svg.w is 300, svg.h is 300
print(f"svg.width is {svg.width}, svg.height is {svg.height}") # Outputs svg.width is 600px, svg.height is 600px
style
Adds a <style>
element to the SVG using CSS content loaded from an external file. When overwrite
is set to True
, any existing <style>
elements are removed and the new one is inserted as the first child. When minify
is True
, the CSS content is minified before insertion.
display
Displays the SVG in an IPython environment.
save
Saves the SVG to a file.