Asciigraf is a python library that turns ascii diagrams of networks into network objects. It returns a networkx graph of nodes for each alpha-numeric element in the input text; nodes are connected in the graph to match the edges represented in the diagram by -, /, \ and |.
Installation
Asciigraf can be installed from pypi using pip:
~/$ pip install asciigraf
Usage
Asciigraf expects a string containg a 2-d ascii diagram. Nodes can be an alphanumeric string composed of words, sentences and punctuation (for a look at what is all tested to work, see the node recognition tests). Edges can be composed of -, /, \ and |.
import asciigraf
network = asciigraf.graph_from_ascii("""
NodeA-----
|
|---NodeB
""")
print(network)
>>> <networkx.classes.graph.Graph at 0x7f24c3a8b470>
print(network.edges())
>>> [('NodeA', 'NodeB')]
print(network.nodes())
>>> ['NodeA', 'NodeB']
Networkx provides tools to attach data to graphs, nodes and edges, and asciigraf leverages these in a number of ways; in the example below you can see that asciigraf uses this to attach a x, y position tuple to each node indicating the line/col position of each node ( 0,0 is at the top-left). It also attaches a length attribute to each edge which matches the number of characters in that edge, as well as a list of positions for each character an edge. In addition, the input data is attached as a graph attribute ascii_string for reference.
print(network.nodes(data=True))
>>> [('NodeA', {'position': (10, 1)}), ('NodeB', {'position': (23, 3)})]
print(network.edges(data=True))
>>> [('NodeA', 'NodeB', OrderedDict([('length', 10), 'points', [...]))]
print(network.edge['NodeA']['NodeB']['points'])
>>> [(15, 1), (16, 1), (17, 1), (18, 1),
(19, 1), (19, 2), (19, 3), (20, 3), (21, 3), (22, 3)]
print(network.graph["ascii_string"])
>>>
NodeA-----
|
|---NodeB
Asciigraf also lets you annotate the edges of graphs using in-line labels — denoted by parentheses. The contents of the label will be attached to the edge on which it is drawn with the attribute name label.
network = asciigraf.graph_from_ascii("""
A---(nuts)----B----(string)---C
|
|
|
D---(pebbles)----E
""")
print(network.get_edge_data("A", "B")["label"])
>>> nuts
print(network.get_edge_data("B", "C")["label"])
>>> string
print(network.get_edge_data("D", "E")["label"])
>>> pebbles
print(hasattr(network.get_edge_data("B", "D"), "label"))
>>> False
Have fun!
import asciigraf
network = asciigraf.graph_from_ascii("""
s---p----1---nx
/ | |
/ | 0---f
6l-a c--
/ | \--k
/ ua | 9e
q \ | /
\-r7z jud
\ |
m y
\ |
v-ow
""")
Release files for asciigraf 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asciigraf-1.1.1.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asciigraf-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.9 kB
Release files / asciigraf-1.1.1.tar.gz
| Download URL | asciigraf-1.1.1.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
394b4227e539b9c1e965b9c4706e34ce87e06ed081b6a86622cb814a867b1bff
|
|
BLAKE2b-256 checksum How to use checksums |
ac35b8215b0b734b3a258d6d3c314389541219e98b3c52a19848ec3b78ad5faa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Release files / asciigraf-1.1.1-py3-none-any.whl
| Download URL | asciigraf-1.1.1-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88323a4a9e1efaf9b0a43877f9cb86b340b9ede58b2a7d058e6aa710675175cd
|
|
BLAKE2b-256 checksum How to use checksums |
a90f0699db8052bd23b4e8b5d0035babeead46c02b7a9938addeb80792f2d121
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|