CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-ipykernel

IPython Kernel for Jupyter - provides the core communication layer between Jupyter frontends and the Python interpreter

Pending
Overview
Eval results
Files

connection-management.mddocs/

Connection Management

Utilities for establishing and managing connections between Jupyter frontends and kernels. Handles connection files, ZMQ socket configuration, and communication setup for the Jupyter messaging protocol.

Capabilities

Connection File Operations

Functions for creating, locating, and managing kernel connection files that contain network configuration for frontend-kernel communication.

def write_connection_file(fname=None, shell_port=0, iopub_port=0, 
                         stdin_port=0, control_port=0, hb_port=0,
                         ip='127.0.0.1', key=b'', transport='tcp',
                         signature_scheme='hmac-sha256', kernel_name=''):
    """
    Write connection information to a JSON file.
    
    Re-exported from jupyter_client for convenience.
    
    Parameters:
    - fname (str, optional): Filename for connection file
    - shell_port (int): Port for shell channel
    - iopub_port (int): Port for IOPub channel  
    - stdin_port (int): Port for stdin channel
    - control_port (int): Port for control channel
    - hb_port (int): Port for heartbeat
    - ip (str): IP address for connections
    - key (bytes): HMAC key for message authentication
    - transport (str): Transport protocol ('tcp' or 'ipc')
    - signature_scheme (str): Message signature scheme
    - kernel_name (str): Name of the kernel
    
    Returns:
    str: Path to the written connection file
    """

def get_connection_file():
    """
    Get the path to the current kernel's connection file.
    
    Returns the connection file path for the currently running kernel,
    or None if not running in a kernel context.
    
    Returns:
    str or None: Path to connection file, or None if not in kernel
    """

def find_connection_file(filename_or_glob='kernel-*.json', profile=None):
    """
    Find a connection file by name or glob pattern.
    
    **DEPRECATED**: This function is deprecated and may be removed.
    
    Parameters:
    - filename_or_glob (str): Filename or glob pattern to search
    - profile (str, optional): IPython profile directory to search
    
    Returns:
    str: Path to found connection file
    
    Raises:
    IOError: If no matching connection file is found
    """

Connection Information

Functions for retrieving and using connection information from running kernels.

def get_connection_info():
    """
    Get connection information for the current kernel.
    
    Returns connection details including ports, IP address, and other
    network configuration needed to connect to the running kernel.
    
    Returns:
    dict: Connection information dictionary containing:
        - shell_port (int): Shell channel port
        - iopub_port (int): IOPub channel port
        - stdin_port (int): Stdin channel port
        - control_port (int): Control channel port
        - hb_port (int): Heartbeat port
        - ip (str): IP address
        - key (str): HMAC key (base64 encoded)
        - transport (str): Transport protocol
        - signature_scheme (str): Signature scheme
    
    Raises:
    RuntimeError: If not running in a kernel context
    """

Frontend Connection

Utilities for connecting external frontends to running kernels.

def connect_qtconsole(argv=None, **kwargs):
    """
    Connect a qtconsole to the current kernel.
    
    Launches a new qtconsole instance connected to the currently
    running kernel using the kernel's connection information.
    
    Parameters:
    - argv (list, optional): Additional command-line arguments for qtconsole
    - **kwargs: Additional options passed to qtconsole
    
    Returns:
    subprocess.Popen: Process object for the launched qtconsole
    
    Raises:
    RuntimeError: If not running in a kernel context
    ImportError: If qtconsole is not available
    """

Usage Examples

Basic Connection Information

from ipykernel import get_connection_info, get_connection_file

# Get current kernel's connection info (only works in kernel context)
try:
    info = get_connection_info()
    print(f"Kernel IP: {info['ip']}")
    print(f"Shell port: {info['shell_port']}")
    print(f"IOPub port: {info['iopub_port']}")
    
    # Get connection file path
    conn_file = get_connection_file()
    print(f"Connection file: {conn_file}")
    
except RuntimeError:
    print("Not running in a kernel context")

Connecting External Frontends

from ipykernel import connect_qtconsole

# Connect qtconsole to current kernel (only works in kernel context)
try:
    process = connect_qtconsole()
    print(f"Started qtconsole with PID: {process.pid}")
    
except RuntimeError:
    print("Not running in a kernel context")
except ImportError:
    print("qtconsole not available")

Creating Connection Files

from ipykernel import write_connection_file

# Create a connection file with specific configuration
conn_file = write_connection_file(
    fname='my-kernel.json',
    ip='192.168.1.100',
    shell_port=54321,
    iopub_port=54322,
    stdin_port=54323,
    control_port=54324,
    hb_port=54325,
    key=b'my-secret-key',
    kernel_name='my-python-kernel'
)

print(f"Created connection file: {conn_file}")

Finding Connection Files

from ipykernel import find_connection_file

# Find connection files (deprecated function)
try:
    conn_file = find_connection_file('kernel-*.json')
    print(f"Found connection file: {conn_file}")
    
except IOError:
    print("No connection files found")

Reading Connection Information

import json
from ipykernel import get_connection_file

# Read connection info from file
try:
    conn_file = get_connection_file()
    if conn_file:
        with open(conn_file, 'r') as f:
            conn_info = json.load(f)
        
        print("Connection configuration:")
        for key, value in conn_info.items():
            print(f"  {key}: {value}")
            
except (RuntimeError, FileNotFoundError):
    print("Could not read connection information")

Connection File Format

Connection files are JSON documents with the following structure:

ConnectionInfo = {
    "shell_port": int,      # Port for shell messages
    "iopub_port": int,      # Port for IOPub messages  
    "stdin_port": int,      # Port for stdin messages
    "control_port": int,    # Port for control messages
    "hb_port": int,         # Port for heartbeat
    "ip": str,              # IP address for connections
    "key": str,             # HMAC key (base64 encoded)
    "transport": str,       # Transport protocol ('tcp' or 'ipc')
    "signature_scheme": str, # Message signature scheme
    "kernel_name": str      # Kernel identifier
}

Install with Tessl CLI

npx tessl i tessl/pypi-ipykernel

docs

communication-framework.md

connection-management.md

core-kernel.md

data-utilities.md

gui-integration.md

in-process-kernels.md

index.md

io-streaming.md

kernel-application.md

kernel-embedding.md

matplotlib-integration.md

tile.json