Skip to main content

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.js
  • term-pty-client.js - WebSocket client for PTY communication
  • term-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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mrmd_pty-0.1.1.tar.gz (75.8 kB view details)

Uploaded Source

Built Distribution

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

mrmd_pty-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file mrmd_pty-0.1.1.tar.gz.

File metadata

  • Download URL: mrmd_pty-0.1.1.tar.gz
  • Upload date:
  • Size: 75.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for mrmd_pty-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aef9dbc131019f36fa07bdce800688d39ff0facaf45b85b3da5d49b2e1c5b11c
MD5 9b61e44736783bc4b417cb28718b7ef2
BLAKE2b-256 5dfc660563a6736562903bc8cab7d50fa2540a7c88664430100f8c78a5b7aa05

See more details on using hashes here.

File details

Details for the file mrmd_pty-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mrmd_pty-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for mrmd_pty-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bf424eeda50354b5c58ae6581dd7c265d4d5d3aaa84e8f135da7d97d64dbfaa7
MD5 629e891113896c52360e74453cee0571
BLAKE2b-256 a7cd60e906c21c7ea263608be54f1297c029e96873e4a18d9c7ce2c45a136397

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