stanza.jupyter.core
Module Contents
Classes
Functions
Data
_config
API
Persistent Jupyter server state stored on disk.
Attributes: pid: Process ID of the Jupyter server url: Full URL with token for accessing JupyterLab started_at: ISO 8601 timestamp when server was started root_dir: Absolute path to the notebook root directory
Value: None
Value: None
Value: None
Value: None
Runtime Jupyter server status with calculated uptime.
Attributes: pid: Process ID of the running Jupyter server url: Full URL with token for accessing JupyterLab uptime_seconds: Number of seconds the server has been running root_dir: Absolute path to the notebook root directory
Value: None
Value: None
Value: None
Value: None
Jupyter runtime information parsed from jpserver-{pid}.json.
Attributes: url: JupyterLab URL with authentication token token: Authentication token for API requests port: Port number the server is listening on runtime_file: Path to the Jupyter runtime JSON file
Value: None
Value: None
Value: None
Value: None
Active Jupyter notebook session with log file metadata.
Attributes: notebook_path: Absolute path to the notebook file log_path: Absolute path to the notebook’s log file size_bytes: Size of the log file in bytes line_count: Number of lines in the log file
Value: None
Value: None
Value: None
Value: None
Configuration for Jupyter server management.
Attributes: state_dir: Directory for storing server state and logs log_max_size: Maximum log file size in bytes before truncation
Value: Path(...)
Value: None
Path to the server state JSON file.
Path to the file lock for preventing concurrent operations.
Path to the Jupyter server stdout log.
Path to the Jupyter server stderr log.
Path to stanza’s managed IPython configuration directory.
Path to IPython startup scripts directory for auto-logging.
Value: Config(...)
Read the Jupyter server state from disk.
Returns:
ServerState if state file exists and is valid, None otherwise
Write the Jupyter server state to disk atomically with secure permissions.
Uses a temporary file and atomic rename to prevent corruption. Sets file permissions to 0o600 (owner read/write only) for security.
Parameters:
ServerState to persist to disk
Delete the server state file and log files, ignoring errors.
Check if a process with the given PID is running.
Parameters:
Process ID to check
Returns:
True if process is running, False otherwise
File-based lock for preventing concurrent Jupyter server operations.
Uses fcntl.flock for process-level locking to ensure only one stanza process can modify the Jupyter server state at a time.
Initialization
Initialize lock with the lock file path and acquisition timeout.
Parameters:
Parameters:
- lock_file: Path to the lock file
- timeout: Maximum seconds to wait for lock acquisition
Acquire exclusive lock, blocking until available or timeout.
Returns:
Self for context manager usage
Raises:
RuntimeError: If lock cannot be acquired within timeout period
Release lock and close file descriptor.
Open stdout and stderr log files, truncating if they exceed max size.
If a log file exceeds log_max_size, it is truncated to keep only the last tail_bytes with a marker indicating truncation occurred.
Parameters:
Number of bytes to preserve when truncating logs
Returns:
Tuple of (stdout_file, stderr_file) opened in append mode
Discover Jupyter runtime information by polling for jpserver-{pid}.json.
Waits for Jupyter to write its runtime file containing the server URL, token, and port. Constructs a JupyterLab URL with authentication token.
Parameters:
Process ID of the Jupyter server
Maximum seconds to wait for runtime file
Seconds between file existence checks
Returns:
RuntimeInfo containing server URL, token, port, and runtime file path
Raises:
RuntimeError: If runtime file is not found within timeout period
Gracefully shutdown Jupyter server via REST API.
Sends a shutdown request to the Jupyter server using the authentication token, then polls until the process terminates or timeout is reached.
Parameters:
Server state containing URL and PID
Maximum seconds to wait for shutdown
Seconds between process aliveness checks
Terminate Jupyter server process with SIGTERM.
Sends SIGTERM signal and waits for process to exit gracefully.
Parameters:
Process ID to terminate
Maximum seconds to wait for termination
Seconds between process aliveness checks
Force kill Jupyter server process with SIGKILL.
Parameters:
Process ID to force kill
Make authenticated request to Jupyter API.
Parameters:
Server state containing URL with token
API endpoint path (e.g., “api/sessions”)
Returns:
JSON response from the API
Raises:
requests.HTTPError: If the request fails
Copy auto-logging startup script to stanza’s IPython configuration directory.
Raises:
RuntimeError: If the startup script cannot be found
Start a Jupyter server in the background with auto-logging enabled.
Launches jupyter_server as a detached process, discovers its runtime information, and saves the state to disk. Configures IPYTHONDIR to enable automatic cell output logging to notebook_name.log files.
Parameters:
Root directory for notebooks
Port number for the server (default: 8888)
Seconds to wait before checking if server started successfully
Returns:
Dictionary with keys: pid, url, started_at, root_dir
Raises:
RuntimeError: If server is already running or fails to start
Stop the Jupyter server using escalating shutdown strategies.
Attempts graceful shutdown via REST API, then SIGTERM, then SIGKILL if necessary. Cleans up state files when complete. Safe to call even if no server is running.
Get the current Jupyter server status including uptime.
Returns:
Dictionary with keys: pid, url, uptime_seconds, root_dir. None if no server is running or state is stale.
List active notebook sessions with log metadata.
Returns:
List of dictionaries, each containing: notebook_path, log_path, size_bytes, line_count. Empty list if no server is running.
Kill kernel for notebook via Jupyter API.
Parameters:
Name of the notebook (case-insensitive substring match)
Raises:
RuntimeError: If no server is running, notebook not found, or API call fails

