Skip to content

Commit 6039f81

Browse files
committed
fix dot language kernel
1 parent c950b39 commit 6039f81

5 files changed

Lines changed: 60 additions & 56 deletions

File tree

src/DotLanguage.InteractiveExtension/ChooseDotLanguageKernelDirective.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/DotLanguage.InteractiveExtension/DotLanguageKernel.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Html;
77
using Microsoft.DotNet.Interactive;
88
using Microsoft.DotNet.Interactive.Commands;
9+
using Microsoft.DotNet.Interactive.Directives;
910
using Microsoft.DotNet.Interactive.Http;
1011

1112
namespace DotLanguage.InteractiveExtension;
@@ -15,7 +16,6 @@ public class DotLanguageKernel : Kernel,
1516
{
1617
private readonly string _cacheBuster;
1718

18-
private ChooseDotLanguageKernelDirective? _chooseKernelDirective;
1919

2020
public DotLanguageKernel() : base("dot")
2121
{
@@ -24,25 +24,49 @@ public DotLanguageKernel() : base("dot")
2424
_cacheBuster = Guid.NewGuid().ToString("N");
2525
}
2626

27+
public override KernelSpecifierDirective KernelSpecifierDirective
28+
{
29+
get
30+
{
31+
var directive = base.KernelSpecifierDirective;
32+
33+
directive.Parameters.Add(new("--width"));
34+
directive.Parameters.Add(new("--height"));
35+
var layoutEngineParameter = new KernelDirectiveParameter("--layout-engine");
36+
layoutEngineParameter.AddCompletions(() => Enum.GetNames(typeof(LayoutEngine)));
37+
directive.Parameters.Add(layoutEngineParameter);
38+
39+
return directive;
40+
}
41+
}
42+
2743
public Task HandleAsync(SubmitCode command, KernelInvocationContext context)
2844
{
29-
var width = "100%";
30-
var height = "600px";
3145
var layoutEngine = LayoutEngine.dot;
32-
if (_chooseKernelDirective is { } chooser)
46+
command.Parameters.TryGetValue("--width", out var width);
47+
if (string.IsNullOrWhiteSpace(width))
48+
{
49+
width = "100%";
50+
}
51+
command.Parameters.TryGetValue("--height", out var height);
52+
if (string.IsNullOrWhiteSpace(height))
3353
{
34-
width = command.KernelChooserParseResult?.GetValueForOption(chooser.WidthOption);
35-
height = command.KernelChooserParseResult?.GetValueForOption(chooser.HeightOption);
36-
layoutEngine = command.KernelChooserParseResult?.GetValueForOption(chooser.LayoutEngineOption) ?? LayoutEngine.dot;
54+
height = "600px";
3755
}
56+
command.Parameters.TryGetValue("--layout-engine", out var layoutEngineString);
57+
if (string.IsNullOrWhiteSpace(layoutEngineString))
58+
{
59+
layoutEngineString = "dot";
60+
}
61+
layoutEngine = Enum.Parse<LayoutEngine>(layoutEngineString, true);
62+
3863

3964
var code = GenerateHtml(command.Code, new Uri("https://cdn.jsdelivr.net/npm/@hpcc-js/wasm@1.16.1/dist/index.min.js", UriKind.Absolute), new Uri("https://cdn.jsdelivr.net/npm/@hpcc-js/wasm@1.16.1/dist", UriKind.Absolute), "1.16.1", _cacheBuster, width, height, layoutEngine);
4065
context.Display(code);
4166
return Task.CompletedTask;
4267

4368
}
4469

45-
public override ChooseKernelDirective ChooseKernelDirective => _chooseKernelDirective ??= new ChooseDotLanguageKernelDirective(this);
4670

4771
private IHtmlContent GenerateHtml(string commandCode, Uri libraryUri, Uri wasmFolder, string? libraryVersion,
4872
string cacheBuster, string? width, string? height, LayoutEngine layoutEngine)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace DotLanguage.InteractiveExtension;
2+
3+
public enum LayoutEngine
4+
{
5+
circo,
6+
dot,
7+
fdp,
8+
sfdp,
9+
neato,
10+
osage,
11+
patchwork,
12+
twopi,
13+
}

src/DuckDB.InteractiveExtension.Tests/KernelExtensionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public async Task It_can_handle_multiple_statements()
164164
using var _ = CreateInMemoryDuckDB(out var connectionString);
165165

166166
var result = await kernel.SubmitCodeAsync(
167-
$"#!connect duckdb --kernel-name mydb \"{connectionString}\"");
167+
$"#!connect duckdb --kernel-name mydb --connection-string \"{connectionString}\"");
168168

169169
result.Events
170170
.Should()

src/DuckDB.InteractiveExtension/DuckDBKernel.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.CommandLine;
4-
using System.CommandLine.Parsing;
53
using System.Data;
64
using System.Linq;
75
using System.Threading.Tasks;
86
using DuckDB.NET.Data;
97

108
using Microsoft.DotNet.Interactive;
119
using Microsoft.DotNet.Interactive.Commands;
10+
using Microsoft.DotNet.Interactive.Directives;
1211
using Microsoft.DotNet.Interactive.Events;
1312
using Microsoft.DotNet.Interactive.Formatting.TabularData;
1413
using Microsoft.DotNet.Interactive.ValueSharing;
@@ -33,11 +32,23 @@ public class DuckDBKernel : Kernel,
3332

3433
public DuckDBKernel(string name, DuckDBConnection connection) : base(name)
3534
{
36-
KernelInfo.LanguageName = "SQL";
35+
KernelInfo.LanguageName = "KQL";
3736
_connection = connection;
3837
RegisterForDisposal(_connection);
3938
}
4039

40+
public override KernelSpecifierDirective KernelSpecifierDirective
41+
{
42+
get
43+
{
44+
var directive = base.KernelSpecifierDirective;
45+
46+
directive.Parameters.Add(new("--name"));
47+
48+
return directive;
49+
}
50+
}
51+
4152
public async Task HandleAsync(SubmitCode submitCode, KernelInvocationContext context)
4253
{
4354
if (_connection.State != ConnectionState.Open)

0 commit comments

Comments
 (0)