Skip to main content

Recursion Visualiser

PyPI downloads Stars Forks

Recursion visualiser is a python tool that visualizes recursion tree with animation and draws recursion tree for recursive function. It works with almost any type of recursive function. Just add the recursion-visualiser decorator to your function and let it do the rest of the work.

Installation

1. Installing graphviz

Windows

The only dependency for recursion visualiser is Graphviz

  • Download graphviz binary
  • Add graphviz bin to path manually or by adding the following line on your script. Change the installation directory according to your installation path
# Set it to bin folder of graphviz  
os.environ["PATH"] += os.pathsep +  'C:/Program Files (x86)/Graphviz2.38/bin/'  

Ubuntu

  • Install graphviz
 sudo apt install graphviz

The instructions to install graphviz for other operating system is available here

2. Installing recursion-visualiser

The easiest way to install recursion-visualiser package is from pypi

pip install recursion-visualiser

An alternative way is to clone the repository and install all the requirements.

pip install -r requirements.txt

Alternative Installation using Docker

If you have docker and docker-compose installed then you can install recursion-tree-visualiser using Docker and docker-compose.yml file

  1. Download Docker file from repo
curl https://raw.githubusercontent.com/Bishalsarang/Recursion-Tree-Visualizer/master/Dockerfile --output Dockerfile
  1. Download docker-compose.yml
curl https://raw.githubusercontent.com/Bishalsarang/Recursion-Tree-Visualizer/master/docker-compose.yml --output docker-compose.yml
  1. Start docker container
CURRENT_UID=$(id -u):$(id -g) docker-compose up
  1. Run any python scripts and run using
docker-compose exec vs python fibonacci.py

Usage

The preferred way to import the decorator class from the package is as:

from visualiser.visualiser import Visualiser as vs

1. Fibonacci

Let's draw the recursion tree for fibonacci number.
Here is how the simple code looks like

def fib(n):  
    if n <= 1: 
        return n 
    return fib(n - 1) + fib(n - 2)  

print(fib(6))  

Now we want to draw the recursion tree for this function. It is as simple as adding a decorator

# Author: Bishal Sarang

# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add decorator
# Decorator accepts optional arguments: ignore_args , show_argument_name, show_return_value and node_properties_kwargs
@vs(node_properties_kwargs={"shape":"record", "color":"#f57542", "style":"filled", "fillcolor":"grey"})
def fib(n):
    if n <= 1:
        return n
    return fib(n=n - 1) + fib(n=n - 2)


def main():
    # Call function
    print(fib(n=6))
    # Save recursion tree to a file
    vs.make_animation("fibonacci.gif", delay=2)


if __name__ == "__main__":
    main()

Here are the changes required:

  • Add decorator Visualiser which accepts optional arguments ignore_args, show_argument_name and 'show_return_value'
  • Change every function calls to pass as keyword arguments.
  • Make_animation

The output image are saved as "fibonacci.gif" and "fibonacci.png"

Here is how the recursion tree looks like:
Animation: enter image description here

enter image description here

Support:

Find other examples here and read more about recursion-visualiser here

TODO:

  • Minimal working version
  • Upload package to pypi
  • Support animation
  • Add node styles
  • Support aliasing for function name
  • Show repeated states
  • Support node_color, backgroundcolor etc
  • Refactor
  • Handle base cases
  • Make more beautiful trees

Metadata

Release files for recursion-visualiser 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for recursion-visualiser 1.0.3
File Size Uploaded Provenance Actions
recursion-visualiser-1.0.3.tar.gz 6.7 kB Without provenance Details for recursion-visualiser-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for recursion-visualiser 1.0.3
File Interpreter ABI Platform Actions
recursion_visualiser-1.0.3-py3-none-any.whl Python 3 none any Details for recursion_visualiser-1.0.3-py3-none-any.whl

Total release size: 13.8 kB

Release files / recursion-visualiser-1.0.3.tar.gz

Download URL recursion-visualiser-1.0.3.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
89eaff79d1ef2547b2ad7749f18377819164ebd18ab9fde5ba14dd6abcbc5b44
BLAKE2b-256 checksum
How to use checksums
4b9c989e10a6dd02b68947c3e2c21f12c0b54c4f801b05b7317680e3b3a56156
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / recursion_visualiser-1.0.3-py3-none-any.whl

Download URL recursion_visualiser-1.0.3-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1aeb190b80452e2b1cb6fa82d11810d3e18a836d1fa3ff057e6c74355356961
BLAKE2b-256 checksum
How to use checksums
5e3683d51f6a2bd41672f3c6b0fffa8aa891e9f6042f96f091dec71f6f64d6f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Security for recursion-visualiser 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page