Embed an SVG into the display. Note if you just want to view a svg image via a URL use `:class:Image` with a url=URL keyword argument.
| 481 | |
| 482 | |
| 483 | class SVG(DisplayObject): |
| 484 | """Embed an SVG into the display. |
| 485 | |
| 486 | Note if you just want to view a svg image via a URL use `:class:Image` with |
| 487 | a url=URL keyword argument. |
| 488 | """ |
| 489 | |
| 490 | _read_flags = 'rb' |
| 491 | # wrap data in a property, which extracts the <svg> tag, discarding |
| 492 | # document headers |
| 493 | _data: Optional[str] = None |
| 494 | |
| 495 | @property |
| 496 | def data(self): |
| 497 | return self._data |
| 498 | |
| 499 | @data.setter |
| 500 | def data(self, svg): |
| 501 | if svg is None: |
| 502 | self._data = None |
| 503 | return |
| 504 | # parse into dom object |
| 505 | from xml.dom import minidom |
| 506 | x = minidom.parseString(svg) |
| 507 | # get svg tag (should be 1) |
| 508 | found_svg = x.getElementsByTagName('svg') |
| 509 | if found_svg: |
| 510 | svg = found_svg[0].toxml() |
| 511 | else: |
| 512 | # fallback on the input, trust the user |
| 513 | # but this is probably an error. |
| 514 | pass |
| 515 | if isinstance(svg, bytes): |
| 516 | self._data = svg.decode(errors="replace") |
| 517 | else: |
| 518 | self._data = svg |
| 519 | |
| 520 | def _repr_svg_(self): |
| 521 | return self._data_and_metadata() |
| 522 | |
| 523 | class ProgressBar(DisplayObject): |
| 524 | """Progressbar supports displaying a progressbar like element |