Skip to main content

A cross-platform embeddable PTY/ConPTY terminal for Jupyter and Python web applications

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.2.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.2.0.tar.gz (1.6 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.2.0-py3-none-any.whl (1.6 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: notebook_terminal-0.2.0.tar.gz
  • Upload date:
  • Size: 1.6 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.2.0.tar.gz
Algorithm Hash digest
SHA256 77958182e5ac44ab350eb6b5a9bdbbaa5d7dbe6066b1d5e65e097e6f55249f1d
MD5 aeaf9be12e6a186bc0d6feed4d745d25
BLAKE2b-256 b37cb72048425f009c422706fb88052b855bcd7544f22e292e68309f41f67b36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for notebook_terminal-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f405c70e839841c498cec668b6d365e2575c9420494b6c8fa8873ca83061443a
MD5 5fb21339268df8c41a8071c5eb11c275
BLAKE2b-256 bbd64f940dabbec3403c836d73cac5d41122fbe96f3483add0d1b5d86cbeacd7

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