Skip to main content

Official Python SDK for GBox API

Project description

GBox Python SDK

PyPI version License: Apache 2.0

Official Python SDK for GBox.

GBox provides a self-hostable sandbox environment designed for AI agents, offering capabilities like terminal access, file management, and browser interaction.
This SDK allows Python applications to programmatically manage GBox resources, primarily the execution environments (Boxes) and the shared file volume, enabling seamless integration with agent workflows.

Installation

Make sure you have Python >= 3.7 installed.

pip install pygbox

The SDK requires the following core dependencies:

  • requests>=2.25.0
  • pydantic>=2.0

Basic Usage

Initialize the client

Initialize the client, pointing it to your self-hosted GBox server:

from pygbox import GBoxClient

# Initialize the client (defaults to http://localhost:28080)
# Use base_url to specify a different server address
gbox = GBoxClient(base_url="your-gbox-server-address")

The GBoxClient can be configured during initialization:

  • base_url (str): The base URL for the GBox API server (e.g., "http://my-gbox-server:28080").
  • timeout (int): The default request timeout in seconds (default: 60).
  • config (GBoxConfig): An instance of gbox.config.GBoxConfig for more advanced configuration (e.g., custom logger).

The client provides access to managers for different resources:

  • gbox.boxes: For managing Box instances.
  • gbox.files: For interacting with the shared file volume.

Box Management

Create a box with a specific image, labels and other configurations.

# --- Create a box with a specific image, labels and other configurations ---
box = gbox.boxes.create(image="python:3.11-slim", labels={"purpose": "sdk_demo"})

# --- Get a box by ID ---
box = gbox.boxes.get(box_id)

# --- List all boxes ---
boxes = gbox.boxes.list()

# --- Delete all boxes ---
gbox.boxes.delete_all()

Box Lifecycle and Usage

Manage the lifecycle of a box.

# --- Start a box ---
box.start()

# --- Stop a box ---
box.stop()

# --- Update status of a box ---
box.reload()

# --- Delete a box ---
box.delete()

Run command in a box.

# --- Run a command in a box ---
exit_code_ls, stdout_ls, stderr_ls = box.run(["ls", "-l"])

# --- Run a command in a box ---
box.run(["touch", "app.py"])

Copy file between box and local directory.

local_copy_source = "demo_copy_local_to_box.txt"
box_copy_target = "box:/tmp/copied_from_local.txt"

box.copy(source=local_copy_source, target=box_copy_target)

box_copy_source = f"box:/tmp/demo_upload.txt"
local_copy_target = "./downloaded_from_box.txt"

box.copy(source=box_copy_source, target=local_copy_target)

Files (Shared Volume)

Interact with files stored in the GBox shared volume using gbox.files.

from gbox import GBoxClient, NotFound, APIError # Assuming necessary imports

# (Assume gbox client is initialized: gbox = GBoxClient())
# (Assume a box object exists: box = gbox.boxes.get("your_box_id"))
# (Assume '/shared/data/my_file.txt' exists in the shared volume)
# (Assume '/app/output.log' exists inside the 'box')

file_path_shared = "/shared/data/my_file.txt" # Path in the shared volume
file_path_in_box = "/app/output.log"         # Path inside the specific box
box_id = box.id                              # Get box ID from the box object

# --- Basic File Operations (Shared Volume) ---

# Check if a file exists
file_exists = gbox.files.exists(file_path_shared)

if file_exists:
    try:
        # Get the File object
        file_obj = gbox.files.get(file_path_shared)

        # Read content (binary)
        content_bytes = file_obj.read()

        # Read content (text, assuming UTF-8)
        content_text = file_obj.read_text()

# --- Sharing Files from a Box ---

try:
    # Share using the box ID
    shared_file_from_id = gbox.files.share_from_box(box_id, file_path_in_box)

    # Or share using the Box object directly
    shared_file_from_obj = gbox.files.share_from_box(box, file_path_in_box)

    # Now 'shared_file_from_obj' can be used to read the shared file's content
    # e.g., shared_content = shared_file_from_obj.read_text()


# --- File Reclamation (Cleanup) ---

reclaim_result = gbox.files.reclaim()

License

This SDK is licensed under the Apache License 2.0. See the LICENSE file for details.

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

pygbox-0.0.1.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

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

pygbox-0.0.1-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file pygbox-0.0.1.tar.gz.

File metadata

  • Download URL: pygbox-0.0.1.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pygbox-0.0.1.tar.gz
Algorithm Hash digest
SHA256 abf77e2e9ad86719286365d1c5e786e40b834fa1dad1368c63bf6fd462bf7962
MD5 35b424e725711872b5425ea45ad5dc8a
BLAKE2b-256 0afa5a3bb8ea0f9115977a5ed9805fab03b85f23f4f8f130c9d38396dab71e03

See more details on using hashes here.

File details

Details for the file pygbox-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pygbox-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pygbox-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a27eebd110cd77832b6c652f79af38ff711b5f30c346a3b484b0e55296b7449
MD5 ad3fef6c1ba895b867c0470abd6fc77e
BLAKE2b-256 d4c3e39eb69d09aff83d55e64ca156c2d5aaa1e6ca67c6c02c30ca0f2be926ae

See more details on using hashes here.

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