Skip to main content

Serve Python CLI applications in the browser using ttyd

Project description

Terminaide

A handy Python library for serving CLI applications in a browser. Terminaide allows developers to instantly web-enable terminal-based Python applications without packaging or distribution overhead, making it ideal for prototypes, demos, and applications with small user bases.

How It Works

Terminaide builds on three core technical elements:

  1. ttyd Management: Automatically handles the installation and lifecycle of ttyd (terminal over WebSocket) binaries for the current platform. This eliminates the need for manual ttyd configuration.

  2. Single-Port Proxying: Routes all HTTP and WebSocket traffic through a single port, simplifying deployments in containers and cloud environments while maintaining cross-origin security.

  3. FastAPI Integration: Seamlessly integrates with FastAPI applications, allowing terminals to coexist with traditional web pages and REST endpoints via flexible route prioritization.

Installation

Install it from PyPI via your favorite package manager:

pip install terminaide
# or
poetry add terminaide

Terminaide automatically installs the ttyd binary if not already present, simplifying setup for both novice users and containerized deployments without requiring system-level dependencies.

Usage

There are two primary ways to use terminaide, depending on your needs:

Single Script

To serve a single Python script with the absolute bare minimum overhead:

# app.py
from fastapi import FastAPI
from terminaide import serve_terminal
import uvicorn

app = FastAPI()
serve_terminal(app, client_script="my_script.py")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

This approach is ideal when you have an existing terminal application that you don't want to modify. Your script runs exactly as it would in a normal terminal, but becomes accessible through any web browser.

Multi Mode

To serve multiple terminals in a more complex application:

# app.py
from fastapi import FastAPI
from terminaide import serve_terminal
import uvicorn

app = FastAPI()

# Custom routes defined first take precedence
@app.get("/")
async def root():
    return {"message": "Welcome to my terminal app"}

serve_terminal(
    app,
    terminal_routes={
        "/cli1": "script1.py",
        "/cli2": ["script2.py", "--arg1", "value"],
        "/cli3": {
            "client_script": "script3.py",
            "title": "Advanced CLI"
        }
    }
)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

This approach works best when you're building a new application with terminaide from the start, especially when you need to combine web interfaces with multiple terminal applications under different routes.

Configuration Options

Terminaide additionally provides a number of configuration options:

serve_terminal(
    app,
    mount_path="/app",               # Base path (default: "/")
    port=7681,                       # Base port for ttyd (default: 7681)
    theme={                          # Terminal theme
        "background": "black",
        "foreground": "white", 
    },
    ttyd_options={                   # ttyd-specific options
        "credential_required": True,
        "username": "user",
        "password": "pass",
    },
    title="My Terminal App",         # Custom title
    debug=True                       # Debug mode
)

Examples

The example/ directory demonstrates these configurations with several ready-to-use examples:

poe serve              # Default mode with instructions
poe serve single       # Single application mode
poe serve multi        # Multi-terminal mode with HTML menu
poe serve container    # Run in Docker container

Pre-Requisites

  • Python 3.12+
  • Linux or macOS (Windows support on roadmap)
  • Docker/Poe for examples

Limitations

Terminaide is designed to support rapid prototype deployments for small user bases. As a result:

  • Not intended for high-traffic production environments
  • Basic security features (though ttyd authentication is supported)
  • Windows installation not yet supported (on roadmap)
  • Terminal capabilities limited to what ttyd provides

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

terminaide-0.0.4.tar.gz (33.3 kB view details)

Uploaded Source

Built Distribution

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

terminaide-0.0.4-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file terminaide-0.0.4.tar.gz.

File metadata

  • Download URL: terminaide-0.0.4.tar.gz
  • Upload date:
  • Size: 33.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Darwin/24.3.0

File hashes

Hashes for terminaide-0.0.4.tar.gz
Algorithm Hash digest
SHA256 07d19770b26be9cf8ea56e86fd4d1117df79700d19db8c2566b18d3f7dbfe2f3
MD5 9fdec86f8b8ea78da681413df2377618
BLAKE2b-256 21e7bf2b4473b4f401244252381a26f9d76f74a8571ce796586bbf4dbb37f4c9

See more details on using hashes here.

File details

Details for the file terminaide-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: terminaide-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 39.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Darwin/24.3.0

File hashes

Hashes for terminaide-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fb3250b24a77d7166189e99b6df607f913f87901cfd8eba8b3c8b3d9f42968df
MD5 14e2dce19a9a33dc35930025a3a5f61b
BLAKE2b-256 beec8183bdf3d6043d2726ad07183b7696976d2bc20f065eb28c7d923ac551ad

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