heaptree-logo
Heaptree

Quick Start

This code snippet briefly illustrates the code execution functionality of Heaptree.

chart.py
import os
 
import anthropic
from heaptree import Heaptree
 
client = anthropic.Anthropic()
 
system = "Act as a Python coding assistant within a Jupyter notebook environment. Your responses should consist solely of executable Python code—no explanations or comments. Do not use markdown formatting or backticks."
prompt = "Create a chart of the top companies by revenue from 2024 save the chart as a png file called 'chart.png' at the home directory (/home/ubuntu)"
 
response = client.messages.create(
    model="claude-3-7-sonnet-latest",
    max_tokens=1024,
    system=system,
    messages=[
        {"role": "user", "content": prompt}
    ],
)
 
code = response.content[0].text
 
ht_client = Heaptree(os.environ.get("HEAPTREE_API_KEY"))
 
node = ht_client.create_node(os="linux", num_nodes=1, node_type="t2.small", user="jordan")
 
ht_client.run_code(
    user="jordan",
    node_id=node.get("node_ids")[0],
    code=code,
    lang="py",
)
 
ht_client.download_files(
    user="jordan",
    node_id=node.get("node_ids")[0],
    from_path="/home/ubuntu/chart.png",
    to_path="/Users/jordanhilado/projects/heaptree/examples/generated/downloaded_chart.png"
)