What it does

The Execute Python tool runs Python code in a secure Jupyter notebook environment. Perfect for data analysis, calculations, visualizations, and any computational tasks your agents need to perform.

Key features

  • Execute Python code in isolated Jupyter notebook cells
  • Support for popular libraries (pandas, numpy, matplotlib, etc.)
  • Rich output formats including text, images, charts, and HTML
  • Automatic error handling with detailed tracebacks
  • Persistent session state across multiple executions

Parameters

ParameterTypeRequiredDescription
codestringYesThe Python code to execute in a single cell

Common use cases

Data analysis

import pandas as pd
import numpy as np

# Create sample data
data = {'sales': [100, 150, 200, 175], 'month': ['Jan', 'Feb', 'Mar', 'Apr']}
df = pd.DataFrame(data)
print(df.describe())

Analyze datasets and generate statistics.

Create visualizations

import matplotlib.pyplot as plt

# Create a simple chart
plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.title('Sample Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

Generate charts and graphs for data visualization.

Mathematical calculations

import math

# Complex calculations
result = math.sqrt(144) + math.pow(2, 8)
print(f"Result: {result}")

# Statistical analysis
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
mean = sum(numbers) / len(numbers)
print(f"Mean: {mean}")

Perform mathematical operations and statistical analysis.

File processing

import json

# Process JSON data
data = {"name": "John", "age": 30, "city": "New York"}
json_string = json.dumps(data, indent=2)
print(json_string)

Process and manipulate various file formats.

Web scraping and APIs

import requests

# Make API calls (if requests is available)
response = requests.get('https://api.github.com/users/octocat')
if response.status_code == 200:
    user_data = response.json()
    print(f"User: {user_data['name']}")

Fetch and process data from web APIs.

Available libraries

Common Python libraries are pre-installed including:

  • Data Science: pandas, numpy, scipy, scikit-learn
  • Visualization: matplotlib, seaborn, plotly
  • Web: requests, beautifulsoup4
  • Utilities: json, csv, datetime, os, sys

Output formats

The tool supports rich output including:

  • Text: Standard print output and string representations
  • Images: PNG, JPEG, SVG graphics from matplotlib, plotly, etc.
  • HTML: Rich HTML content and tables
  • Charts: Interactive visualizations
  • Markdown: Formatted text with markdown syntax

Best practices

  • Write clear, well-commented code
  • Handle errors gracefully with try/except blocks
  • Use print statements to show intermediate results
  • Break complex operations into smaller steps
  • Import libraries at the beginning of your code

Troubleshooting

“Module not found” errors

  • Check if the library is in the available libraries list
  • Try importing alternative libraries with similar functionality
  • Use built-in Python modules when possible

“Code execution timeout”

  • Simplify complex operations
  • Avoid infinite loops or very long-running processes
  • Break large datasets into smaller chunks

“Memory errors”

  • Reduce dataset size or use sampling
  • Clear variables you no longer need with del variable
  • Use more memory-efficient data structures

“Syntax errors”

  • Check Python syntax carefully
  • Ensure proper indentation
  • Verify parentheses and brackets are balanced