Skip to main content

Draws ASCII trees.

Project description

Occasionally, you want things to look slightly pretty in the terminal. If trees are involved, give asciitree a shot!

Sample output

Here’s a code sample:

class Node(object):
    def __init__(self, name, children):
        self.name = name
        self.children = children

    def __str__(self):
        return self.name

root = Node('root', [
    Node('sub1', []),
    Node('sub2', [
        Node('sub2sub1', [])
    ]),
    Node('sub3', [
        Node('sub3sub1', [
            Node('sub3sub1sub1', [])
        ]),
        Node('sub3sub2', [])
    ])
])

print draw_tree(root)

and its output:

root
  +--sub1
  +--sub2
  |  +--sub2sub1
  +--sub3
     +--sub3sub1
     |  +--sub3sub1sub1
     +--sub3sub2

API

The module consists of a single public function, with the following signature:

draw_tree(node, child_iter, text_str)

where node is the root of the tree to be drawn, child_iter is a function that when called with a node, returns an iterable over all its children and text_str turns a node into the text to be displayed in the tree.

The default implementations of these two arguments retrieve the children by accessing node.children and simply use str(node) to convert a node to a string.

The resulting tree is drawn into a buffer and returned as a string.

Project details


Download files

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

Source Distribution

asciitree-0.2.tar.gz (2.0 kB view details)

Uploaded Source

File details

Details for the file asciitree-0.2.tar.gz.

File metadata

  • Download URL: asciitree-0.2.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for asciitree-0.2.tar.gz
Algorithm Hash digest
SHA256 cd6be4b0e247d4eeb1fa474830c5e5f4a2acbe98f119a854c62be207da3cbabe
MD5 06f6765687935109d500dd1d67e91462
BLAKE2b-256 9679efb329ae905980e51cfd9eb98280d186a1b235a0d0a6410bb1e0f1582af8

See more details on using hashes here.

Supported by

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