59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 | @typ_ext.override
def draw(self, context: bpy.types.Context) -> None: # noqa: C901, PLR0912, PLR0915
"""Draw the Jupyter kernel panel, including a few options.
Notes:
Run by Blender when the panel needs to be displayed.
Parameters:
context: The Blender context object.
"""
if context.region is None or context.preferences is None:
return
width_units_nrm = context.region.width / context.preferences.system.dpi
width_chars = max(int(width_units_nrm / 0.11), 1)
layout = self.layout
if layout is None:
return
####################
# - Section: Respect Online Access
####################
if not bpy.app.online_access:
box = layout.box()
col = box.column()
row = col.row(align=True)
row.alignment = 'CENTER'
row.label(text='Online Access Disabled')
col.label(text='Required for exposing kernel sockets.')
col.label(text='1. Open System Preferences.')
col.label(text='2. Toggle "Allow Online Access".')
op = box.operator('screen.userpref_show', text='Open System Preferences')
op.section = 'SYSTEM' # pyright: ignore[reportAttributeAccessIssue]
####################
# - Section: Stop/Start Kernel
####################
row = layout.row(align=True)
col = row.column(align=True)
col.enabled = bpy.app.online_access
_ = col.operator(OperatorType.StartJupyterKernel, text='Start Kernel')
_ = col.operator(OperatorType.StopJupyterKernel, text='Stop Kernel')
col = row.column(align=True)
col.scale_y = 2.0
col.progress(
text='Active' if jupyter_kernel.is_kernel_running() else 'Inactive',
factor=1.0 if jupyter_kernel.is_kernel_running() else 0.0,
)
####################
# - Section: Kernel Connection
####################
header, body = layout.panel(
PanelType.JupyterPanel + '_connectionfile',
default_closed=False,
)
header.label(text='Kernel Connection File')
if body is not None: # pyright: ignore[reportUnnecessaryComparison]
####################
# - Copy File Path | Parent Path | JSON Contents
####################
col = body.column(align=True)
# Copy P
row = col.row(align=True)
op = row.operator(
OperatorType.CopyKernelInfoToClipboard,
icon='COPYDOWN',
text='File Path',
)
op.value_to_copy = ( # pyright: ignore[reportAttributeAccessIssue]
str(jupyter_kernel.IPYKERNEL.path_connection_file)
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else ''
)
op = row.operator(
OperatorType.CopyKernelInfoToClipboard,
icon='COPYDOWN',
text='Parent Path',
)
op.value_to_copy = ( # pyright: ignore[reportAttributeAccessIssue]
str(jupyter_kernel.IPYKERNEL.path_connection_file.parent)
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else ''
)
op = col.operator(
OperatorType.CopyKernelInfoToClipboard,
icon='COPYDOWN',
text='File JSON Contents',
)
op.value_to_copy = ( # pyright: ignore[reportAttributeAccessIssue]
jupyter_kernel.IPYKERNEL.connection_info.json_str_with_key
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else ''
)
####################
# - Label w/File Path
####################
subheader, subbody = body.panel(
PanelType.JupyterPanel + '_connectionfile_path',
default_closed=True,
)
subheader.label(text='File Path')
if subbody is not None: # pyright: ignore[reportUnnecessaryComparison]
if (
jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
):
wrapped_path_connection_file_lines = textwrap.wrap(
str(jupyter_kernel.IPYKERNEL.path_connection_file),
width=width_chars,
)
box = subbody.box()
col = box.column(align=False)
col.scale_y = 0.5
for line in wrapped_path_connection_file_lines:
row = col.row(align=True)
row.alignment = 'CENTER'
row.label(text=line)
else:
box = subbody.box()
row = box.row(align=False)
row.alignment = 'CENTER'
row.label(text='Kernel Inactive')
####################
# - Label w/JSON Contents
####################
subheader, subbody = body.panel(
PanelType.JupyterPanel + '_connectionfile_json',
default_closed=True,
)
subheader.label(text='JSON Contents')
if subbody is not None: # pyright: ignore[reportUnnecessaryComparison]
if (
jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
):
wrapped_path_connection_file_lines = textwrap.wrap(
jupyter_kernel.IPYKERNEL.connection_info.json_str,
width=width_chars,
)
box = subbody.box()
col = box.column(align=False)
col.scale_y = 0.5
for line in wrapped_path_connection_file_lines:
row = col.row(align=True)
row.alignment = 'CENTER'
row.label(text=line)
else:
box = subbody.box()
row = box.row(align=False)
row.alignment = 'CENTER'
row.label(text='Kernel Inactive')
####################
# - Section: Kernel Networking
####################
header, body = layout.panel(
PanelType.JupyterPanel + '_ipports',
default_closed=True,
)
header.label(text='Kernel IP/Ports')
if body is not None: # pyright: ignore[reportUnnecessaryComparison]
####################
# - IP Address
####################
grid = body.grid_flow(
row_major=True, columns=2, even_rows=True, even_columns=True
)
grid.label(text='Kernel IP')
grid_section = grid.column()
grid_section_row = grid_section.row()
grid_section_row.alignment = 'RIGHT'
grid_section_row.label(
text=str(jupyter_kernel.IPYKERNEL.connection_info.ip)
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else '',
icon='QUESTION' if not jupyter_kernel.is_kernel_running() else 'NONE',
)
op = grid_section_row.operator(
OperatorType.CopyKernelInfoToClipboard, icon='COPYDOWN', text=''
)
op.value_to_copy = ( # pyright: ignore[reportAttributeAccessIssue]
str(jupyter_kernel.IPYKERNEL.connection_info.ip)
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else ''
)
####################
# - Ports
####################
grid = body.grid_flow(
row_major=True, columns=2, even_rows=True, even_columns=True
)
for label, value in zip(
[
'Shell',
'IOPub',
'Stdin',
'Control',
'Heartbeat',
],
[
jupyter_kernel.IPYKERNEL.connection_info.shell_port,
jupyter_kernel.IPYKERNEL.connection_info.iopub_port,
jupyter_kernel.IPYKERNEL.connection_info.stdin_port,
jupyter_kernel.IPYKERNEL.connection_info.control_port,
jupyter_kernel.IPYKERNEL.connection_info.hb_port,
]
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else 5 * ('',),
strict=True,
):
grid.label(text=label)
grid_section = grid.column()
grid_section_row = grid_section.row()
grid_section_row.alignment = 'RIGHT'
grid_section_row.label(
text=str(value),
icon='QUESTION'
if not jupyter_kernel.is_kernel_running()
else 'NONE',
)
op = grid_section_row.operator(
OperatorType.CopyKernelInfoToClipboard, icon='COPYDOWN', text=''
)
op.value_to_copy = str(value) # pyright: ignore[reportAttributeAccessIssue]
####################
# - Section: Kernel Security
####################
header, body = layout.panel(
PanelType.JupyterPanel + '_security',
default_closed=True,
)
header.label(text='Kernel Security')
if body is not None: # pyright: ignore[reportUnnecessaryComparison]
####################
# - Security
####################
grid = body.grid_flow(
row_major=True, columns=2, even_rows=True, even_columns=True
)
for label, value in zip(
[
'Key',
'Protocol',
'Sig. Algo',
],
[
jupyter_kernel.IPYKERNEL.connection_info.key,
jupyter_kernel.IPYKERNEL.connection_info.transport,
jupyter_kernel.IPYKERNEL.connection_info.signature_scheme,
]
if jupyter_kernel.IPYKERNEL is not None
and jupyter_kernel.IPYKERNEL.is_running
else 3 * ('',),
strict=True,
):
grid.label(text=label)
grid_section = grid.column()
grid_section_row = grid_section.row()
grid_section_row.alignment = 'RIGHT'
grid_section_row.label(
text=str(value),
icon='QUESTION'
if not jupyter_kernel.is_kernel_running()
else 'NONE',
)
# Special Case: Allow Copying Security-Sensitive Values
## An attacker with access to Blender's memory could get it anyway.
## Thus, there's no good reason to keep it from the operator.
if isinstance(value, pyd.SecretStr):
op = grid_section_row.operator(
OperatorType.CopyKernelInfoToClipboard, icon='COPYDOWN', text=''
)
op.value_to_copy = str(value.get_secret_value()) # pyright: ignore[reportAttributeAccessIssue]
else:
op = grid_section_row.operator(
OperatorType.CopyKernelInfoToClipboard, icon='COPYDOWN', text=''
)
op.value_to_copy = str(value) # pyright: ignore[reportAttributeAccessIssue]
|