graflo.plot.render¶
Turning a networkx graph into a file, and the Graphviz details that needs.
Kept apart from the graphs themselves so every plot in this package builds a
plain :mod:networkx graph — which any test can assert against — and only this
module renders. Nothing here is specific to what is being drawn.
Rendering needs no system packages: Graphviz runs as WebAssembly
(wasi-graphviz on wasmtime) and writes SVG; PDF is converted from the
SVG (svglib + reportlab) and PNG rasterised from the PDF
(pypdfium2). PDF and PNG text uses the Liberation fonts shipped in
graflo/plot/fonts, which are metric-compatible with the Helvetica, Times
and Courier metrics Graphviz sizes nodes by, so text fits the boxes drawn for it.
Graphviz's layout recurses as deep as the graph, and the WebAssembly stack is
bounded, so a large enough graph cannot be laid out here: :func:draw raises a
:class:RuntimeError saying so, and the DOT source (format dot) can still
be written and laid out with a native Graphviz.
Attributes¶
DEFAULT_DPI = 150
module-attribute
¶
OUTPUT_FORMATS = ('svg', 'pdf', 'png', 'dot')
module-attribute
¶
__all__ = ['DEFAULT_DPI', 'OUTPUT_FORMATS', 'StackExhausted', 'draw', 'header_band', 'pdf_to_png', 'record_escape', 'render_svg', 'resolve_format', 'sanitize_id', 'svg_to_pdf', 'to_dot']
module-attribute
¶
Classes¶
StackExhausted
¶
Functions:¶
draw(dot, path, *, output_format=None, prog='dot', dpi=None)
¶
Write dot to path, creating the directory if it is missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dot
|
DotGraph
|
The graph to write. |
required |
path
|
str | PathLike[str]
|
Where to write it; its suffix chooses the format by default. |
required |
output_format
|
str | None
|
Override the format the suffix implies. |
None
|
prog
|
str
|
Graphviz layout program. |
'dot'
|
dpi
|
int | None
|
Raster resolution, for |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The path written. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Rendering is unavailable or failed; the message says why.
:class: |
ValueError
|
The format is not one this can write. |
Source code in graflo/plot/render.py
header_band(colour, *, header_lines, rows, fontsize)
¶
Fill attributes that colour only a record's first field, the rest white.
A record cannot fill one field, but a two-stop gradient whose weight is
the header's share of the node height draws the same picture. Graphviz
sizes a field as lines * fontsize * 1.2 plus 8 points of padding, so
the share is exact for single-line rows stacked under rankdir=LR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colour
|
str
|
The header colour. |
required |
header_lines
|
int
|
Text lines in the header field. |
required |
rows
|
int
|
Single-line fields below the header. |
required |
fontsize
|
float
|
The node's font size. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
|
Source code in graflo/plot/render.py
pdf_to_png(pdf, *, dpi=DEFAULT_DPI)
¶
The first page of pdf rasterised at dpi on a white background.
Source code in graflo/plot/render.py
record_escape(text)
¶
text as a record-label field: { } | < >, \ and newlines escaped.
render_svg(source, *, prog='dot')
¶
Lay out and draw DOT source as SVG.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
The |
Source code in graflo/plot/render.py
resolve_format(path, output_format)
¶
The format to write, from output_format or the path's own suffix.
Source code in graflo/plot/render.py
sanitize_id(node_id, taken=None)
¶
A Graphviz-safe node id, one-to-one with node_id.
: separates a node from its port and . a record field, so an id
like left:Firm.firm_id cannot be written as it stands. taken carries
the mapping across a whole graph so two different ids that sanitize alike
are still told apart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The id to convert. |
required |
taken
|
dict[str, str] | None
|
Accumulating |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The safe id, stable for the same node_id within one taken. |
Source code in graflo/plot/render.py
svg_to_pdf(svg)
¶
svg as a one-page PDF whose text uses the shipped Liberation fonts.