• Latest
  • Trending
3 Tools to Track and Visualize the Execution of Your Python Code

3 Tools to Track and Visualize the Execution of Your Python Code

January 10, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Friday, 1 May, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

3 Tools to Track and Visualize the Execution of Your Python Code

by ITECHNEWS
January 10, 2022
in Data Science, Leading Stories
0 0
0
3 Tools to Track and Visualize the Execution of Your Python Code

Motivation

Have you ever seen an error output like below:

2 divided by 1 is equal to 2.0.
Traceback (most recent call last):
  File "loguru_example.py", line 17, in <module>
    divide_numbers(num_list)
  File "loguru_example.py", line 11, in divide_numbers
    res = division(num1, num2)
  File "loguru_example.py", line 5, in division
    return num1/num2
ZeroDivisionError: division by zero

 

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

and wish the output can be a little bit easier to understand as shown here?

 


3 Tools to Track and Visualize the Execution of Your Python Code
Image by Author
 

 

You might also want to visualize which lines of code are being executed and how many times they are executed in real-time:

 


3 Tools to Track and Visualize the Execution of Your Python Code
GIF by Author
 

 

If so, this article will give you the tools to do exactly the above. Those 3 tools are:

  • Loguru — print better exceptions
  • snoop — print the lines of code being executed in a function
  • heartrate — visualize the execution of a Python program in real-time

And all it takes to use these tools is one line of code!

 

Loguru — Print Better Exceptions

Loguru is a library that aims to make logging in Python enjoyable. Loguru provides many interesting functionalities, but one functionality that I found to be the most helpful is the ability to catch unexpected errors and display which value of a variable causes your code to fail.

To install Loguru, type

pip install loguru

 

To understand how Loguru can be useful, imagine that you have 2 functions division and divide_numbersand the function divide_numbers is executed.

 

from itertools import combinations
def division(num1: int, num2: int):
return num1/num2
def divide_numbers(num_list: list):
“””Division of 2 numbers in the number list “””
for comb in combinations(num_list, 2):
num1, num2 = comb
res = division(num1, num2)
print(f”{num1} divided by {num2} is equal to {res}.”)
if __name__ ==‘__main__’:
num_list = [2, 1, 0]
divide_numbers(num_list)
view rawloguru_example.py hosted with ❤ by GitHub

 

Note that combinations([2,1,0], 2) returns [(2, 1), (2, 0), (1, 0)] . After running the code above, we get this error:

2 divided by 1 is equal to 2.0.
Traceback (most recent call last):
  File "loguru_example.py", line 17, in <module>
    divide_numbers(num_list)
  File "loguru_example.py", line 11, in divide_numbers
    res = division(num1, num2)
  File "loguru_example.py", line 5, in division
    return num1/num2
ZeroDivisionError: division by zero

 

From the output, we know that the line return num1/num2 is where the error occurs, but we don’t know which values of num1 and num2 cause the error. Luckily, this can be easily tracked by adding Loguru’s logger.catch decorator:

 

from loguru import logger
from itertools import combinations
def division(num1: int, num2: int):
return num1/num2
@logger.catch # Add this to track errors
def divide_numbers(num_list: list):
for comb in combinations(num_list, 2):
num1, num2 = comb
res = division(num1, num2)
print(f”{num1} divided by {num2} is equal to {res}.”)
if __name__ ==‘__main__’:
num_list = [2, 1, 0]
divide_numbers(num_list)
view rawloguru_example.py hosted with ❤ by GitHub

 

Output:

 


3 Tools to Track and Visualize the Execution of Your Python Code
Image by Author
 

 

By adding logger.catch, the exceptions are much easier to understand! It turns out that the error occurs when dividing 2 by 0.

 

snoop — Print the Lines of Code being Executed in a Function

What if there is no error in the code, but we want to figure out what is going on in the code? That is when snoop comes in handy.

snoop is a Python package that prints the lines of code being executed along with the values of each variable by adding only one decorator.

To install snoop, type:

pip install snoop

 

Let’s imagine we have a function calledfactorial that finds the factorial of an integer.

 

import snoop
def factorial(x: int):
if x == 1:
return 1
else:
return (x * factorial(x–1))
if __name__ == “__main__”:
num = 5
print(f”The factorial of {num} is {factorial(num)}“)
view rawsnoop_example.py hosted with ❤ by GitHub

 

Output:

The factorial of 5 is 120

 

To understand why the output of factorial(5) is 20 , we can add snoop decorator to the function factorial .

 

import snoop
@snoop
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x–1))
if __name__ == “__main__”:
num = 5
print(f”The factorial of {num} is {factorial(num)}“)
view rawsnoop_example.py hosted with ❤ by GitHub

 

Output:

 


3 Tools to Track and Visualize the Execution of Your Python Code
Image by Author
 

 

In the output above, we can view the values of the variables and which lines of code are executed. Now we can understand how recursion works much better!

 

heartrate — Visualize the Execution of a Python Program in Real-Time

If you want to visualize which lines are executed and how many times they are executed, try heartrate.

heartrate is also created by the creator of snoop. To install heartrate, type:

pip install heartrate

 

Now let’s add heartrate.trace(browser=True) to our previous code. This will open a browser window displaying the visualization of the file where trace() was called.

 

import heartrate
heartrate.trace(browser=True)
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x–1))
if __name__ == “__main__”:
num = 5
print(f”The factorial of {num} is {factorial(num)}“)
view rawheartrate_example.py hosted with ❤ by GitHub

 

A new browser should pop up when you run the code above. If not, go to http://localhost:9999. You should see the output like below:

 


3 Tools to Track and Visualize the Execution of Your Python Code
Image by Author
 

 

Cool! The bars show the lines that have been hit. The longer bars mean more hits, lighter colors mean more recent.

From the output above, we can see that the program executes:

  • if x==1 5 times
  • return 1 once
  • return (x * factorial(x-1)) 4 times

The output makes sense since the initial value of x is 5 and the function is called repetitively until x equals to 1 .

Now let’s see what it is like to visualize the execution of a Python program in real-time using heartrate. Let’s add sleep(0.5) so that the program runs a little bit slower and increase num to 20 .

 

import heartrate
from time import sleep
heartrate.trace(browser=True)
def factorial(x):
if x == 1:
return 1
else:
sleep(0.5)
return (x * factorial(x–1))
if __name__ == “__main__”:
num = 20
print(f”The factorial of {num} is {factorial(num)}“)
view rawheartrate_example.py hosted with ❤ by GitHub

 

 


3 Tools to Track and Visualize the Execution of Your Python Code
GIF by Author
 

 

Awesome! We can see which lines of code are being executed and how many times each of them has been executed in real-time.

 

Conclusion

Congratulations! You have just learned 3 tools to track and visualize the execution of your Python code.

Tags: Python
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version