Skip to main content

A cross-platform PTY/ConPTY terminal for Jupyter with bundled xterm.js

Project description

Notebook Terminal

Notebook Terminal embeds a real PTY/ConPTY terminal in Jupyter notebooks and provides reusable terminal sessions for Flask and Django.

Version 0.4.0 removes the anywidget dependency. The notebook frontend uses bundled xterm.js plus standard ipywidgets channels, so it requires no JupyterLab extension, administrator installation, HTTP server, iframe, WebSocket, CDN, or internet connection.

Requirements

  • Python 3.9+
  • ipywidgets 8+
  • Windows: pywinpty
  • Linux/macOS: the standard POSIX PTY implementation
  • A trusted notebook that permits JavaScript output

Installation

python -m pip install notebook-terminal

For an offline installation, download the wheel and its dependencies on another computer and install them with --no-index --find-links.

Jupyter quick start

from notebook_terminal import terminal

term = terminal(height=450, interactive=True)

The initial banner, prompt, username, host name, and working directory are generated by the real shell process. Nothing such as PS C:\\Users\\Admin> is hardcoded.

term.run("python --version")
term.run("git status")

Read-only mode:

term = terminal(interactive=False)
term.run("python script.py")

Python execution and rich output

term.run_python("""
import pandas as pd
import matplotlib.pyplot as plt

frame = pd.DataFrame({"x": [1, 2, 3], "y": [3, 1, 4]})
display(frame)
plt.plot(frame["x"], frame["y"])
plt.show()
""", rich_output=True, clear_previous=True)

Rich output tabs support DataFrames, images, HTML, Plotly, and standard Jupyter widgets created with run_kernel().

Status output

term.success("Code completed successfully")
term.info("Starting")
term.warning("Check configuration")
term.error("Operation failed")

These messages are emitted to the display output channel and are never sent to PowerShell/Bash stdin.

History and process control

term.history()
term.last_command
term.rerun()
term.clear_history()
term.interrupt()
term.restart()
term.wait_for("ready", timeout=10)
term.wait_until_idle(timeout=30)
term.clear(clear_tabs=True)

Clipboard

  • Ctrl+C copies selected terminal text.
  • Ctrl+C without a selection interrupts the process.
  • Ctrl+V pastes in interactive mode.

Flask and Django

The framework-independent TerminalSession and WebSocket adapters remain available:

from notebook_terminal.web import manager

session_id, session = manager.create(cwd=".", interactive=True)
session.run("python --version")

Install optional integrations:

python -m pip install "notebook-terminal[flask]"
python -m pip install "notebook-terminal[django]"

Flask uses flask-sock; Django uses Channels. Both reuse the same terminal session backend and xterm-compatible message protocol.

How the Jupyter bridge works

The notebook frontend is made from:

  1. a real PTY/ConPTY TerminalSession,
  2. bundled xterm.js and CSS,
  3. three hidden standard ipywidgets.Textarea channels,
  4. JavaScript output that connects xterm.js to those standard widgets,
  5. a kernel dispatcher that performs widget updates on the kernel event loop.

This avoids AnyModel, custom JupyterLab extensions, version registration errors, and the shell_parent ContextVar error caused by mutating widgets directly from PTY reader threads.

Limitations

Jupyter must allow trusted JavaScript output. Some heavily restricted notebook hosts may disable JavaScript output entirely; that cannot be bypassed by a Python package without administrator support. Standard ipywidgets must also work.

Build

python -m pip install build twine pytest
python -m pytest
python -m build
python -m twine check dist/*

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

notebook_terminal-0.3.0.tar.gz (3.1 MB view details)

Uploaded Source

Built Distribution

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

notebook_terminal-0.3.0-py3-none-any.whl (1.6 MB view details)

Uploaded Python 3

File details

Details for the file notebook_terminal-0.3.0.tar.gz.

File metadata

  • Download URL: notebook_terminal-0.3.0.tar.gz
  • Upload date:
  • Size: 3.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for notebook_terminal-0.3.0.tar.gz
Algorithm Hash digest
SHA256 29116e1e4182407d5a86f1f420b7b81ca444488cb6e9f5d4c997825a1011708f
MD5 9c93a7345bda3c19a14360380f04d528
BLAKE2b-256 01c698db2c82b319d4292a40a95a3c4ffc634d9fade7d2515cc84c35aaeba8a8

See more details on using hashes here.

File details

Details for the file notebook_terminal-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for notebook_terminal-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0316b06db3c729943131c364a14fd3d326f8e0047d56826a198d2de4147ca3c6
MD5 ccdc834d8db915dd2eef34a225b9f3d9
BLAKE2b-256 fa3966430157cbc0c842e91b4e8b5f3267ed26d6d105895398cb2227e441d437

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