Gunnar

nordisk/knut

.nornir/graph-to-python-for-idiots.md

<!-- nornir:class: idiot -->
# For idiots — "just get me the graph in Python"

You have knut. You want the graph in Python so you can run `networkx` on it. No
database, no fuss. There are two ways. Both end in the SAME `MultiDiGraph`.

## First, once

```bash
python3 -m venv .venv
.venv/bin/pip install -r demo/requirements.txt
```

## Way 1 — dump to files, then load (simplest)

```bash
cargo run -p knut-rel2graph-bench --release --bin rel2graph-export -- 1 target/rel2graph-export
.venv/bin/python demo/rel2graph_networkx_demo.py target/rel2graph-export
```

You should see a table of counts and, at the end:

```
ALL ASSERTIONS PASSED.
```

That means the Python graph matches knut's own answer key. Done.

## Way 2 — run a server, pull over the network

Terminal 1 (leave it running):

```bash
cargo run -p knut-flight --release --bin knut-flight-server -- --addr 0.0.0.0:50055 --scale 1
```

Terminal 2:

```bash
cargo run -p knut-rel2graph-bench --release --bin rel2graph-export -- 1 target/rel2graph-export
.venv/bin/python demo/flight_networkx_demo.py grpc://localhost:50055 target/rel2graph-export
```

Again you want `ALL ASSERTIONS PASSED` at the end. It also prints a list of
"flights" (datasets) the server offers.

## Way 3 — serve a graph from real files on disk (not the pretend generator)

Add `--parquet` when you export, then point the server at that folder:

```bash
cargo run -p knut-rel2graph-bench --release --bin rel2graph-export -- 1 ./graph-export --parquet
cargo run -p knut-flight --release --bin knut-flight-server -- --parquet ./graph-export
```

Now the server reads real `.parquet` files off disk and builds the graph from
THEM (that's how korp will serve a graph it pulled from the lakehouse). You get
the exact same graph as Way 2 — same 1106 nodes, 2955 edges.

## What is this graph?

A little pretend company: `Order`s, `Product`s, `Customer`s, `Employee`s, and so
on — 1106 things (nodes) and 2955 links (edges) at scale 1. Employees report to
other employees (`REPORTS_TO`), and the demo proves nobody reports to *themselves*
(that was a real bug once; it's fixed and stays fixed).

## It broke. Now what?

* **`ModuleNotFoundError: networkx`** — you skipped the `pip install` step, or you
  ran plain `python` instead of `.venv/bin/python`.
* **`export dir ... not found`** — run the `rel2graph-export` command first.
* **`Flight ... failed to connect`** — the server (Terminal 1) isn't running, or
  you used a different `--addr` than the `grpc://…` you passed the demo.
* **bigger graph** — change the `1` to `10` or `100` in BOTH the export/server
  command and re-run. The answer key recomputes, so the asserts still hold.

## Where the grown-up docs are

- [graph-to-python-guide.md](graph-to-python-guide.md) — the real how-to.
- [rel2graph-networkx-design.md](rel2graph-networkx-design.md) — Way 1 internals.
- [flight-graph-server-design.md](flight-graph-server-design.md) — Way 2 internals.