Using Linnet from Python

The linnet_py extension provides native Linnet graphs to Python 3.10 and newer. It is a real binding with runtime tests, but it is not currently published to PyPI.

Developer preview: build from source
There is no supported pip install linnet-py release yet. The similarly named linnet project on PyPI is unrelated. Use this page from a GammaLoop checkout until official linnet-py wheels are published.

Build the extension

Install a stable Rust toolchain and Python 3.10 or newer, then run:

git clone https://github.com/alphal00p/gammaloop.git
cd gammaloop
python3.10 -m venv .venv
. .venv/bin/activate
python -m pip install "maturin>=1.7,<2.0"
maturin develop --locked \
  --manifest-path crates/linnet-py/Cargo.toml \
  --features extension-module,abi3

The declared Python dependencies install typst 0.15.0 alongside the extension. Python rendering uses that in-process binding; a typst command in PATH is neither required nor invoked.

Parse and inspect a graph

Save this as linnet_quickstart.py:

import linnet_py as lp

codec = lp.DotCodec.topology()
graph = lp.Graph.from_dot(
    """
    digraph G {
      A;
      B;
      A -> B;
    }
    """,
    codec,
)

whole = graph.full_subgraph()
assert graph.n_nodes == 2
assert graph.n_edges == 1
assert len(graph.nodes_of(whole)) == 2
assert len(graph.edges_of(whole)) == 1

print(graph.to_dot())

This editable example loads automatically when it comes into view. It requires JavaScript and downloads Python and its dependencies on the first visit. You can also run the example above locally.

Run python linnet_quickstart.py. Success means the assertions pass and the graph is printed as DOT. Graph also exposes typed nodes, half-edges, subgraphs, cycles, oriented cuts, and traversal trees; use the Python reference for the exact current surface.

Continue with Rendering graphs from Python to explore a complete graph with custom drawing selectors. The live Python playground also lets you edit DOT and tune the layout while watching the solver run.

Use the Rust guide when you need Linnet’s complete crate API, or the Typst guide when the final result should be a drawing.