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
Metadata
Release files for mrmd-pty 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mrmd_pty-0.2.0.tar.gz | 72.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mrmd_pty-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 82.6 kB
Release files / mrmd_pty-0.2.0.tar.gz
| Download URL | mrmd_pty-0.2.0.tar.gz |
|---|---|
| Size | 72.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08f681d619227c12563328ba5b0ce0c812d29aac038d8bc153741b5cd1f5638c
|
|
BLAKE2b-256 checksum How to use checksums |
cf7f23277db772e0639848afd9fd9947d23dc493a7d93f17b43ea292e313f441
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / mrmd_pty-0.2.0-py3-none-any.whl
| Download URL | mrmd_pty-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5cc256afed3ff981c1f0f8ade73d6f9c4dbf1cfc32e9b2fa91d055bf2baf877
|
|
BLAKE2b-256 checksum How to use checksums |
b63291190169c7b0710db5752905a565a85b3acb0f81460d51e8bca6416a831d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|