Skip to content

# `` rdf2dot

A commandline tool for drawing RDF graphs in Graphviz DOT format

You can draw the graph of an RDF file directly:

    rdf2dot my_rdf_file.rdf | dot -Tpng | display

Functions:

- `main` –
- `rdf2dot` –

  Convert the RDF graph to DOT

Attributes:

- `EDGECOLOR` –
- `ISACOLOR` –
- `LABEL_PROPERTIES` –
- `NODECOLOR` –
- `XSDTERMS` –

## `` EDGECOLOR `module-attribute`

    EDGECOLOR = 'blue'

## `` ISACOLOR `module-attribute`

    ISACOLOR = 'black'

## `` LABEL_PROPERTIES `module-attribute`

    LABEL_PROPERTIES = [label, URIRef('http://purl.org/dc/elements/1.1/title'), URIRef('http://xmlns.com/foaf/0.1/name'), URIRef('http://www.w3.org/2006/vcard/ns#fn'), URIRef('http://www.w3.org/2006/vcard/ns#org')]

## `` NODECOLOR `module-attribute`

    NODECOLOR = 'black'

## `` XSDTERMS `module-attribute`

    XSDTERMS = [(XSD[x]) for x in ('anyURI', 'base64Binary', 'boolean', 'byte', 'date', 'dateTime', 'decimal', 'double', 'duration', 'float', 'gDay', 'gMonth', 'gMonthDay', 'gYear', 'gYearMonth', 'hexBinary', 'ID', 'IDREF', 'IDREFS', 'int', 'integer', 'language', 'long', 'Name', 'NCName', 'negativeInteger', 'NMTOKEN', 'NMTOKENS', 'nonNegativeInteger', 'nonPositiveInteger', 'normalizedString', 'positiveInteger', 'QName', 'short', 'string', 'time', 'token', 'unsignedByte', 'unsignedInt', 'unsignedLong', 'unsignedShort')]

## `` main

    main()

Source code in `rdflib/tools/rdf2dot.py`

    180
    181

    def main():
        rdflib.extras.cmdlineutils.main(rdf2dot, _help)

## `` rdf2dot

    rdf2dot(g: Graph, stream: TextIO, opts: Dict[str, Any] = {})

Convert the RDF graph to DOT writes the dot output to the stream

Source code in `rdflib/tools/rdf2dot.py`

     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165

    def rdf2dot(g: Graph, stream: TextIO, opts: Dict[str, Any] = {}):
        """
        Convert the RDF graph to DOT
        writes the dot output to the stream
        """

        fields = collections.defaultdict(set)
        nodes: Dict[Node, str] = {}

        def node(x: Node) -> str:
            if x not in nodes:
                nodes[x] = "node%d" % len(nodes)
            return nodes[x]

        def label(x: Node, g: Graph):
            for labelProp in LABEL_PROPERTIES:  # noqa: N806
                l_ = g.value(x, labelProp)
                if l_:
                    return l_
            try:
                # type error: Argument 1 to "compute_qname" of "NamespaceManager" has incompatible type "Node"; expected "str"
                return g.namespace_manager.compute_qname(x)[2]  # type: ignore[arg-type]
            except Exception:
                return x

        def formatliteral(l: Literal, g):  # noqa: E741
            v = html.escape(l)
            if l.datatype:
                return "&quot;%s&quot;^^%s" % (v, qname(l.datatype, g))
            elif l.language:
                return "&quot;%s&quot;@%s" % (v, l.language)
            return "&quot;%s&quot;" % v

        def qname(x: URIRef, g: Graph) -> str:
            try:
                q = g.compute_qname(x)
                return q[0] + ":" + q[2]
            except Exception:
                return x

        def color(p):
            return "BLACK"

        stream.write('digraph { \n node [ fontname="DejaVu Sans" ] ; \n')

        for s, p, o in g:
            sn = node(s)
            if p == rdflib.RDFS.label:
                continue
            if isinstance(o, (rdflib.URIRef, rdflib.BNode)):
                on = node(o)
                opstr = (
                    "\t%s -> %s [ color=%s, label=< <font point-size='10' "
                    + "color='#336633'>%s</font> > ] ;\n"
                )
                # type error: Argument 1 to "qname" has incompatible type "Node"; expected "URIRef"
                stream.write(opstr % (sn, on, color(p), qname(p, g)))  # type: ignore[arg-type]
            else:
                # type error: Argument 1 to "qname" has incompatible type "Node"; expected "URIRef"
                fields[sn].add((qname(p, g), formatliteral(o, g)))  # type: ignore[arg-type]

        for u, n in nodes.items():
            stream.write("# %s %s\n" % (u, n))
            f = [
                "<tr><td align='left'>%s</td><td align='left'>%s</td></tr>" % x
                for x in sorted(fields[n])
            ]
            opstr = (
                "%s [ shape=none, color=%s label=< <table color='#666666'"
                + " cellborder='0' cellspacing='0' border='1'><tr>"
                + "<td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>"
                + "<td href='%s' bgcolor='#eeeeee' colspan='2'>"
                + "<font point-size='10' color='#6666ff'>%s</font></td>"
                + "</tr>%s</table> > ] \n"
            )
            stream.write(
                opstr
                # type error: Value of type variable "AnyStr" of "escape" cannot be "Node"
                % (n, NODECOLOR, html.escape(label(u, g)), u, html.escape(u), "".join(f))  # type: ignore[type-var]
            )

        stream.write("}\n")