3 Libraries to visualize the execution of your Python code in real-time | by Satyam Kumar | Towards Data Science

Python is comparatively slower than other languages because of its dynamic nature and versatility. Sometimes complex computations or algorithms take a lot of time to execute in Python. There arises a need to track the execution flow of the code and get a deep dive understanding of performance bottlenecks.
The logging module in Python can be used to track events of your code and can be used to identify the cause of the crashing of the code. Efficient use of loggers can also track the time complexity of the code snippets. Logging can be useful but it's too technical and require proper implementation. In this article, we will discuss 3 such open-source Python libraries that can help developers to visualize the execution of the program in just a few lines of code.
Pyheat is an open-source Python library to help developers to get a line-by-line time distribution of execution of the code. Instead of presenting in tabular format, Pyheat represents the time required to run each line of code with a heatmap.
Pyheat can be installed from PyPl using
pip install py-heat
and import it using from pyheat import PyHeat
Pyheat can be used to generate a heatmap of time numbers for each line of code for a Python module. Pass the path of the Python file as a parameter to the PyHeat function.
ph = PyHeat('merge_sort.py')ph.create_heatmap()ph.show_heatmap()
The above-mentioned code generates a heatmap denoting the time required (seconds) to run each line of the code in the merge_sort.py module.
From observing the above heatmap, while loop in line 17, is taking most of the time (7 seconds) to execute. The heatmap helps the developers to identify the code snippets that are taking most of the time to execute that may further be optimized.
Heartrate is an open-source Python library that offers real-time visualization of the execution of a Python program. It offers a line-by-line visualization of the execution with the number donating the number of times each line is been executed. It displays the execution of the code in a separate window in the browser.
Heartrate library can be installed from PyPl using:
pip install heartrate
After importing the package (import heartrate) , use heartrate.trace(browser=True) function that will open a browser window displaying the visualization of the file where trace() was called.
On executing the code, a window will pop up in your browser, if now you can follow http://localhost:9999 to observe the output visualization of the execution.
The number on the leftmost side of each line denotes the number of times a particular line is been executed. The bars show the lines that have been hit recently — longer bars mean more hits, lighter colors mean more recent. It also shows a live stack trace.
Snoop is another package that can be used as a debugging tool. Snoop helps you to identify the values of local variables and track the execution of the program. Snoop is mainly used as a debugging tool to figure out why your Python code isn’t doing what you think it should be doing.
All this is possible by just using a snoop decorator before the function you want to track.
Snoop library can be installed from PyPl using:
pip install snoop
import snoop 

@snoop
def factorial(x):
    if x == 1:
        return 1
    else:
        return (x * factorial(x-1))

num = 5
print("The factorial of "+str(num)+" is: "+str(factorial(num)))

23:38:22.55 >>> Call to factorial in File "<ipython-input-2-222c25d20b49>", line 2
23:38:22.55 ...... x = 5
23:38:22.55    2 | def factorial(x):
23:38:22.55    3 |     if x == 1:
23:38:22.55    6 |         return (x * factorial(x-1))
    23:38:22.55 >>> Call to factorial in File "<ipython-input-2-222c25d20b49>", line 2
    23:38:22.55 ...... x = 4
    23:38:22.55    2 | def factorial(x):
    23:38:22.56    3 |     if x == 1:
    23:38:22.56    6 |         return (x * factorial(x-1))
        23:38:22.56 >>> Call to factorial in File "<ipython-input-2-222c25d20b49>", line 2
        23:38:22.56 ...... x = 3
        23:38:22.56    2 | def factorial(x):
        23:38:22.56    3 |     if x == 1:
        23:38:22.56    6 |         return (x * factorial(x-1))
            23:38:22.56 >>> Call to factorial in File "<ipython-input-2-222c25d20b49>", line 2
            23:38:22.56 ...... x = 2
            23:38:22.56    2 | def factorial(x):
            23:38:22.56    3 |     if x == 1:
            23:38:22.56    6 |         return (x * factorial(x-1))
                23:38:22.56 >>> Call to factorial in File "<ipython-input-2-222c25d20b49>", line 2
                23:38:22.56 ...... x = 1
                23:38:22.56    2 | def factorial(x):
                23:38:22.56    3 |     if x == 1:
                23:38:22.56    4 |         return 1
                23:38:22.56 <<< Return value from factorial: 1
            23:38:22.56    6 |         return (x * factorial(x-1))
            23:38:22.56 <<< Return value from factorial: 2
        23:38:22.56    6 |         return (x * factorial(x-1))
        23:38:22.57 <<< Return value from factorial: 6
    23:38:22.57    6 |         return (x * factorial(x-1))
    23:38:22.57 <<< Return value from factorial: 24
23:38:22.57    6 |         return (x * factorial(x-1))
23:38:22.57 <<< Return value from factorial: 120

The factorial of 5 is: 120

 

In this article, we have converted 3 libraries that can be used to visualize the execution of the Python code. These libraries help the developers to dig deep into code, find and understand performance bottlenecks, just by adding a few lines of code.
[1] Pyheat GitHub: https://github.com/csurfer/pyheat
[2] Heartrate GitHub: https://github.com/alexmojaki/heartrate
[3] Snoop GitHub: https://github.com/alexmojaki/snoop
Thank You for Reading