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 Charts & 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 Sandbox
with Sandbox() as sandbox:
sandbox.run_code()("x = 1")
execution = sandbox.run_code()("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 Sandbox
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 Sandbox() as sandbox:
# you can install dependencies in "jupyter notebook style"
sandbox.run_code("!pip install matplotlib")
# plot random chart
execution = sandbox.run_code(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 Sandbox
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 Sandbox() as sandbox:
sandbox.run_code(code, on_stdout=print, on_stderr=print, on_result=(lambda result: print(result.text)))
More resources
-
Check out the JavaScript/TypeScript and Python "Hello World" guides to learn how to use our SDK.
-
See E2B documentation to get started.
-
Visit our Cookbook to get inspired by examples with different LLMs and AI frameworks.
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
e2b_code_interpreter-1.0.0.tar.gz
(10.8 kB
view hashes)
Built Distribution
Close
Hashes for e2b_code_interpreter-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1922ff62e75caac1545b0f45f63f6cf57bfe7b74eb93c1bb31c01598e01a3f1 |
|
MD5 | e83ae1e68b01fda090228689c32a6164 |
|
BLAKE2b-256 | 36816476a29ba0655b5895c1c7b128317fac4e6ad8709416fd315c6e389e539d |
Close
Hashes for e2b_code_interpreter-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20a5f3bdd8a18d1a7eeee622be5357feb88ad40ca95fedc77b40b3ea341a443c |
|
MD5 | c184d000e3e18efe7d767bfa7fbcaf79 |
|
BLAKE2b-256 | ce8ee39d728250f15d66af8733d47ff4b1efbe5d620926e259dca37569224a1d |