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

Uploaded Python 3

File details

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

File metadata

  • Download URL: notebook_terminal-0.4.2.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.2.tar.gz
Algorithm Hash digest
SHA256 ec212c312385ae11c02d25710ff44380b6b793504a0863535bf194037b6662eb
MD5 1742c51355e42b2f756569e1a0310a2c
BLAKE2b-256 abfbd933fb52446cdcaff7f26f7f37f597ce5418a31424fd092d099cd1124c8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for notebook_terminal-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 27cd497d76dda858fa66cd499163b9f2b2501981633ad7a255472d669b28c6ee
MD5 0ba23f133a21dc315d05875cb598616b
BLAKE2b-256 1ac453a8adff10e2a583dd48cba33c2387e79dc6f80f81cbc96b59b6915100b6

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