PTY WebSocket server for terminal blocks in MRMD
Project description
mrmd-pty
PTY WebSocket server for terminal blocks in MRMD documents.
Overview
mrmd-pty provides persistent pseudo-terminal (PTY) sessions accessible via WebSocket. It's designed for embedding interactive terminals in MRMD markdown documents using ```term code blocks.
Features
- Persistent sessions - PTY sessions survive client disconnects
- Multi-client support - Multiple clients can view the same terminal
- Buffer replay - Reconnecting clients see previous output (last 64KB)
- Virtual environment support - Activate venvs in terminal sessions
- Session management - Create, list, rename, and kill terminals via REST API
Installation
pip install mrmd-pty
Or with uv:
uv pip install mrmd-pty
Usage
Start the server
# Default: localhost:8765
mrmd-pty
# Custom host/port
mrmd-pty --host 0.0.0.0 --port 9000
WebSocket API
Connect to ws://localhost:8765/api/pty with query parameters:
| Parameter | Description |
|---|---|
session_id |
Unique session identifier |
cwd |
Working directory (optional) |
venv |
Path to venv's python (optional) |
file_path |
Associated file path (optional) |
Messages to server:
// User input
{"type": "input", "data": "ls -la\n"}
// Resize terminal
{"type": "resize", "cols": 80, "rows": 24}
Messages from server:
Raw terminal output as text.
REST API
| Endpoint | Method | Description |
|---|---|---|
/api/terminals |
GET | List all terminals |
/api/terminals |
POST | Create terminal (pre-allocate) |
/api/terminals/rename |
POST | Rename a terminal |
/api/pty/kill |
POST | Kill a terminal session |
/api/terminals/kill-for-file |
POST | Kill terminals for a file |
Programmatic usage
from aiohttp import web
from mrmd_pty import setup_pty_routes, create_app
# Option 1: Use standalone app
app = create_app()
web.run_app(app, port=8765)
# Option 2: Add routes to existing app
app = web.Application()
setup_pty_routes(app)
# ... add your other routes
web.run_app(app)
Integration with mrmd-editor
The mrmd-editor package includes client-side components for ```term blocks:
term-widget.js- CodeMirror widget embedding xterm.jsterm-pty-client.js- WebSocket client for PTY communicationterm-block.js- Model for terminal block state
Architecture
Browser Server (mrmd-pty)
┌──────────────┐ ┌───────────────────┐
│ xterm.js │◄──WebSocket───►│ PtySession │
│ (terminal │ │ (pty.openpty()) │
│ emulator) │ │ │
└──────────────┘ └───────────────────┘
↑ ↑
keystrokes PTY master fd
renders output reads/writes
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mrmd_pty-0.2.0.tar.gz.
File metadata
- Download URL: mrmd_pty-0.2.0.tar.gz
- Upload date:
- Size: 72.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08f681d619227c12563328ba5b0ce0c812d29aac038d8bc153741b5cd1f5638c
|
|
| MD5 |
42ee1933f6c7fee310b58a1d8b881ed6
|
|
| BLAKE2b-256 |
cf7f23277db772e0639848afd9fd9947d23dc493a7d93f17b43ea292e313f441
|
File details
Details for the file mrmd_pty-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mrmd_pty-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5cc256afed3ff981c1f0f8ade73d6f9c4dbf1cfc32e9b2fa91d055bf2baf877
|
|
| MD5 |
c1badffd0c6e6f6ed9accf100e5b53fa
|
|
| BLAKE2b-256 |
b63291190169c7b0710db5752905a565a85b3acb0f81460d51e8bca6416a831d
|