E2B Code Interpreter - Stateful code execution
Project description
Code interpreter extension for Python
The repository contains a template and modules for the code interpreter sandbox. It is based on the Jupyter server and implements the Jupyter Kernel messaging protocol. This allows for sharing context between code executions and improves support for plotting charts and other display-able data.
Key Features
- Stateful Execution: Unlike traditional sandboxes that treat each code execution independently, this package maintains context across executions.
- Displaying Graph & Data: Implements parts of the Jupyter Kernel messaging protocol, which support for interactive features like plotting charts, rendering DataFrames, etc.
Installation
pip install e2b-code-interpreter
Examples
Minimal example with the sharing context
from e2b_code_interpreter import CodeInterpreter
with CodeInterpreter() as sandbox:
sandbox.notebook.exec_cell("x = 1")
execution = sandbox.notebook.exec_cell("x+=1; x")
print(execution.text) # outputs 2
Get charts and any display-able data
import base64
import io
from matplotlib import image as mpimg, pyplot as plt
from e2b_code_interpreter import CodeInterpreter
code = """
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
"""
with CodeInterpreter() as sandbox:
# you can install dependencies in "jupyter notebook style"
sandbox.notebook.exec_cell("!pip install matplotlib")
# plot random graph
execution = sandbox.notebook.exec_cell(code)
# there's your image
image = execution.results[0].png
# example how to show the image / prove it works
i = base64.b64decode(image)
i = io.BytesIO(i)
i = mpimg.imread(i, format='PNG')
plt.imshow(i, interpolation='nearest')
plt.show()
Streaming code output
from e2b_code_interpreter import CodeInterpreter
code = """
import time
import pandas as pd
print("hello")
time.sleep(3)
data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
display(data.head(10))
time.sleep(3)
print("world")
"""
with CodeInterpreter() as sandbox:
sandbox.notebook.exec_cell(code, on_stdout=print, on_stderr=print, on_result=(lambda result: print(result.text)))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for e2b_code_interpreter-0.0.11b10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56f849bb44c028ebfc9886807b83e87bb1a5c407343678240e413e0dfe0d2443 |
|
MD5 | c842ce8dd361260637b4f706f47aae2e |
|
BLAKE2b-256 | 1ec1c19910ee5678bf9293c0cb457a624497cd126b55d6e897ec410039f31a1a |
Close
Hashes for e2b_code_interpreter-0.0.11b10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46a32693809b532b75e96b77fcd623a7c44fc32a3b5c13d8ffa0d169a914ce17 |
|
MD5 | 93dc5e891f034ffc4e6771c6d8ed2ee5 |
|
BLAKE2b-256 | 5e91009209901f059a817b0e8394581c850cb469cd59be5f98d0f3e999a081ad |