This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 465
Expand file tree
/
Copy pathConnectJupyterKernelDirective.cs
More file actions
142 lines (117 loc) · 4.75 KB
/
Copy pathConnectJupyterKernelDirective.cs
File metadata and controls
142 lines (117 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Jupyter.Connection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Directives;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.CodeAnalysis.Tags;
namespace Microsoft.DotNet.Interactive.Jupyter;
public class ConnectJupyterKernelDirective : ConnectKernelDirective<ConnectJupyterKernel>
{
private readonly List<IJupyterKernelConnectionOptions> _connectionCreators = new();
private KeyValuePair<int, IEnumerable<CompletionItem>> _mruKernelSpecSuggestions;
public ConnectJupyterKernelDirective() : base("jupyter", "Connects a Jupyter kernel as a .NET Interactive subkernel.")
{
KernelSpecNameParameter.AddCompletions(GetKernelSpecsCompletionsAsync);
Parameters.Add(KernelSpecNameParameter);
Parameters.Add(InitScriptParameter);
}
public KernelDirectiveParameter KernelSpecNameParameter { get; } =
new("--kernel-spec", "The kernel spec to connect to")
{
Required = true
};
public KernelDirectiveParameter InitScriptParameter { get; } =
new("--init-script", "Script to run on kernel initialization");
public ConnectJupyterKernelDirective AddConnectionOptions(IJupyterKernelConnectionOptions connectionOptions)
{
foreach (var option in connectionOptions.GetParameters())
{
Parameters.Add(option);
}
_connectionCreators.Add(connectionOptions);
return this;
}
public override async Task<IEnumerable<Kernel>> ConnectKernelsAsync(
ConnectJupyterKernel connectCommand,
KernelInvocationContext context)
{
context.DisplayAs(
"The `#!connect jupyter` feature is in preview. Please report any feedback or issues at https://github.com/dotnet/interactive/issues/new/choose.",
"text/markdown");
var kernelSpecName = connectCommand.KernelSpecName;
var initScript = connectCommand.InitScript;
var connection = GetJupyterConnection(connectCommand);
if (connection is null)
{
throw new InvalidOperationException("No supported connection options were specified");
}
var connector = new JupyterKernelConnector(connection, kernelSpecName, initScript);
var localName = connectCommand.ConnectedKernelName;
var kernel = await connector.CreateKernelAsync(localName);
if (connection is IDisposable disposableConnection)
{
kernel.RegisterForDisposal(disposableConnection);
}
return new[] { kernel };
}
private IJupyterConnection GetJupyterConnection(ConnectJupyterKernel connectCommand)
{
foreach (var connectionOptions in _connectionCreators)
{
var connection = connectionOptions.GetConnection(connectCommand);
if (connection is not null)
{
return connection;
}
}
return null;
}
private async Task<IEnumerable<CompletionItem>> GetKernelSpecsCompletionsAsync(KernelDirectiveCompletionContext ctx)
{
var hash = GetParseResultHash(ctx);
if (_mruKernelSpecSuggestions.Key == hash)
{
return _mruKernelSpecSuggestions.Value;
}
IEnumerable<CompletionItem> completions = [];
var connection = GetJupyterConnection(new ConnectJupyterKernel(""));
using (connection as IDisposable)
{
completions = await GetKernelSpecsCompletionsAsync(connection);
}
if (completions is not null)
{
_mruKernelSpecSuggestions = new(hash, completions);
}
return completions;
}
private async Task<IEnumerable<CompletionItem>> GetKernelSpecsCompletionsAsync(IJupyterConnection connection)
{
var completions = new List<CompletionItem>();
var specs = await connection.GetKernelSpecsAsync();
if (specs is not null)
{
foreach (var s in specs)
{
completions.Add(new CompletionItem(s.Name, WellKnownTags.Parameter));
}
}
return completions;
}
private int GetParseResultHash(KernelDirectiveCompletionContext context)
{
string values = string.Empty;
foreach (var option in Parameters)
{
if (option != KernelSpecNameParameter)
{
// FIX: (GetParseResultHash) values += parseResult.GetValueForOption(option);
}
}
return (values).GetHashCode();
}
}