const result = await sandbox.runCode(
`
import matplotlib.pyplot as plt
import pandas as pd
# Create DataFrame
df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
print(df)
# Create plot
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('/workspace/plot.png')
`,
{ rich: true }
);
// Access rich outputs
console.log(`Captured ${result.rich_outputs?.length || 0} rich outputs`);
for (const output of result.rich_outputs || []) {
console.log(`Type: ${output.type}`);
console.log(`Path: ${output.path}`);
if (output.type === 'image') {
// Download image
const imgData = await sandbox.files.readBytes(output.path);
await fs.promises.writeFile('plot.png', imgData);
}
}