Skip to content

Python and notebooks

Everything the commands do is also available from Python. In a Jupyter notebook, a map shows up as soon as you draw it:

import slitherflow

m = slitherflow.draw("my-pipeline_subway.yaml")
m                       # shows the map

draw() takes the path of a spec, or the spec as a dictionary, and the same palette and theme options as render:

m = slitherflow.draw("my-pipeline_subway.yaml", theme="dark", palette="tol-bright")
m.problems              # what the overlap check found, as a list of messages (empty when it's clean)
m.save("map.svg")       # .svg, .png or .pdf (PNG and PDF need rsvg-convert)

Editing in a notebook

Load the spec once, change it, and draw it again. draw() never changes the dictionary you give it, so you can re-run a cell as often as you like:

spec = slitherflow.load_spec("my-pipeline_subway.yaml")

spec["lines"]["qc"]["row"] = 1.5
spec["lines"]["peaks"]["stations"][0]["x"] = 1100
slitherflow.draw(spec)

When you're happy with it, copy the changes into the YAML file, or save the dictionary back with yaml.safe_dump. That drops the comments in the file, so copying by hand is usually better.

Tip

For editing the YAML file itself, slitherflow preview shows the map in your browser and redraws it every time you save.

What's available

Name What it does
draw(spec, palette=None, theme=None) Draws a spec (a path or a dictionary) and checks it for overlaps. Returns a Map
Map.svg, Map.problems, Map.save(path) The SVG text, the overlap messages, and a way to write .svg, .png or .pdf
load_spec(path) Reads a YAML spec into a dictionary
spec_from_rulegraph(dot_text, title=...) A draft spec, as YAML text, from snakemake --rulegraph output
main([...]) Runs a command, e.g. slitherflow.main(["render", "map.yaml", "--png"])