IPython Kernel for Jupyter - provides the core communication layer between Jupyter frontends and the Python interpreter
—
The main IPython kernel that handles code execution, message processing, and maintains kernel state. Provides the core functionality for interactive Python computing including code execution, completion, inspection, debugging, and history management.
Main kernel implementation that processes execution requests and manages kernel state.
class IPythonKernel:
"""
Main IPython kernel implementation.
Handles code execution, completion, introspection, and maintains
execution state for interactive Python computing.
"""
def do_execute(self, code, silent=False, store_history=True,
user_expressions=None, allow_stdin=None):
"""
Execute Python code in the kernel.
Parameters:
- code (str): Python code to execute
- silent (bool): If True, don't broadcast output
- store_history (bool): Whether to store in execution history
- user_expressions (dict, optional): Expressions to evaluate
- allow_stdin (bool, optional): Whether to allow stdin requests
Returns:
dict: Execution result with status, execution_count, etc.
"""
def do_complete(self, code, cursor_pos):
"""
Generate code completion suggestions.
Parameters:
- code (str): Code context for completion
- cursor_pos (int): Cursor position in code
Returns:
dict: Completion results with matches, cursor positions
"""
def do_inspect(self, code, cursor_pos, detail_level=0):
"""
Inspect objects and provide documentation.
Parameters:
- code (str): Code containing object to inspect
- cursor_pos (int): Cursor position in code
- detail_level (int): Level of detail (0=brief, 1=detailed)
Returns:
dict: Inspection results with documentation, source, etc.
"""
def do_history(self, hist_access_type, output, raw, session=None,
start=None, stop=None, n=None, pattern=None, unique=False):
"""
Access execution history.
Parameters:
- hist_access_type (str): Type of history access
- output (bool): Include output in results
- raw (bool): Return raw input or processed
- session (int, optional): Session number
- start (int, optional): Start line number
- stop (int, optional): Stop line number
- n (int, optional): Number of entries
- pattern (str, optional): Search pattern
- unique (bool): Remove duplicates
Returns:
dict: History entries matching criteria
"""
def do_is_complete(self, code):
"""
Check if code is complete or needs more input.
Parameters:
- code (str): Code to check for completeness
Returns:
dict: Status indicating complete, incomplete, invalid, or unknown
"""
def do_shutdown(self, restart):
"""
Shutdown the kernel.
Parameters:
- restart (bool): Whether this is a restart
Returns:
dict: Shutdown confirmation
"""
# Kernel state attributes
execution_count: int # Current execution count
user_ns: dict # User namespace for code execution
shell: object # IPython shell instanceFoundation kernel class providing core messaging and execution infrastructure.
class Kernel:
"""
Base kernel class providing fundamental kernel functionality.
Implements the Jupyter kernel protocol and provides the framework
for message handling and kernel lifecycle management.
"""
def start(self):
"""Start the kernel's main event loop."""
def record_ports(self, ports):
"""
Record the ports being used by this kernel.
Parameters:
- ports (dict): Dictionary of port names to port numbers
"""
# Protocol version information
protocol_version: str
protocol_version_info: tuple
# Kernel identification
kernel_info: dict # Kernel metadata and capabilities
banner: str # Kernel banner textCompatibility aliases for different naming conventions.
# Backward compatibility alias
Kernel = IPythonKernel # Alias for IPythonKernelfrom ipykernel.ipkernel import IPythonKernel
# Create kernel instance (typically done by framework)
kernel = IPythonKernel()
# Execute code
result = kernel.do_execute("print('Hello, World!')")
print(f"Execution count: {result['execution_count']}")
print(f"Status: {result['status']}")
# Get completion suggestions
completions = kernel.do_complete("import num", 10)
print(f"Matches: {completions['matches']}")
# Inspect an object
inspection = kernel.do_inspect("len", 3, detail_level=1)
if inspection['found']:
print(f"Documentation: {inspection['data']['text/plain']}")from ipykernel.ipkernel import IPythonKernel
kernel = IPythonKernel()
# Check if code is complete
complete_code = "x = 1"
result = kernel.do_is_complete(complete_code)
print(f"Status: {result['status']}") # 'complete'
# Check incomplete code
incomplete_code = "if True:"
result = kernel.do_is_complete(incomplete_code)
print(f"Status: {result['status']}") # 'incomplete'
print(f"Indent: {result.get('indent', '')}")from ipykernel.ipkernel import IPythonKernel
kernel = IPythonKernel()
# Execute some code first
kernel.do_execute("x = 42")
kernel.do_execute("y = x * 2")
# Get recent history
history = kernel.do_history(
hist_access_type='tail',
output=True,
raw=False,
n=5
)
for session, line_num, input_code in history['history']:
print(f"[{line_num}]: {input_code}")from ipykernel.ipkernel import IPythonKernel
kernel = IPythonKernel()
# Get kernel information
info = kernel.kernel_info
print(f"Language: {info['language_info']['name']}")
print(f"Version: {info['language_info']['version']}")
print(f"Protocol version: {kernel.protocol_version}")Install with Tessl CLI
npx tessl i tessl/pypi-ipykernel