Skip to content

[Feature] Add sandbox #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

[Feature] Add sandbox #702

wants to merge 4 commits into from

Conversation

Yunnglin
Copy link
Collaborator

@Yunnglin Yunnglin commented Jul 11, 2025

Start server first, you can run remote or local:

from evalscope.sandbox import DockerSandboxServer

server = DockerSandboxServer()
server.run()
  • Usage1:
from evalscope.sandbox import DockerSandboxClient

# Using the client as a context manager
with DockerSandboxClient("http://localhost:8000") as client:
    # Execute code that imports libraries
    setup_result = client.execute_code(dedent("""
    import pandas as pd
    import numpy as np
    
    # Create a sample dataframe
    data = {
        'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'Score': [85.5, 90.0, 78.5, 92.5]
    }
    
    df = pd.DataFrame(data)
    df.to_csv('/sandbox/data.csv', index=False)
    print("Created sample data")
    """))
    
    print(setup_result)
    
    # Read the generated CSV
    file_content = client.read_file("/sandbox/data.csv")
    print("CSV file contents:")
    print(file_content)
    
    # Analyze the data
    analysis_result = client.execute_code(dedent("""
    import pandas as pd
    import numpy as np
    
    # Read the data
    df = pd.read_csv('/sandbox/data.csv')
    
    # Perform some analysis
    print(f"Average age: {df['Age'].mean()}")
    print(f"Average score: {df['Score'].mean()}")
    print(f"Correlation between Age and Score: {df['Age'].corr(df['Score'])}")
    """))
    
    print("Analysis results:")
    print(analysis_result)
  • Usage 2:
from evalscope.sandbox import DockerSandboxClient, DockerContainerConfig

# Initialize client
client = DockerSandboxClient("http://localhost:8000")

# Create a container
container_id = client.create_container(
    DockerContainerConfig(
        memory_limit="2g",
    )
)

try:
    # Execute Python code
    result = client.execute_code(dedent("""
    import numpy as np
    import matplotlib.pyplot as plt
    import io
    import base64
    
    # Generate some data
    x = np.linspace(0, 10, 100)
    y = np.sin(x)
    
    # Create a plot
    plt.figure(figsize=(8, 4))
    plt.plot(x, y)
    plt.title('Sine Wave')
    plt.grid(True)
    
    # Save plot to a buffer
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    
    # Print the base64 encoded image
    print(base64.b64encode(buf.read()).decode('utf-8'))
    """))

    print(f"Execution output: {result['output']}")  # Show beginning of output

    # Execute a shell command
    cmd_result = client.execute_command(["ls", "-la", "/sandbox"])
    print(f"Command output: {cmd_result['output']}")
    
    # Write a file
    client.write_file("/sandbox/test.txt", "Hello from the sandbox!")
    
    # Read a file
    content = client.read_file("/sandbox/test.txt")
    print(f"File content: {content}")
    
finally:
    # Delete the container
    client.delete_container()

@Yunnglin Yunnglin marked this pull request as ready for review July 16, 2025 07:54
@Yunnglin Yunnglin marked this pull request as draft July 18, 2025 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant