railroad-diagrams

Railroad-Diagram Generator, Python Version

This is a small library for generating railroad diagrams (like what JSON.org uses) using SVG or Unicode/ASCII text, with both JS and Python ports. Here’s an online dingus for you to play with and get SVG code from!

(This is the README for the Python port; see the main README for other ports, and for more non-Python-specific information.)

Installation

Requires Python 3.12+.

pip install railroad-diagrams

Diagrams

Constructing a diagram is a set of nested calls. To create an SVG diagram:

import sys
from railroad import Diagram, Choice

d = Diagram("foo", Choice(0, "bar", "baz"))
# Stream directly to a file/writer:
d.writeSvg(sys.stdout.write)
# Or get a string directly:
svg = d.toSvg()  # or str(d)

To create a text-format diagram:

import sys
from railroad import Diagram, Choice

d = Diagram("foo", Choice(0, "bar", "baz"))
d.writeText(sys.stdout.write)
# Or get a string directly:
text = d.toText()

A railroad diagram must be started as a Diagram (or ComplexDiagram) object, which takes a list of diagram items, defined below.

The Diagram() constructor also optionally takes a type keyword argument:

After constructing a Diagram, you can call .format(*paddings) on it, specifying 0-4 padding values (just like CSS) for some additional “breathing space” around the diagram (the paddings default to 20px).

Output methods on Diagram (and .writeText() / .toText() / .toTextDiagram() on any DiagramItem):

CLI

You can also render diagrams from the command line via railroad-diagrams (or python -m railroad), accepting Python expressions (default), Bikeshed’s indentation DSL (-s dsl), KDL 2.0 (-s kdl), or auto-detected DSL/KDL (-s auto):

railroad-diagrams svg -e 'Diagram("foo", Choice(0, "bar", "baz"))'
railroad-diagrams unicode -s dsl -e 'Choice: 0
	T: bar
	T: baz'
railroad-diagrams svg -s kdl -e 'choice 0 { t bar; t baz }'

Parsing Text Syntaxes (DSL & KDL 2.0)

In addition to constructing diagrams programmatically, you can parse either Bikeshed’s indentation DSL or KDL 2.0. See the Input Syntaxes section of the main README for the full grammar, supported commands/nodes, properties, and auto-detection rules.

import railroad

# Auto-detect Bikeshed DSL vs KDL 2.0:
d1 = railroad.parse("""
Stack:
	Or: 1
		T: --
		Seq:
			Opt: skip
				T: -
			N: ident
""")

d2 = railroad.parse("""
stack {
	choice 1 {
		t "--"
		seq {
			opt skip { t "-" }
			n ident
		}
	}
}
""")

Available helpers:

Components

Components are either leaves (containing only text or similar) or containers (containing other components).

The leaves:

The containers:

Options

There are a few options you can tweak, living as UPPERCASE_CONSTANTS at the top of the module (and mirrored on railroad.Options to match the JS API); these can be adjusted via either railroad.OPTION_NAME_HERE = ... or railroad.Options.OPTION_NAME_HERE = .... Note that if you change the text sizes in the CSS, you’ll have to adjust the text metrics here as well.