openenv/coding_env
21
1---2title: Coding Environment Server3emoji: ๐ป4colorFrom: gray5colorTo: indigo6sdk: docker7pinned: false8app_port: 80009base_path: /web10tags:11 - openenv12---13 14# Coding Environment15 16A Python code execution environment that runs arbitrary Python code and returns results. Perfect for testing code execution infrastructure and demonstrating environment usage patterns.17 18## Quick Start19 20The simplest way to use the Coding environment is through the `CodingEnv` class:21 22```python23from envs.coding_env import CodeAction, CodingEnv24 25try:26 # Create environment from Docker image27 coding_env = CodingEnv.from_docker_image("coding-env:latest")28 29 # Reset30 result = coding_env.reset()31 print(f"Reset complete: exit_code={result.observation.exit_code}")32 33 # Execute Python code34 code_samples = [35 "print('Hello, World!')",36 "x = 5 + 3\nprint(f'Result: {x}')",37 "import math\nprint(math.pi)"38 ]39 40 for code in code_samples:41 result = coding_env.step(CodeAction(code=code))42 print(f"Code: {code}")43 print(f" โ stdout: {result.observation.stdout.strip()}")44 print(f" โ exit_code: {result.observation.exit_code}")45 46finally:47 # Always clean up48 coding_env.close()49```50 51That's it! The `CodingEnv.from_docker_image()` method handles:52- Starting the Docker container53- Waiting for the server to be ready54- Connecting to the environment55- Container cleanup when you call `close()`56 57## Building the Docker Image58 59Before using the environment, you need to build the Docker image:60 61```bash62# From project root63docker build -t coding-env:latest -f src/envs/coding_env/server/Dockerfile .64```65 66## Environment Details67 68### Action69**CodeAction**: Contains a single field70- `code` (str) - The Python code to execute71 72### Observation73**CodeObservation**: Contains the execution results74- `stdout` (str) - Standard output from code execution75- `stderr` (str) - Standard error from code execution76- `exit_code` (int) - Exit code (0 for success, non-zero for errors)77 78### State79**CodeState**: Tracks execution state80- `episode_id` (str) - Unique identifier for the episode81- `step_count` (int) - Number of steps taken82- `last_exit_code` (int) - Exit code from the last execution83 84## Advanced Usage85 86### Connecting to an Existing Server87 88If you already have a Coding environment server running, you can connect directly:89 90```python91from envs.coding_env import CodingEnv92 93# Connect to existing server94coding_env = CodingEnv(base_url="<ENV_HTTP_URL_HERE>")95 96# Use as normal97result = coding_env.reset()98result = coding_env.step(CodeAction(code="print('Hello!')"))99```100 101Note: When connecting to an existing server, `coding_env.close()` will NOT stop the server.102 103## Development & Testing104 105### Running the Full Example106 107Run the complete example that demonstrates the full workflow:108 109```bash110python3 src/envs/coding_env/client/example_usage.py111```112 113This example shows:114- Creating an environment from a Docker image115- Resetting and executing code through the environment116- Automatic cleanup with `close()`117 118## Project Structure119 120```121coding_env/122โโโ README.md # This file123โโโ models.py # Action, Observation, and State models124โโโ client/125โ โโโ coding_env_client.py # CodingEnv client implementation126โ โโโ example_usage.py # Usage examples127โโโ server/128 โโโ python_codeact_env.py # Core environment logic129 โโโ app.py # FastAPI application130 โโโ transforms.py # Observation transforms131 โโโ Dockerfile # Container image definition132 โโโ README.md # Server-specific documentation133```134 