├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── examples ├── circle.png ├── labeled-tree.png └── tree.png ├── pygraph ├── __init__.py └── cli.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python template 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | # Created by .ignore support plugin (hsz.mobi) 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Pedro Rodriguez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include examples 2 | include README.md 3 | include LICENSE 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pygraph 2 | pygraph is an extremely simple CLI tool for using graphviz. It takes a file to write to and 3 | adjacency list to construct the graph from. The intended use case is for generating 4 | small graphs to embed in things like homework problems. The API is documented via the `--help` 5 | option 6 | 7 | ## Examples 8 | ```bash 9 | pygraph -d tree ab ac bd be 10 | 11 | pygraph -u -e neato circle ab bc cd de ea 12 | 13 | pygraph -d -n "Sample Tree" labeled-tree root-left:1 root-right:1 left-child:4 right-child:3 root-root:0 14 | ``` 15 | 16 | ![tree](examples/tree.png) 17 | ![circle](examples/circle.png) 18 | ![labeled-tree](examples/labeled-tree.png) 19 | 20 | ## Usage 21 | ```bash 22 | $ pygraph --help 23 | Usage: pygraph [OPTIONS] FILE EDGES... 24 | 25 | Options: 26 | -e, --engine [dot|neato|twopi|circo|fdp|sfdp|patchword|osage] 27 | Choose layout engine to use 28 | -u, --undirected / -d, --directed 29 | Specify undirected or directed edges 30 | --format TEXT Image format 31 | -n, --name TEXT Name of graph in image 32 | --dot Preserve the source dot file 33 | --no-vertex-labels Don't label vertex labels 34 | --help Show this message and exit. 35 | ``` 36 | 37 | ### Installation 38 | ```bash 39 | pip install pygraph-cli 40 | ``` 41 | 42 | ### Syntax 43 | * First argument is file to save to without extension 44 | * Pairs of single characters after are interpreted as creating an edge between the them (eg: `ab ac cd`) 45 | * Nodes can have any name when separated by `-` (eg: `root-child`) 46 | * To label the edge, append `:` and any text (eg: `root-child:label`) 47 | * Name the image by passing `-n` option 48 | * To keep the original dot source, pass `--dot` 49 | -------------------------------------------------------------------------------- /examples/circle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EntilZha/pygraph/54186cd511d10ab51d4bee7abc5c72c6f15ffc94/examples/circle.png -------------------------------------------------------------------------------- /examples/labeled-tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EntilZha/pygraph/54186cd511d10ab51d4bee7abc5c72c6f15ffc94/examples/labeled-tree.png -------------------------------------------------------------------------------- /examples/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EntilZha/pygraph/54186cd511d10ab51d4bee7abc5c72c6f15ffc94/examples/tree.png -------------------------------------------------------------------------------- /pygraph/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EntilZha/pygraph/54186cd511d10ab51d4bee7abc5c72c6f15ffc94/pygraph/__init__.py -------------------------------------------------------------------------------- /pygraph/cli.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | from collections import namedtuple 3 | import click 4 | import graphviz 5 | from functional import seq 6 | 7 | ENGINES = ['dot', 'neato', 'twopi', 'circo', 'fdp', 'sfdp', 'patchword', 'osage'] 8 | Edge = namedtuple('Edge', 'left right label') 9 | 10 | 11 | def split_edge(edge): 12 | edge_label = None 13 | if ':' in edge: 14 | edge, edge_label = edge.split(':') 15 | if '-' in edge: 16 | left, right = edge.split('-') 17 | if right == '': 18 | right = None 19 | else: 20 | left, right = edge 21 | return Edge(left, right, edge_label) 22 | 23 | 24 | @click.command() 25 | @click.option('--engine', '-e', default='dot', type=click.Choice(ENGINES), 26 | help="Choose layout engine to use") 27 | @click.option('--undirected/--directed', '-u/-d', default=True, 28 | help="Specify undirected or directed edges") 29 | @click.option('--format', default='png', type=str, help='Image format') 30 | @click.option('--name', '-n', default=None, type=str, help='Name of graph in image') 31 | @click.option('--dot', is_flag=True, help='Preserve the source dot file') 32 | @click.option('--no-vertex-labels', is_flag=True, help="Don't label vertex labels") 33 | @click.argument('file', type=click.Path(writable=True)) 34 | @click.argument('edges', nargs=-1, required=True) 35 | def main(engine, undirected, format, name, dot, file, edges, no_vertex_labels): 36 | if undirected: 37 | graph = graphviz.Graph(engine=engine, format=format) 38 | else: 39 | graph = graphviz.Digraph(engine=engine, format=format) 40 | if name: 41 | graph.body.append(r'label = "{0}"'.format(name)) 42 | edges = seq(edges).map(split_edge) 43 | 44 | if no_vertex_labels: 45 | edges.map(lambda e: (e.left, e.right)).flatten().distinct()\ 46 | .filter_not(lambda n: n is None).for_each(lambda n: graph.node(n, label='')) 47 | else: 48 | edges.map(lambda e: (e.left, e.right)).flatten().distinct() \ 49 | .filter_not(lambda n: n is None).for_each(lambda n: graph.node(n)) 50 | 51 | edges.filter(lambda e: e.right is not None) \ 52 | .for_each(lambda e: graph.edge(e.left, e.right, label=e.label)) 53 | filepath, filename = path.split(file) 54 | filepath = filepath if filepath != '' else None 55 | graph.render(filename=filename, directory=filepath, cleanup=not dot) 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | [wheel] 4 | universal = 1 5 | 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name='pygraph-cli', 5 | description='CLI for pygraphviz powered by graphviz', 6 | url='https://github.com/EntilZha/pygraph', 7 | author='Pedro Rodriguez', 8 | author_email='ski.rodriguez@gmail.com', 9 | maintainer='Pedro Rodriguez', 10 | maintainer_email='ski.rodriguez@gmail.com', 11 | license='MIT', 12 | keywords='graph visualization graphviz python cli', 13 | packages=find_packages(), 14 | version='0.1.0', 15 | install_requires=['graphviz', 'click', 'pyfunctional'], 16 | entry_points={ 17 | 'console_scripts': ['pygraph = pygraph.cli:main'] 18 | }, 19 | classifiers=[ 20 | 'Development Status :: 4 - Beta', 21 | 'Intended Audience :: Developers', 22 | 'License :: OSI Approved :: MIT License', 23 | 'Programming Language :: Python :: 2.7', 24 | 'Programming Language :: Python :: Implementation :: CPython', 25 | 'Natural Language :: English', 26 | 'Operating System :: OS Independent', 27 | 'Topic :: Software Development :: Libraries :: Python Modules' 28 | ] 29 | ) 30 | 31 | --------------------------------------------------------------------------------