Skip to main content

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

Project description

notebook-terminal

Cross-platform xterm.js terminal for Jupyter Notebook and JupyterLab, with local PTY/ConPTY and remote JupyterHub/Jupyter Server backends.

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

Installation

pip install notebook\_terminal-0.4.1-py3-none-any.whl

Restart the kernel and refresh JupyterLab after replacing an older build.

Local terminal

from notebook\_terminal import terminal

term = terminal(height=450)
term.run("python main.py", cwd="/home/user/project")

On Windows:

term.run("python main.py", cwd=r"C:\\Users\\User\\project")

Python code with a working directory

term.run\_python(
    """
from utils import helper
helper()
""",
    cwd="/home/user/project",
)

Python file, arguments and working directory

term.run\_python\_file(
    "main.py",
    cwd="/home/user/project",
    args=\["input.csv", "two words"],
    kwargs={"limit": 100, "verbose": True},
)

For a local backend, a relative file path is resolved against cwd. The child Python process is run from that directory, so imports such as from utils import helper work when utils.py or the package is located there.

Remote JupyterHub terminal

import os
from notebook\_terminal import remote\_terminal

remote = remote\_terminal(
    server\_url="https://jupyter.example.com/user/<user>/",
    token=os.environ\["JUPYTERHUB\_TOKEN"],
)

remote.run("python main.py", cwd="/home/user/project")
remote.run\_python("from utils import helper; helper()", cwd="/home/user/project")
remote.run\_python\_file(
    "main.py",
    cwd="/home/user/project",
    args=\["input.csv"],
)

A default remote directory can also be set once:

remote = remote\_terminal(
    server\_url="https://jupyter.example.com/user/user/",
    token=os.environ\["JUPYTERHUB\_TOKEN"],
    cwd="/home/user/project",
)

remote.run("git status")
remote.run\_python\_file("main.py")

The remote backend uses the Jupyter Server terminal REST API and terminal WebSocket. It does not require SSH. Remote cwd currently targets POSIX shells, which are standard on Linux JupyterHub installations.

cwd behavior

  • cwd on a single method call applies only to that command.
  • The current interactive shell directory is restored after the command.
  • terminal(cwd=...) sets the initial directory for a local terminal session.
  • remote\_terminal(cwd=...) sets the default directory used for remote commands.
  • A per-call cwd overrides the remote default.

Main API

term.run(command, cwd=None)
term.run\_python(code, cwd=None, rich\_output=False)
term.run\_python\_file(path, cwd=None, args=None, kwargs=None, rich\_output=False)
term.send\_text(text)
term.interrupt()
term.history()
term.success("Done")

Security

Treat a JupyterHub token as a password. Do not commit it to Git or place it directly in a notebook that will be shared.

Author

Robert Zawadzki — GitHub: ZawadzkiR

Function-level working directory and Python interpreter

cwd is supplied to each operation. Remote connection creation does not set a working directory.

term.run("git status", cwd="/home/user/project")
term.run\_python("from helpers import load\_data", cwd="/home/user/project", interpreter="python3.12")
term.run\_python\_file(
    "main.py",
    cwd="/home/user/project",
    interpreter="/home/user/.venvs/project/bin/python",
    args=\["input.csv"],
)

The same calls work with remote\_terminal(...). executable= remains available as a backward-compatible alias for interpreter=; do not pass different values to both.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: notebook_terminal-0.4.1.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.1.tar.gz
Algorithm Hash digest
SHA256 f763ced9f6923c9c8e42f4b18bf6227641b54f1480db371a5c47557e42085fc4
MD5 b2dde3f33a695784e12f00d18b978b67
BLAKE2b-256 cdf92d16a8b26818a320bf70f372fa10069fbb9f0fb8fd00b3cf017ffd1444db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for notebook_terminal-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5550f07dee15cb60f4942f87e22d6093282da7f691982007cd3a067515f5b50
MD5 44721e66380d9276477b4c07b5dffd1b
BLAKE2b-256 3e838c3335a3949f36481a6fb98779d0c9f9e9140699f4e7d39873b16134ff5b

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