0
# Kernel Application and Lifecycle
1
2
Core kernel application framework for launching, configuring, and managing IPython kernel instances within the Jupyter ecosystem. Provides the essential infrastructure for kernel startup, configuration, and integration with Jupyter's architecture.
3
4
## Capabilities
5
6
### Kernel Application Class
7
8
Main application class that manages kernel lifecycle, configuration, and startup procedures.
9
10
```python { .api }
11
class IPKernelApp:
12
"""
13
Main application for launching IPython kernels.
14
15
Handles configuration, connection setup, and kernel initialization.
16
"""
17
18
def initialize(self, argv=None):
19
"""
20
Initialize the application with configuration and command-line arguments.
21
22
Parameters:
23
- argv (list, optional): Command-line arguments for configuration
24
"""
25
26
def start(self):
27
"""
28
Start the kernel application and begin message processing.
29
"""
30
31
# Configuration attributes
32
connection_file: str # Path to connection file
33
ip: str # IP address for kernel connections
34
shell_port: int # Port for shell messages
35
iopub_port: int # Port for IOPub messages
36
stdin_port: int # Port for stdin messages
37
control_port: int # Port for control messages
38
hb_port: int # Port for heartbeat
39
```
40
41
### Kernel Launching
42
43
Primary entry point for starting new kernel instances with proper configuration and connection setup.
44
45
```python { .api }
46
def launch_new_instance(argv=None, **kwargs):
47
"""
48
Launch a new IPython kernel instance.
49
50
This is the main entry point used by Jupyter to start kernels.
51
52
Parameters:
53
- argv (list, optional): Command-line arguments
54
- **kwargs: Additional configuration options
55
56
Returns:
57
None (runs kernel until termination)
58
"""
59
```
60
61
### Kernel Specification Management
62
63
Tools for installing and managing kernel specifications that tell Jupyter how to launch the IPython kernel.
64
65
```python { .api }
66
KERNEL_NAME: str # Default kernel name based on Python version
67
68
def make_ipkernel_cmd(mod='ipykernel_launcher', **kwargs):
69
"""
70
Build command list for launching IPython kernel.
71
72
Parameters:
73
- mod (str): Module name for kernel launcher
74
- **kwargs: Additional command-line options
75
76
Returns:
77
list: Command arguments for kernel launch
78
"""
79
80
def get_kernel_dict(**kwargs):
81
"""
82
Construct kernel.json dictionary for kernel specification.
83
84
Parameters:
85
- **kwargs: Kernel specification options
86
87
Returns:
88
dict: Kernel specification dictionary
89
"""
90
91
def write_kernel_spec(path=None, overrides=None, **kwargs):
92
"""
93
Write kernel specification to specified directory.
94
95
Parameters:
96
- path (str, optional): Directory to write kernel spec
97
- overrides (dict, optional): Override default kernel spec values
98
- **kwargs: Additional kernel spec options
99
100
Returns:
101
str: Path to written kernel specification
102
"""
103
104
def install(kernel_spec_manager=None, user=False, kernel_name=None,
105
display_name=None, profile=None, prefix=None, **kwargs):
106
"""
107
Install IPython kernelspec for Jupyter.
108
109
Parameters:
110
- kernel_spec_manager: Kernel spec manager instance
111
- user (bool): Install for current user only
112
- kernel_name (str, optional): Name for installed kernel
113
- display_name (str, optional): Display name for kernel
114
- profile (str, optional): IPython profile to use
115
- prefix (str, optional): Installation prefix
116
- **kwargs: Additional installation options
117
118
Returns:
119
str: Path to installed kernel specification
120
"""
121
```
122
123
### Kernel Specification Application
124
125
Application class for installing kernel specifications from command line.
126
127
```python { .api }
128
class InstallIPythonKernelSpecApp:
129
"""
130
Application for installing IPython kernel specifications.
131
132
Provides command-line interface for kernel spec installation.
133
"""
134
135
def start(self):
136
"""Start the kernel spec installation process."""
137
138
# Configuration options
139
user: bool # Install for user only
140
kernel_name: str # Name for kernel specification
141
display_name: str # Display name for kernel
142
profile: str # IPython profile to use
143
```
144
145
## Usage Examples
146
147
### Basic Kernel Launch
148
149
```python
150
from ipykernel.kernelapp import launch_new_instance
151
152
# Launch kernel with default configuration
153
launch_new_instance()
154
155
# Launch with custom configuration
156
launch_new_instance(argv=['--ip=127.0.0.1', '--shell-port=12345'])
157
```
158
159
### Kernel Specification Installation
160
161
```python
162
from ipykernel.kernelspec import install, get_kernel_dict
163
164
# Install default IPython kernel
165
install()
166
167
# Install with custom name and display name
168
install(
169
user=True,
170
kernel_name='python3-custom',
171
display_name='Python 3 (Custom)'
172
)
173
174
# Get kernel specification dictionary
175
kernel_dict = get_kernel_dict(
176
display_name='Python 3',
177
interrupt_mode='message'
178
)
179
print(kernel_dict)
180
```
181
182
### Application Configuration
183
184
```python
185
from ipykernel.kernelapp import IPKernelApp
186
187
# Create and configure kernel application
188
app = IPKernelApp()
189
app.initialize(['--ip=0.0.0.0', '--shell-port=54321'])
190
191
# Start the kernel
192
app.start() # Runs until kernel shutdown
193
```