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.

Test PyPI version Python 3.10 License


✨ Features

  • ✅ Seamless remote Python REPL over dfx canister call
  • ✅ Captures stdout, stderr, and return values
  • ✅ Simple commands (:q to quit, :help for guidance)
  • ✅ Packaged as a Python CLI: kybra-simple-shell
  • ✅ Network selection support (local, IC mainnet via --ic, and custom networks)

📦 Installation

pip install kybra-simple-shell

⚡ Quick usage within your canister

# Recommended setup
pyenv install 3.10.7
pyenv local 3.10.7
python -m venv venv
source venv/bin/activate

# Install the package
pip install kybra-simple-shell

Now add the following code to your canister:

from kybra import update, ic

@update
def execute_code(code: str) -> str:
    """Executes Python code and returns the output.

    This is the core function needed for the Kybra Simple Shell to work.
    It captures stdout, stderr, and return values from the executed code.
    """
    import io
    import sys
    import traceback

    stdout = io.StringIO()
    stderr = io.StringIO()
    sys.stdout = stdout
    sys.stderr = stderr

    try:
        # Try to evaluate as an expression
        result = eval(code, globals())
        if result is not None:
            ic.print(repr(result))
    except SyntaxError:
        try:
            # If it's not an expression, execute it as a statement
            # Use the built-in exec function but with a different name to avoid conflict
            exec_builtin = exec
            exec_builtin(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()
# 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 <MY_CANISTER_ID>              # Local network (default)
# kybra-simple-shell --ic <MY_CANISTER_ID>       # IC mainnet
>>> print("Hello from your canister!")
Hello from your canister!
>>> import my_library_inside_the_canister
>>> a = 42
>>>:q
Goodbye!

Now, connect to canister again and see any changes in memory:

kybra-simple-shell <MY_CANISTER_ID>
>>> print(a)
42

🔍 CLI Features

The shell provides helpful version information on startup (Python, DFX, Kybra versions) and supports commands like :help for guidance and :q to exit.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

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.5.tar.gz (11.0 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.5-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kybra_simple_shell-0.1.5.tar.gz
  • Upload date:
  • Size: 11.0 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.5.tar.gz
Algorithm Hash digest
SHA256 af1557088dec0ad168d74a70e987a0c3d004c90186e82639d983f929cd3aaf77
MD5 d5a622cdd6e22002df6c14bffe4df544
BLAKE2b-256 d4432097d1dc6f561c95b9a3782fd56136e24defa7b1a6135d8952e42c9cffcc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kybra_simple_shell-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7f9e577f3500ed01685ecf11922b1544484f563cd6c750de62f3ab28c7f0f527
MD5 cd7fc31ed78098650bad68e6dcf29f14
BLAKE2b-256 702033527db2afbb75ed020c0bfc495fef462bd57f64e573b60042cfe720227b

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