Skip to content
Β 
Β 

Latest commit

Β 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐍 Python Code Runner

A secure Python code execution environment that runs code in isolated Docker containers. This project provides two different frontend interfaces for the same backend mechanism.

Features

  • Secure Execution: Code runs in isolated Docker containers
  • Real-time Output: See execution results and runtime immediately
  • Modern UI: Beautiful, responsive interface
  • Code Persistence: Your code is automatically saved
  • Keyboard Shortcuts: Use Ctrl+Enter to run code quickly
  • Error Handling: Clear error messages and status indicators

Two Frontend Options

1. Web Frontend (Recommended)

A modern, responsive web interface built with HTML, CSS, and JavaScript.

2. Streamlit Frontend

A Python-based web interface using Streamlit framework.

Prerequisites

  • Python 3.8+
  • Docker installed and running
  • Required Python packages (see requirements.txt)

Installation

  1. Clone the repository:
git clone <repository-url>
cd attempy-daa
  1. Install dependencies:
pip install -r requirements.txt
  1. Make sure Docker is running on your system.

Usage

Option 1: Web Frontend (Recommended)

  1. Start the FastAPI server:
python api_server.py
  1. Open your browser and navigate to:
http://localhost:8000
  1. Write your Python code in the editor and click "Run Code" or press Ctrl+Enter.

Option 2: Streamlit Frontend (on the code_runner branch)

  1. Start the Streamlit app:
streamlit run code_runner_app.py
  1. The app will automatically open in your browser, or navigate to:
http://localhost:8501 

NOTE: To use the agent, run the backend on port 8005, then run

adk web

this will start up the dev ui of the google adk package

API Endpoints

The web frontend uses a REST API with the following endpoints:

  • GET / - Serve the main HTML page
  • POST /api/run-code - Execute Python code
  • GET /api/health - Health check endpoint

Example Code

Here are some examples you can try:

Basic Operations

# Simple calculation
x = 10
y = 20
print(f"Sum: {x + y}")
print(f"Product: {x * y}")

List Operations

# List comprehensions
numbers = [1, 2, 3, 4, 5]
squares = [n**2 for n in numbers]
print(f"Squares: {squares}")

# Filter even numbers
evens = [n for n in numbers if n % 2 == 0]
print(f"Even numbers: {evens}")

Functions

def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

print(f"Factorial of 5: {factorial(5)}")

Classes

class Calculator:
    def __init__(self):
        self.history = []
    
    def add(self, a, b):
        result = a + b
        self.history.append(f"{a} + {b} = {result}")
        return result

calc = Calculator()
print(f"5 + 3 = {calc.add(5, 3)}")

Project Structure

attempy-daa/
β”œβ”€β”€ api_server.py          # FastAPI server for web frontend
β”œβ”€β”€ code_runner_app.py     # Streamlit frontend
β”œβ”€β”€ container_runner.py    # Core backend (Docker execution)
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ static/               # Web frontend files
β”‚   β”œβ”€β”€ index.html        # Main HTML page
β”‚   β”œβ”€β”€ styles.css        # CSS styles
β”‚   └── script.js         # JavaScript functionality
└── README.md            # This file

Security Features

  • Container Isolation: Each code execution runs in a separate Docker container
  • Resource Limits: Containers are automatically removed after execution
  • No Network Access: Containers run without network access by default
  • Timeouts: Execution is limited to prevent infinite loops

Customization

Adding New Examples

You can add more example code by modifying the addExampleCode function in static/script.js.

Styling

The web frontend uses CSS custom properties and can be easily customized by modifying static/styles.css.

Backend Configuration

The ContainerRunner class in container_runner.py can be modified to:

  • Use different Python versions
  • Add more security restrictions
  • Implement resource limits
  • Add support for other programming languages

Troubleshooting

Docker Issues

  • Make sure Docker is running: docker --version
  • Check if you have permission to run Docker commands
  • On Windows/macOS, ensure Docker Desktop is running

Port Conflicts

  • Web frontend uses port 8000 by default
  • Streamlit uses port 8501 by default
  • Change ports in the respective files if needed

Code Execution Errors

  • Check that your Python syntax is correct
  • Ensure all required imports are included
  • Remember that input() functions won't work (use fixed values instead)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test both frontends
  5. Submit a pull request

Support

If you encounter any issues:

  1. Check the troubleshooting section
  2. Ensure all prerequisites are met
  3. Try running the code locally first
  4. Open an issue with detailed error information

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages