Home > OS >  Python Graphviz - Import SVG file inside a node
Python Graphviz - Import SVG file inside a node

Time:12-19

I need to import a SVG image using the python librairy of graphviz.

Here is the SVG file (created with the software draw.io):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Do not edit this file with editors other than diagrams.net -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="37" height="88" viewBox="-0.5 -0.5 37 88" content="&lt;mxfile host=&quot;Electron&quot; modified=&quot;2022-12-15T10:43:57.185Z&quot; agent=&quot;5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/19.0.3 Chrome/102.0.5005.63 Electron/19.0.3 Safari/537.36&quot; etag=&quot;3oZj78ENfpeNirtN8up0&quot; version=&quot;19.0.3&quot; type=&quot;device&quot;&gt;&lt;diagram id=&quot;9rhVbpvjZH1Aqyv7po2j&quot; name=&quot;Page-1&quot;&gt;jZNNT8MwDIZ/TY9IbbqNcRzdBkiAkCaE4Ja1XhuR1FXqbh2/npS6X5omIfXgPLYT 7XrhZGpH6wsshdMQHvCT2ovXHtCBDMhvObzk3NLlot5C1KrEg4awE79AEOfaaUSKCeBhKhJFVMYY55DTBMmrcXTNOyAevpqIVO4ALtY6kv6oRLKuAtxO/BHUGnWvRws7lqPkV0wd1JmMsHTCIUbL4wsIrWWqSPQjXidLm3e9oq3L8xCTv9JeP9UWzrIrXh9ouW8/MavFG74lqPUFTe8igktV0znTgZXfNGYldFtQHh/BEvKCfUs96DfsFSkMHcheyRCMwpYaZU2DsLC0YyMdofAmViRVjlE/eh8B7kglwv11U6DXj 3eIAGyJ5dCCfMfJacd27WLdNpmGDIKBsNb8FM8s6k/c2DrM5gZbvjMME/3 g/CDe/&lt;/diagram&gt;&lt;/mxfile&gt;"><defs><filter id="dropShadow"><feGaussianBlur in="SourceAlpha" stdDeviation="1.7" result="blur"/><feOffset in="blur" dx="3" dy="3" result="offsetBlur"/><feFlood flood-color="#3D4574" flood-opacity="0.4" result="offsetColor"/><feComposite in="offsetColor" in2="offsetBlur" operator="in" result="offsetBlur"/><feBlend in="SourceGraphic" in2="offsetBlur"/></filter></defs><g filter="url(#dropShadow)"><ellipse cx="15" cy="7.5" rx="7.5" ry="7.5" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><path d="M 15 15 L 15 40 M 15 20 L 0 20 M 15 20 L 30 20 M 15 40 L 0 60 M 15 40 L 30 60" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe flex-start; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 67px; margin-left: 15px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: nowrap;">Actor</div></div></div></foreignObject><text x="15" y="79" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="12px" text-anchor="middle">Actor</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

Here is the code I have tried to import this inside a node:

import os
import graphviz


os.environ["PATH"]  = os.pathsep   r'C:\Program Files\Graphviz\bin'

path_current = os.path.dirname(__file__)
path_graph = os.path.join(path_current, "build", "graph")
path_svg = os.path.join(path_current, "test.svg")

graph = graphviz.Digraph(
    'structs', 
    filename=path_graph, 
    format="svg", 
    graph_attr={"rankdir": "LR"},
    node_attr={'shape': 'record'}
)

graph.node("test node SVG", **{
    "image": path_svg,
})
graph.view()

The image is just not showing without any error.

CodePudding user response:

The problem is the following:

It is necessary to have an empty label for the node:

graph.node("test node SVG", **{
    "image": path_svg,
    "label": ""
})

The image will display without problem then

  • Related