Skip to main content

A lightweight CLI tool for interacting with Python-based Kybra canisters

Project description

Kybra Simple Shell

A lightweight CLI tool for interacting with Python-based Kybra canisters on the Internet Computer (ICP). This acts as a remote REPL, letting you send Python code to your canister and see the output — including stdout, return values, and errors — right in your terminal.


✨ Features

  • ✅ Seamless remote Python REPL over dfx canister call
  • ✅ Captures stdout, stderr, and return values
  • ✅ Simple commands (:q to quit, more coming)
  • ✅ Packaged as a Python CLI: kybra-simple-shell

📦 Installation

First, clone the repo and install locally:

git clone https://github.com/your-org/kybra-simple-shell.git
cd kybra-simple-shell
pip install -e .

⚡ Quick Start with Example Canister

Want to try it out quickly? The repository includes a test canister with the required execute_code method:

# Clone the repository
git clone https://github.com/your-org/kybra-simple-shell.git
cd kybra-simple-shell

# Install the package
pip install -e .

# Navigate to the tests directory
cd tests

# Start a local Internet Computer replica
dfx start --clean --background

# Deploy the test canister
dfx deploy

# Use the shell with the test canister directly by name
kybra-simple-shell test

Now you can try Python commands in the shell:

>>> print("Hello from your canister!")
Hello from your canister!
>>> a = 42
>>> a * 2
84
>>> import math
>>> math.sqrt(16)
4.0

# You can access and interact with objects in the canister's memory
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list.append(6)
>>> my_list
[1, 2, 3, 4, 5, 6]

# Objects persist between commands as they're stored in the canister's memory
>>> len(my_list)
6

# You can even import modules defined in your canister
>>> from tests import test_functions
>>> dir(test_functions)

When you're done, stop the local replica:

dfx stop

🚀 Usage

Before using the shell, make sure:

Your Kybra canister is running and deployed.

The canister has an exec(code: str) -> str update method that executes Python code remotely.

Start the shell bash Copy Edit kybra-simple-shell Example interaction: python-repl Copy Edit Kybra Simple Shell (type ':q' to quit)

a = 5 print(a) 5 a * 2 10 1 / 0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero :q ⚙️ Configuration By default, the tool looks for a canister called my_canister.

You can edit shell.py to change the CANISTER_NAME, or add config file support (e.g., .kybra_shell.toml) as a future improvement.

🧠 How It Works Under the hood, kybra-simple-shell wraps calls to:

bash Copy Edit dfx canister call my_canister exec '(record { code = "..." })' The canister executes the Python code, captures output and errors using io.StringIO, and returns everything as a string. The CLI parses and prints the result.

📚 Canister Requirements Your Kybra canister should include this exec() method:

python Copy Edit @update def exec(code: str) -> str: import sys, io, traceback stdout = io.StringIO() stderr = io.StringIO() sys.stdout = stdout sys.stderr = stderr

try:
    result = eval(code, globals())
    if result is not None:
        print(repr(result))
except SyntaxError:
    try:
        exec(code, globals())
    except Exception:
        traceback.print_exc()
except Exception:
    traceback.print_exc()

sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
return stdout.getvalue() + stderr.getvalue()

📌 Future Plans Multi-line input

Command history

Custom shell commands like :logs, :clear, :canister

Configurable canister name from .kybra_shell.toml

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

kybra_simple_shell-0.1.4.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

kybra_simple_shell-0.1.4-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file kybra_simple_shell-0.1.4.tar.gz.

File metadata

  • Download URL: kybra_simple_shell-0.1.4.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for kybra_simple_shell-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e40a571dcf612d19e7dbc67b6af57a8c3fd8eb000cb55f3cba8bbc40fb35a12d
MD5 98802de65262ab848c29ba4802eb672c
BLAKE2b-256 1aa45499621590932079ea7861c6bdbe573cbab86582c44e60f5ee8b0d89bcca

See more details on using hashes here.

File details

Details for the file kybra_simple_shell-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for kybra_simple_shell-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 71cad67976dadb3641491e6810111d3bf10374148fe670cfef2c93c5e6e457b8
MD5 b0427ce2130f900a8af637737dbed0a7
BLAKE2b-256 be3395d14b34e40477bf1def406e61f41eb88b34612e5f3f8fa549556f5c1c98

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