Skip to content

Commit

Permalink
add kernel name to Jupyter metadata (#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Oct 25, 2022
1 parent c457349 commit 54c3c8e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ public void notebook_metadata_default_language_is_honored_in_cells_without_langu
});
}

[Fact]
public void Default_language_can_be_specified_in_metadata()
[Theory]
[InlineData("C#", "csharp")]
[InlineData("F#", "fsharp")]
[InlineData("f#", "fsharp")]
[InlineData("PowerShell", "powershell")]
public void Metadata_default_kernel_name_is_based_on_specified_language(string languageName, string kernelName)
{
var originalDoc = new InteractiveDocument().WithJupyterMetadata("fsharp");
var originalDoc = new InteractiveDocument().WithJupyterMetadata(languageName);

var parsedDoc = Notebook.Parse(originalDoc.ToJupyterJson());

parsedDoc.GetDefaultKernelName()
.Should()
.Be("fsharp");
.Should()
.Be(kernelName);
}


[Fact]
public void missing_metadata_defaults_to_csharp_kernel()
{
Expand Down Expand Up @@ -983,6 +986,14 @@ public void serialized_notebook_has_appropriate_metadata()
name = "C#",
pygments_lexer = "csharp",
version = "10.0"
},
dotnet_interactive = new
{
defaultKernelName = "csharp",
items = new object[]
{
new { name = "csharp" }
}
}
})));
jupyter["nbformat"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ internal static bool TryGetKernelInfoFromMetadata(
return true;
}

if (metadata.TryGetValue("dotnet_interactive", out var dotnetInteractiveObj))
{
if (dotnetInteractiveObj is IDictionary<string, object> dotnetInteractiveDict)
{
kernelInfo = new();

if (dotnetInteractiveDict.TryGetValue("defaultKernelName", out var nameObj) &&
nameObj is string name)
{
kernelInfo.DefaultKernelName = name;
}

return true;

// FIX: (TryGetKernelInfoFromMetadata) add items
}
}

// check for .ipynb / Jupyter metadata
if (metadata.TryGetValue("kernelspec", out var kernelspecObj))
{
Expand All @@ -242,7 +260,6 @@ internal static bool TryGetKernelInfoFromMetadata(
}
}
}

}

// check if a KernelInfoCollection was directly serialized into the metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,38 @@ public static InteractiveDocument WithJupyterMetadata(
this InteractiveDocument document,
string language = "C#")
{
var (moniker, langVersion, fileExtension) =
language switch
var (kernelName, canonicalLanguageName, langVersion, fileExtension) =
language.ToLowerInvariant() switch
{
"C#" or "csharp" => ("csharp", "10.0", ".cs"),
"F#" or "fsharp" => ("fsharp", "6.0", ".fs"),
"PowerShell" or "pwsh" => ("powershell", "7.0", ".ps1"),
"c#" or "csharp" => ("csharp","C#", "10.0", ".cs"),
"f#" or "fsharp" => ("fsharp","F#", "6.0", ".fs"),
"powershell" or "pwsh" => ("powershell", "PowerShell", "7.0", ".ps1"),
_ => throw new ArgumentException($"Unrecognized language: {language}")
};

document.Metadata.GetOrAdd("kernelspec", _ => new Dictionary<string, object>())
.MergeWith(new Dictionary<string, object>
{
["display_name"] = $".NET ({language})",
["language"] = language,
["name"] = $".net-{moniker}"
["display_name"] = $".NET ({canonicalLanguageName})",
["language"] = canonicalLanguageName,
["name"] = $".net-{kernelName}"
});

document.Metadata.GetOrAdd("language_info", _ => new Dictionary<string, object>())
.MergeWith(new Dictionary<string, object>
{
["file_extension"] = fileExtension,
["mimetype"] = $"text/x-{moniker}",
["name"] = language,
["pygments_lexer"] = moniker,
["mimetype"] = $"text/x-{kernelName}",
["name"] = canonicalLanguageName,
["pygments_lexer"] = kernelName,
["version"] = langVersion
});

var kernelInfos = document.Metadata.GetOrAdd("dotnet_interactive", _ => new KernelInfoCollection());

kernelInfos.DefaultKernelName = kernelName;
kernelInfos.Add(new(kernelName));

return document;
}
}
9 changes: 6 additions & 3 deletions src/Microsoft.DotNet.Interactive/Kernel.Static.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ public static DisplayedValue display(
/// <returns>The user input value.</returns>
public static async Task<string> GetInputAsync(string prompt = "", string typeHint = "text")
{
return await GetInputAsync(prompt, false);
return await GetInputAsync(prompt, false, typeHint);
}

public static async Task<string> GetPasswordAsync(string prompt = "")
{
return await GetInputAsync(prompt, true);
}

private static async Task<string> GetInputAsync(string prompt, bool isPassword)
private static async Task<string> GetInputAsync(string prompt, bool isPassword, string typeHint = "text")
{
var command = new RequestInput(prompt, isPassword ? "password" : default);
var command = new RequestInput(
prompt,
isPassword ? "password" : default,
inputTypeHint: typeHint);

var results = await Root.SendAsync(command, CancellationToken.None);

Expand Down

0 comments on commit 54c3c8e

Please sign in to comment.