Skip to main content

A graph visualization tool

Project description

Treevizer

Visualize node data structures using Graphviz.

How it works

Treevizer iterates over your datastructure to generate a DOT file and runs Graphviz turn it into an image. The datastructure needs to be built using Nodes.

It also support recursive functions.

Examples

Linked list

Image of linked list

Balanced binary tree

Image of Balanced binary tree

Recursive Fibonacci

Image of recursive Fibonacci function

Prerequisites

You need to install Graphviz and make sure it is in $PATH.

Cygwin

Don't install Graphviz in Cygwin, do a windows installation. Otherwise there will be a problem with paths.

Installing

pip install treevizer

Usage

The following structures are supported:

Linked list (ll)

Structure type is called "ll". The config key is called "LinkedList".

Require a Node class with the attributes that fulfill the following class diagram.

Class diagram of Node class for linked list.

Balanced binary tree (bbt)

Structure type is called "bbt". The config key is called "BalancedBinaryTree".

Require a Node class with the attributes that fulfill the following class diagram.

Class diagram of Node class for Balance binary tree.

Recursion

The config key is called "Recursion".

Decorate your recursive function with recursion_viz. It is possible to decorate multiple functions. Each function is identified by the name of your function when creating the PNG.

Example

import treevizer

@treevizer.recursion_viz
def a_rec_func():
    a_rec_func()


@treevizer.recursion_viz
def another_rec_func():
    another_rec_func()

a_rec_func()
another_rec_func()

treevizer.recursion_to_png("a_rec_func", dot_path="recursion.dot", png_path="recursion.png")
treevizer.recursion_to_png("another_rec_func", dot_path="rec2.dot", png_path="rec2.png")

Functions

Structure to DOT file

import treevizer

treevizer.to_dot(root, structure_type="bbt", dot_path="tree.dot")
    """
    Generate DOT file from node structure.

    Parameters
    ----------
    root : Node
        Root node for datastructure
    structure_type : str
        Name of the type of datastructure (default is "bbt")
    dot_path : str
        Path to generated DOT file (default is tree.dot)
    """

Structure to PNG

This also creates a DOT file.

import treevizer

treevizer.to_png(root, structure_type="bbt", dot_path="tree.dot", png_path="tree.png")
    """
    Generate DOT file from node structure and use Graphviz to create image.

    Parameters
    ----------
    root : Node
        Root node for datastructure
    structure_type : str
        Name of the type of datastructure (default is "bbt")
    dot_path : str
        Path to generated DOT file (default is tree.dot)
    png_path : str
        Path to generated png file (default is tree.png)
    """

Recursion decorator

This also creates a DOT file.

import treevizer

@treevizer.recursion_viz
def a_recusive_function():
    a_recusive_function()


treevizer.recursion_to_png(function_name, dot_path="recursion.dot", png_path="recursion.png")
    """
    Generate DOT file of recursive function calls and use Graphviz to create image.

    Parameters
    ----------
    function_name : str
        Name of your decorated function.
    dot_path : str
        Path to generated DOT file (default is recursion.dot)
    png_path : str
        Path to generated png file (default is recursion.png)
    """

DOT file to PNG

import treevizer

treevizer.dot_to_png(dot_path="tree.dot", png_path="tree.png")
    """
    Use Graphviz to create image from a DOT file.

    Parameters
    ----------
    dot_path : str
        Path to your DOT file (default is tree.dot)
    png_path : str
        Path to generated png file (default is tree.png)
    """

Configure

Create .dot.json in root folder to change DOT configuration. Available options can be found in Graphviz documentation.

For example to change color of Nodes in image for balanced binary tree use the following.

# .dot.json
{
    "BalancedBinaryTree": {
        "node": {
            "fillcolor": "green"
        }
    }
}

To change size and shape of nodes and color of edges in Linked list use the following.

# .dot.json
{
    "LinkedList": {
        "node": {
            "shape": "square",
            "width": 1.5
        },
        "edge": {
            "color": "red"
        }
    }
}

To change color of the font on edge labels for recursion.

{
    "Recursion": {
        "edge": {
            "fontcolor": "black"
        }
    }
}

Known Errors/Warnings

Known Errors and Warnings

Links

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

treevizer-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file treevizer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: treevizer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for treevizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c7c72a38cdac29de70472c9c476b2ceb0b0648ec71661150343bc5d990830be
MD5 0e349f235831ec7a9e709c7e3edf094c
BLAKE2b-256 9674714068a2db43dd8088ed5facda94c1aa486e7a436ddc5f26f5d78bc76c24

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page