Skip to main content

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

Project description

notebook-terminal 0.4.0

Cross-platform PTY/ConPTY terminal for JupyterLab with bundled xterm.js. It works locally on Windows and Linux and can also connect to a terminal on a remote JupyterHub/Jupyter Server using its REST API and terminal WebSocket endpoint.

Repository: https://github.com/ZawadzkiR/notebook-terminal

Local terminal

from notebook_terminal import terminal

term = terminal(height=450, scrollback=10_000)
term.run("python --version")

The prompt is produced by the real shell of the current user. It is not hard-coded.

Remote JupyterHub terminal

import os
from notebook_terminal import remote_terminal

term = remote_terminal(
    hub_url="https://jupyter.example.com",
    username="robert",
    token=os.environ["JUPYTERHUB_TOKEN"],
)

term.run("hostname")
term.run("pwd")
term.run("ls -la")

You can also provide the URL of the single-user Jupyter Server directly:

term = remote_terminal(
    server_url="https://jupyter.example.com/user/robert/",
    token=os.environ["JUPYTERHUB_TOKEN"],
)

Equivalent unified API:

term = terminal(
    backend="jupyterhub",
    server_url="https://jupyter.example.com/user/robert/",
    token=os.environ["JUPYTERHUB_TOKEN"],
)

The remote backend:

  1. creates a terminal with POST /api/terminals;
  2. opens wss://.../terminals/websocket/<name>;
  3. forwards xterm input as stdin messages;
  4. forwards remote stdout messages to xterm.js;
  5. synchronizes rows and columns with set_size;
  6. removes the created terminal on close by default.

To attach to an existing terminal:

term = remote_terminal(
    server_url="https://jupyter.example.com/user/robert/",
    token=os.environ["JUPYTERHUB_TOKEN"],
    terminal_name="1",
    create_terminal=False,
    delete_on_close=False,
)

For a named JupyterHub server:

term = remote_terminal(
    hub_url="https://jupyter.example.com",
    username="robert",
    server_name="gpu",
    token=os.environ["JUPYTERHUB_TOKEN"],
)

TLS

Keep certificate verification enabled. For a private CA, provide its certificate bundle:

term = remote_terminal(
    server_url="https://jupyter.example.com/user/robert/",
    token=os.environ["JUPYTERHUB_TOKEN"],
    verify_ssl="/path/company-ca.pem",
)

verify_ssl=False exists for controlled testing only.

Token security

Do not put the token directly in a notebook that will be committed or shared. Use an environment variable, keyring, or another secret store. A token with terminal access can execute commands with your remote account permissions.

By default the token is sent in the Authorization: token ... header. allow_token_in_url=True is available only for unusual proxies that reject authorization headers on WebSocket upgrades, because query-string tokens are easier to leak.

Remote limitations

  • The remote user server must already be running and reachable over HTTPS/WSS.
  • The token must be allowed to access the single-user server.
  • The administrator or reverse proxy may disable terminals or WebSockets.
  • run_python_file() interprets its path as a path on the remote server.
  • Rich-output capture is currently local-only. Use normal terminal output remotely.

Python file arguments

term.run_python_file(
    "train.py",
    args=["dataset.csv"],
    kwargs={"epochs": 20, "verbose": True},
)

This runs:

python train.py dataset.csv --epochs 20 --verbose

Installation

python -m pip install notebook_terminal-0.4.0-py3-none-any.whl

Restart the kernel and refresh JupyterLab after replacing an earlier version.

Optional integrations

pip install "notebook-terminal[flask]"
pip install "notebook-terminal[django]"
pip install "notebook-terminal[data,plotly]"

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.4.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.4.0-py3-none-any.whl (1.6 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: notebook_terminal-0.4.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.4.0.tar.gz
Algorithm Hash digest
SHA256 f0865d3a9436d041cdeb94825f6f2b96c6a2e980bfcdfb70f1277f6cda82c254
MD5 8f281ba330d03166b821b7e689867dd1
BLAKE2b-256 79f4384e02cf80bce58ae9c76e3359395a2faf9374053f97380f6819e05d4f8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for notebook_terminal-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3be0c7040d99fdb14f73154cfa9d1809434086f4247565fc47544a8e149c3bf6
MD5 e3a66d9cfdc98fd1212267c52a460a3a
BLAKE2b-256 dd840a3fca45164fb51f2521d8347d5b156bd3fed2b26ce07422f9692c0abf11

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