| title | Coding Environment Server | |
|---|---|---|
| emoji | 💻 | |
| colorFrom | blue | |
| colorTo | blue | |
| sdk | docker | |
| pinned | false | |
| app_port | 8000 | |
| base_path | /web | |
| tags |
|
A Python code execution environment that runs arbitrary Python code and returns results. Perfect for testing code execution infrastructure and demonstrating environment usage patterns.
The simplest way to use the Coding environment is through the CodingEnv class:
from envs.coding_env import CodeAction, CodingEnv
try:
# Create environment from Docker image
coding_env = CodingEnv.from_docker_image("coding-env:latest")
# Reset
result = coding_env.reset()
print(f"Reset complete: exit_code={result.observation.exit_code}")
# Execute Python code
code_samples = [
"print('Hello, World!')",
"x = 5 + 3\nprint(f'Result: {x}')",
"import math\nprint(math.pi)"
]
for code in code_samples:
result = coding_env.step(CodeAction(code=code))
print(f"Code: {code}")
print(f" → stdout: {result.observation.stdout.strip()}")
print(f" → exit_code: {result.observation.exit_code}")
finally:
# Always clean up
coding_env.close()That's it! The CodingEnv.from_docker_image() method handles:
- Starting the Docker container
- Waiting for the server to be ready
- Connecting to the environment
- Container cleanup when you call
close()
Before using the environment, you need to build the Docker image:
# From project root
docker build -t coding-env:latest -f src/envs/coding_env/server/Dockerfile .CodeAction: Contains a single field
code(str) - The Python code to execute
CodeObservation: Contains the execution results
stdout(str) - Standard output from code executionstderr(str) - Standard error from code executionexit_code(int) - Exit code (0 for success, non-zero for errors)
CodeState: Tracks execution state
episode_id(str) - Unique identifier for the episodestep_count(int) - Number of steps takenlast_exit_code(int) - Exit code from the last execution
If you already have a Coding environment server running, you can connect directly:
from envs.coding_env import CodingEnv
# Connect to existing server
coding_env = CodingEnv(base_url="<ENV_HTTP_URL_HERE>")
# Use as normal
result = coding_env.reset()
result = coding_env.step(CodeAction(code="print('Hello!')"))Note: When connecting to an existing server, coding_env.close() will NOT stop the server.
Run the complete example that demonstrates the full workflow:
python3 src/envs/coding_env/client/example_usage.pyThis example shows:
- Creating an environment from a Docker image
- Resetting and executing code through the environment
- Automatic cleanup with
close()
coding_env/
├── README.md # This file
├── models.py # Action, Observation, and State models
├── client/
│ ├── coding_env_client.py # CodingEnv client implementation
│ └── example_usage.py # Usage examples
└── server/
├── python_codeact_env.py # Core environment logic
├── app.py # FastAPI application
├── transforms.py # Observation transforms
├── Dockerfile # Container image definition
└── README.md # Server-specific documentation