Skip to main content
Execute Python code with access to pre-installed data science and machine learning libraries.
from gravixlayer import GravixLayer

client = GravixLayer(api_key="YOUR_API_KEY")

# Create a sandbox
sandbox = client.sandbox.sandboxes.create(template="python-base-v1", timeout=300)
sid = sandbox.sandbox_id

# Simple expression
result = client.sandbox.sandboxes.run_code(sid, code="print('Hello World!')")
print(result.logs)

# Multi-line script with imports
code = """\
import math
x = math.sqrt(25)
print(f'Square root of 25 = {x}')
numbers = [1, 2, 3, 4, 5]
print(f'Sum = {sum(numbers)}')
"""
result = client.sandbox.sandboxes.run_code(sid, code=code, language="python")
print(result.logs)

# Clean up
client.sandbox.sandboxes.kill(sid)

Request Parameters

ParameterTypeRequiredDescription
codestringYesPython code to execute
languagestringNoSet to “python” (default)
timeoutintegerNoTimeout in seconds (default: 30)

Response

{
  "execution_id": "exec_abc123",
  "results": {
    "stdout": [
      {
        "type": "stdout",
        "line": "   name  age",
        "timestamp": "2025-01-27T10:30:00Z"
      },
      {
        "type": "stdout",
        "line": "0  Alice   25",
        "timestamp": "2025-01-27T10:30:00Z"
      }
    ]
  },
  "error": null,
  "logs": {
    "stdout": ["   name  age", "0  Alice   25", "1    Bob   30"],
    "stderr": []
  }
}

Available Libraries

  • Data Science: NumPy, Pandas, Matplotlib, Seaborn
  • Machine Learning: Scikit-learn, TensorFlow, PyTorch
  • Web: Requests, BeautifulSoup4, FastAPI, Flask
  • Development: Jupyter, pytest, black

Example: Data Analysis

from gravixlayer import GravixLayer

client = GravixLayer(api_key="YOUR_API_KEY")
sandbox = client.sandbox.sandboxes.create(template="python-base-v1", timeout=300)
sid = sandbox.sandbox_id

code = """\
import numpy as np
import matplotlib.pyplot as plt

# Generate data
data = np.random.normal(0, 1, 1000)

# Calculate statistics
print(f"Mean: {np.mean(data):.3f}")
print(f"Std: {np.std(data):.3f}")

# Save plot
plt.hist(data, bins=30)
plt.savefig("/home/user/histogram.png")
print("Plot saved to histogram.png")
"""

result = client.sandbox.sandboxes.run_code(sid, code=code, language="python")
print(result.logs)

client.sandbox.sandboxes.kill(sid)

Next Steps

Run JavaScript Code

Execute Node.js code with npm packages

Run Commands

Execute shell commands and install packages

Read Files

Read files from the sandbox filesystem

Create Sandbox

Create a new sandbox for code execution