Skip to content

Feature: Render tree to a string #9

Open

Description

@zachriggle

Currently, it seems there is no way to "capture" the output of the rendered tree, to make it available for later use (as opposed to immediately printing it).

My current workaround for this involves hooking sys.stdout, which isn't ideal but works:

    try:
        buf = io.StringIO()
        stdout = sys.stdout
        sys.stdout = buf
        pptree.print_tree(tree)
    finally:
        sys.stdout = stdout

    buf.seek(0)
    render = buf.read()

It would be nice if there was a version that just returned the string. You can then refactor the code of print_tree to just print the output of this operation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions