Skip to content

Commit

Permalink
handling markdown cells as output
Browse files Browse the repository at this point in the history
  • Loading branch information
colombod committed Jan 4, 2023
1 parent 2cc83ad commit 86335ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
15 changes: 4 additions & 11 deletions src/Microsoft.DotNet.Interactive.Tests/ImportNotebookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,13 @@ public async Task It_imports_and_runs_well_known_polyglot_file_formats(string no
[Theory]
[InlineData(".ipynb")]
[InlineData(".dib")]
public async Task It_imports_and_forwards_markdown_content_to_frontend(string notebookExt)
public async Task It_produces_DisplayedValueProduced_events_for_markdown_cells(string notebookExt)
{
var frontend = new FakeKernel("vscode");
var markdownContent = new System.Text.StringBuilder();
frontend.RegisterCommandHandler((SendEditableCode command,KernelInvocationContext context) =>
{
markdownContent.Append(command.Code);
return Task.CompletedTask;
});

using var kernel = new CompositeKernel {
new CSharpKernel(),
new FSharpKernel(),
new HtmlKernel(),
frontend
}
.UseImportMagicCommand();

Expand All @@ -115,7 +108,7 @@ public async Task It_imports_and_forwards_markdown_content_to_frontend(string no
},
new InteractiveDocumentElement
{
Contents = "5+3",
Contents = "5+11",
KernelName = "markdown" //should not evaluate to 8
},
new InteractiveDocumentElement
Expand Down Expand Up @@ -150,6 +143,6 @@ public async Task It_imports_and_forwards_markdown_content_to_frontend(string no

await kernel.SubmitCodeAsync($"#!import {filePath}");

markdownContent.ToString().Should().Be("5+3");
events.Should().ContainSingle<DisplayedValueProduced>(v => v.FormattedValues.Any(f => f.MimeType == "text/markdown"));
}
}
21 changes: 3 additions & 18 deletions src/Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static TKernel UseImportMagicCommand<TKernel>(this TKernel kernel)
var command = new Command("#!import", "Imports and runs another notebook.");
command.AddArgument(new Argument<FileInfo>("notebookFile").ExistingOnly());
command.Handler = CommandHandler.Create(
async (FileInfo notebookFile, KernelInvocationContext _) =>
async (FileInfo notebookFile, KernelInvocationContext context) =>
{
var document = await InteractiveDocument.LoadAsync(
notebookFile,
Expand All @@ -104,23 +104,8 @@ public static TKernel UseImportMagicCommand<TKernel>(this TKernel kernel)
switch (element.KernelName?.ToLowerInvariant())
{
case "markdown":
var markDownKernel = kernel.RootKernel.FindKernelByName(element.KernelName);
if (markDownKernel is { })
{
await markDownKernel.SendAsync(new SubmitCode(element.Contents, element.KernelName));
}
else
{
var frontEndKernel = kernel.RootKernel.FindKernel(k => k.SupportsCommandType(typeof(SendEditableCode)));
if (frontEndKernel is { })
{
await frontEndKernel.SendAsync(new SendEditableCode(element.KernelName, element.Contents));
}
else
{
await kernel.RootKernel.SendAsync(new SubmitCode(element.Contents, element.KernelName));
}
}
var @event = new DisplayedValueProduced(element.Contents, context.Command, new[] { new FormattedValue("text/markdown", element.Contents) });
context.Publish(@event);
break;
default:
await kernel.RootKernel.SendAsync(new SubmitCode(element.Contents, element.KernelName));
Expand Down

0 comments on commit 86335ff

Please sign in to comment.