Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Microsoft.DotNet.Interactive
public static class KernelExtensions
public static Kernel FindKernelByName(System.String name)
public static System.Collections.Generic.IEnumerable<Kernel> FindKernels(System.Func<Kernel,System.Boolean> predicate)
public static System.Threading.Tasks.Task LoadAndRunInteractiveDocument(System.IO.FileInfo file)
public static System.Threading.Tasks.Task LoadAndRunInteractiveDocument(System.IO.FileInfo file, Microsoft.DotNet.Interactive.Commands.KernelCommand parentCommand = null)
public static System.Collections.Generic.IEnumerable<Kernel> Subkernels(System.Boolean recursive = False)
public static System.Collections.Generic.IEnumerable<Kernel> SubkernelsAndSelf(System.Boolean recursive = False)
public static System.Threading.Tasks.Task<KernelCommandResult> SubmitCodeAsync(System.String code)
Expand Down
44 changes: 44 additions & 0 deletions src/Microsoft.DotNet.Interactive.Tests/ImportNotebookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.App;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Documents;
using Microsoft.DotNet.Interactive.Documents.Jupyter;
Expand Down Expand Up @@ -144,4 +145,47 @@ public async Task It_produces_DisplayedValueProduced_events_for_markdown_cells(s

events.Should().ContainSingle<DisplayedValueProduced>(v => v.FormattedValues.Any(f => f.MimeType == "text/markdown"));
}

[Theory]
[InlineData(".ipynb")]
[InlineData(".dib")]
public async Task It_load_packages_from_imports(string notebookExt)
{

using var kernel = new CompositeKernel {
new CSharpKernel().UseNugetDirective()
.UseKernelHelpers()
.UseWho()
.UseValueSharing(),
new FSharpKernel(),
new HtmlKernel(),
}
.UseImportMagicCommand();

var document = new InteractiveDocument
{
new InteractiveDocumentElement
{
Contents = "#r \"nuget: Microsoft.DotNet.Interactive.AIUtilities, 1.0.0-beta.23517.1\"",
KernelName = "csharp"
}
};

var notebookContents = notebookExt switch
{
".ipynb" => document.ToJupyterJson(),
".dib" => document.ToCodeSubmissionContent(),
_ => throw new InvalidOperationException($"Unrecognized extension for a notebook: {notebookExt}")
};

var filePath = $@".\testnotebook{notebookExt}";

await File.WriteAllTextAsync(filePath, notebookContents);

using var events = kernel.KernelEvents.ToSubscribedList();

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

events.Should().NotContainErrors();
}
}
20 changes: 16 additions & 4 deletions src/Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public static TKernel UseImportMagicCommand<TKernel>(this TKernel kernel)
command.SetHandler(async ctx =>
{
var file = ctx.ParseResult.GetValueForArgument(fileArg);
await LoadAndRunInteractiveDocument(kernel, file);
var currentInvocationContext = ctx.GetService<KernelInvocationContext>();
await LoadAndRunInteractiveDocument(kernel, file, currentInvocationContext.Command);
});

kernel.AddDirective(command);
Expand All @@ -110,7 +111,8 @@ public static TKernel UseImportMagicCommand<TKernel>(this TKernel kernel)

public static async Task LoadAndRunInteractiveDocument(
this Kernel kernel,
FileInfo file)
FileInfo file,
KernelCommand parentCommand = null)
{
var kernelInfoCollection = CreateKernelInfos(kernel.RootKernel as CompositeKernel);
var document = await InteractiveDocument.LoadAsync(
Expand All @@ -124,11 +126,21 @@ public static async Task LoadAndRunInteractiveDocument(
StringComparer.OrdinalIgnoreCase.Equals(kernelInfo.LanguageName, "markdown"))
{
var formattedValue = new FormattedValue("text/markdown", element.Contents);
await kernel.SendAsync(new DisplayValue(formattedValue));
var displayValue = new DisplayValue(formattedValue);
if (parentCommand is not null)
{
displayValue.SetParent(parentCommand);
}
await kernel.SendAsync(displayValue);
}
else
{
await kernel.RootKernel.SendAsync(new SubmitCode(element.Contents, element.KernelName));
var submitCode = new SubmitCode(element.Contents, element.KernelName);
if (parentCommand is not null)
{
submitCode.SetParent(parentCommand);
}
await kernel.RootKernel.SendAsync(submitCode);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/Microsoft.DotNet.Interactive/KernelInvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
Expand Down Expand Up @@ -203,7 +204,7 @@ void StopPublishingMainCommandEvents()

void StopPublishingChildCommandEvents()
{
if (_childCommands.TryGetValue(command, out var events) &&
if (TryGetChildCommandEvents(command, out var events) &&
!events.IsDisposed)
{
events.OnCompleted();
Expand Down Expand Up @@ -250,7 +251,7 @@ public void Publish(KernelEvent @event, bool publishOnAmbientContextOnly)
@event.StampRoutingSlipAndLog(HandlingKernel.KernelInfo.Uri);
}

if (!publishOnAmbientContextOnly && _childCommands.TryGetValue(command, out var events))
if (!publishOnAmbientContextOnly && TryGetChildCommandEvents(command, out var events))
{
events.OnNext(@event);
}
Expand All @@ -275,6 +276,11 @@ public void Publish(KernelEvent @event, bool publishOnAmbientContextOnly)

public KernelCommandResult Result { get; }

private bool TryGetChildCommandEvents(KernelCommand command, out ReplaySubject<KernelEvent> events)
{
return _childCommands.TryGetValue(command, out events);
}

internal KernelCommandResult ResultFor(KernelCommand command)
{
if (command.Equals(Command))
Expand All @@ -283,7 +289,7 @@ internal KernelCommandResult ResultFor(KernelCommand command)
}
else
{
var events = _childCommands[command];
TryGetChildCommandEvents(command, out var events);
var result = new KernelCommandResult(command);
using var _ = events.Subscribe(result.AddEvent);
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-interactive/PackageDirectoryExtensionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private async Task LoadFromExtensionDibScriptAsync(

context.Display(logMessage);

await kernel.LoadAndRunInteractiveDocument(extensionFile);
await kernel.LoadAndRunInteractiveDocument(extensionFile, context.Command);

op.Succeed();
}
Expand Down