Skip to main content

Run Python code in a MicroPython/WASM sandbox

Project description

datasette-agent-micropython

PyPI Changelog Tests License

Run Python code in a MicroPython/WASM sandbox

Installation

Install this plugin in the same environment as Datasette.

datasette install datasette-agent-micropython

Usage

This plugin adds an execute_micropython tool to Datasette Agent. The tool runs Python code using a sandboxed MicroPython interpreter compiled to WASM.

Use it for pure computation, parsing, data transformation, math, and checking algorithms. Output is captured from stdout and stderr, so code should use print() to return values:

print(sum(range(10)))

Each Datasette Agent conversation gets its own persistent MicroPython interpreter. Variables, imports, and functions defined in one tool call can be reused later in the same conversation:

def fib(n):
    a, b = 0, 1
    out = []
    for _ in range(n):
        out.append(a)
        a, b = b, a + b
    return out

print(fib(10))

That state is not shared with other conversations. Use reset_context=true to clear the current conversation's interpreter before running code. If execution times out, exhausts fuel, or traps the WASM runtime, that conversation's interpreter is discarded and the next call starts fresh.

No network APIs are available, and no host filesystem is exposed by default.

The MicroPython environment includes a read_only_sql_query() helper for querying Datasette databases:

result = read_only_sql_query(
    "data",
    "select name, qty from items where qty > :min_qty",
    {"min_qty": 2},
)
print(result["columns"])
print(result["rows"])

It accepts name_of_database, sql, and an optional params dict. It uses the current actor's execute-sql permission and returns a dict containing columns, rows, and truncated, or an error key.

The tool accepts:

  • python - MicroPython code to execute
  • reset_context - clear this conversation's interpreter before running
  • show_result - render the code and captured output inline for the user

Use show_result=true when the user asked to see the code or output, or when showing both would help with explanation, debugging, teaching, or reproducibility. The rendered HTML shows the executed code, then the captured stdout. When this is set, the tool result tells the agent that the output has already been shown to the user and does not need to be repeated. Leave it unset for scratch calculations.

Configuration

Configure the plugin using Datasette plugin settings:

plugins:
  datasette-agent-micropython:
    max_sessions: 16
    wall_clock_timeout_seconds: 5.0
    memory_bytes: 16777216
    fuel: 10000000
    max_output_chars: 10000

Settings:

  • max_sessions - maximum live conversation interpreters to keep; defaults to 16. The least recently used interpreter is closed when the limit is exceeded.
  • wall_clock_timeout_seconds - timeout for each code execution; defaults to 5.0.
  • memory_bytes - maximum WebAssembly linear memory for each interpreter; defaults to 16777216 (16MB).
  • fuel - Wasmtime fuel budget refreshed for each execution; defaults to 10000000.
  • max_output_chars - maximum captured output/error characters returned to the model; defaults to 10000.

Development

To set up this plugin locally, first checkout the code. You can confirm it is available like this:

cd datasette-agent-micropython
# Confirm the plugin is visible
uv run datasette plugins

To run the tests:

uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datasette_agent_micropython-0.1a0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datasette_agent_micropython-0.1a0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file datasette_agent_micropython-0.1a0.tar.gz.

File metadata

File hashes

Hashes for datasette_agent_micropython-0.1a0.tar.gz
Algorithm Hash digest
SHA256 aff205fb4aeb31efe6263276eda1b09f10835ba14fcc8b34c708eb0ff457c20f
MD5 242ffa32348528e16d00723946f8243c
BLAKE2b-256 ca9aabaf729d2aa076702be2f87364a243ca7bc86b1b9f94d3507c3005d5e158

See more details on using hashes here.

Provenance

The following attestation bundles were made for datasette_agent_micropython-0.1a0.tar.gz:

Publisher: publish.yml on datasette/datasette-agent-micropython

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file datasette_agent_micropython-0.1a0-py3-none-any.whl.

File metadata

File hashes

Hashes for datasette_agent_micropython-0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b8207e48beccebb9cf62caed7d92666de3a08abbcb7e5d23039cbbec7ddbe98
MD5 9a606071818f821b023a20e73ce0b183
BLAKE2b-256 e2c78a64d677058d7665ecf04b9107f0d6a1edc17c2959691ef869e691b2f0a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for datasette_agent_micropython-0.1a0-py3-none-any.whl:

Publisher: publish.yml on datasette/datasette-agent-micropython

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page