An interactive step-by-step Python code execution visualizer
Project description
๐ Python Code Visualizer
An interactive step-by-step Python code execution visualizer
Features โข Installation โข Quick Start โข API โข Examples โข Contributing
โจ Overview
Python Code Visualizer is a powerful library that helps developers and educators understand Python code execution by generating interactive, step-by-step visualizations. Watch your code come alive as you trace through variables, function calls, and control flow.
๐ฏ Features
๐ Execution Tracing
|
๐ Visual Analytics
|
๐ก๏ธ Safety & Robustness
|
๐จ Rich UI
|
๐ฆ Installation
Prerequisites
- Python 3.8 or higher
- pip package manager
Install via pip
# Clone the repository
git clone https://github.com/jayachandranpm/python-code-visualizer.git
cd python-code-visualizer
# Install dependencies
pip install -r requirements.txt
Dependencies
| Package | Version | Purpose |
|---|---|---|
jinja2 |
โฅ3.0.0 | HTML template rendering |
๐ Quick Start
Basic Usage
from PythonVisualizer import CodeVisualizer
code = """
numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers]
total = sum(squares)
print(f"Sum of squares: {total}")
"""
# Create visualizer
viz = CodeVisualizer(code)
# Execute and capture trace
viz.execute()
# Generate HTML visualization
viz.render(output_file='visualization.html')
Then open visualization.html in your browser! ๐
๐ API Reference
CodeVisualizer
The main entry point for the library.
CodeVisualizer(
code: str, # Python code to visualize
inputs: list = None, # Mock inputs for input() calls
timeout: float = 10.0, # Execution timeout in seconds
max_steps: int = 10000 # Maximum trace steps
)
Methods
| Method | Description | Returns |
|---|---|---|
execute() |
Runs the code and captures trace | dict with steps, counts, limit_reached |
render(format='web', output_file='visualization.html') |
Generates visualization | File path or HTML string |
๐ก Examples
Example 1: Recursive Function
from PythonVisualizer import CodeVisualizer
code = """
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
result = factorial(5)
print(f"5! = {result}")
"""
viz = CodeVisualizer(code)
viz.execute()
viz.render(output_file='factorial.html')
What you'll see:
- ๐ข Green markers for each recursive call
- ๐ Orange markers for each return
- ๐ Deep call stack during recursion
Example 2: User Input Handling
from PythonVisualizer import CodeVisualizer
code = """
name = input("What's your name? ")
age = input("How old are you? ")
print(f"Hello {name}, you are {age} years old!")
"""
# Provide mock inputs
viz = CodeVisualizer(code, inputs=["Alice", "25"])
viz.execute()
viz.render(output_file='input_demo.html')
What you'll see:
- ๐ฌ Terminal shows the conversation
- ๐ Variables update with input values
Example 3: Exception Visualization
from PythonVisualizer import CodeVisualizer
code = """
def divide(a, b):
return a / b
result = divide(10, 0) # This will crash!
"""
viz = CodeVisualizer(code)
viz.execute()
viz.render(output_file='exception.html')
What you'll see:
- ๐ด Red highlighted crash line
- โ ๏ธ Error banner with exception details
- ๐ด Red marker on timeline
๐ฎ Keyboard Shortcuts
| Key | Action |
|---|---|
โ |
Step backward |
โ |
Step forward |
Space |
Play / Pause |
๐๏ธ Project Structure
PythonVisualizer/
โโโ __init__.py # Package exports
โโโ api.py # CodeVisualizer class
โโโ core/
โ โโโ __init__.py
โ โโโ parser.py # AST validation
โ โโโ tracer.py # sys.settrace logic
โ โโโ executor.py # Code execution engine
โโโ visualization/
โ โโโ __init__.py
โ โโโ web_renderer.py # HTML generation
โ โโโ templates/
โ โโโ view.html.j2 # Jinja2 template
โโโ utils/
โโโ __init__.py
โโโ serializer.py # Object serialization
๐ Security
Python Code Visualizer includes multiple security measures:
| Feature | Description |
|---|---|
| Timeout | Prevents infinite loops (configurable) |
| Step Limit | Prevents memory exhaustion |
| HTML Escaping | Prevents XSS attacks in output |
| Sandboxed Execution | Code runs with limited globals |
โ ๏ธ Warning: This library executes arbitrary Python code. Only visualize code you trust.
๐ค Contributing
Contributions are welcome! Please read our Contributing Guide first.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Highlight.js for syntax highlighting
- Jinja2 for templating
- VS Code for UI inspiration
Made with โค๏ธ by developers, for developers
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_code_visualizer-1.0.2.tar.gz.
File metadata
- Download URL: python_code_visualizer-1.0.2.tar.gz
- Upload date:
- Size: 4.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7a5a9661dd72e0a26f0ad44789edef0bde4953886f63ade83c8e265acfba796
|
|
| MD5 |
cd0f68cdb0203d04868d2bb8e03ef880
|
|
| BLAKE2b-256 |
8f9bf24964b1467644a2a4987830962f1327734922dafbdc58413129ea568d21
|
File details
Details for the file python_code_visualizer-1.0.2-py3-none-any.whl.
File metadata
- Download URL: python_code_visualizer-1.0.2-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b18d90c674955720c4a29a7957c2bfafa19c3085d76bd12a3df7a6c99133940a
|
|
| MD5 |
fd4d6a78b7356c3f104b1e9812e02fca
|
|
| BLAKE2b-256 |
6337649dd6d77814411ebaeb0f5155ccfa16e4cb53a06d95ff80a604b90c4703
|