这是indexloc提供的服务,不要输入任何密码
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
7 changes: 7 additions & 0 deletions DotNetTry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactiv
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactive", "Microsoft.DotNet.Interactive\Microsoft.DotNet.Interactive.csproj", "{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XPlot.DotNet.Interactive.KernelExtensions", "XPlot.DotNet.Interactive.KernelExtensions\XPlot.DotNet.Interactive.KernelExtensions.csproj", "{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -181,6 +183,10 @@ Global
{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5}.Release|Any CPU.Build.0 = Release|Any CPU
{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -214,6 +220,7 @@ Global
{91902AAC-F4E9-4648-AC6B-4E4A722D3CC5} = {8192FEAD-BCE6-4E62-97E5-2E9EA884BD71}
{113A4166-5734-4F6E-B609-D6CF42679399} = {6EE8F484-DFA2-4F0F-939F-400CE78DFAC2}
{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5} = {6EE8F484-DFA2-4F0F-939F-400CE78DFAC2}
{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C} = {6EE8F484-DFA2-4F0F-939F-400CE78DFAC2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D6CD99BA-B16B-4570-8910-225CBDFFA3AD}
Expand Down
63 changes: 63 additions & 0 deletions MLS.Agent.Tests/GetChartHtmlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 FluentAssertions;
using HtmlAgilityPack;
using System.Linq;
using XPlot.DotNet.Interactive.KernelExtensions;
using XPlot.Plotly;
using Xunit;

namespace MLS.Agent.Tests
{
public partial class XplotKernelExtensionTests
{
public class GetChartHtmlTests
{
[Fact]
public void Returns_the_html_with_div()
{
var extension = new XPlotKernelExtension();
var html = extension.GetChartHtml(new PlotlyChart());
var document = new HtmlDocument();
document.LoadHtml(html);

document.DocumentNode.SelectSingleNode("//div").InnerHtml.Should().NotBeNull();
document.DocumentNode.SelectSingleNode("//div").Id.Should().NotBeNullOrEmpty();
}

[Fact]
public void Returns_the_html_with_script_containing_require_config()
{
var extension = new XPlotKernelExtension();
var html = extension.GetChartHtml(new PlotlyChart());
var document = new HtmlDocument();
document.LoadHtml(html);

document.DocumentNode.SelectSingleNode("//script").InnerHtml.Should().Contain("require.config({paths:{plotly:\'https://cdn.plot.ly/plotly-latest.min\'}});");
}

[Fact]
public void Returns_the_html_with_script_containing_require_plotly_and_call_to_new_plot_function()
{
var extension = new XPlotKernelExtension();
var html = extension.GetChartHtml(new PlotlyChart());
var document = new HtmlDocument();
document.LoadHtml(html);

var divId = document.DocumentNode.SelectSingleNode("//div").Id;
document.DocumentNode
.SelectSingleNode("//script")
.InnerHtml.Split("\n")
.Select(item => item.Trim())
.Where(item => !string.IsNullOrWhiteSpace(item))
.Should()
.ContainInOrder(@"require(['plotly'], function(Plotly) {",
"var data = null;",
@"var layout = """";",
$"Plotly.newPlot('{divId}', data, layout);");
}
}

}
}
2 changes: 1 addition & 1 deletion MLS.Agent.Tests/MLS.Agent.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.7.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.8" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
<PackageReference Include="HttpResponseMessageAssertions" Version="0.1.8580001">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
44 changes: 44 additions & 0 deletions MLS.Agent.Tests/XplotKernelExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 FluentAssertions;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using System.Linq;
using System.Threading.Tasks;
using WorkspaceServer.Tests.Kernel;
using Xunit;
using Xunit.Abstractions;

namespace MLS.Agent.Tests
{
public partial class XplotKernelExtensionTests : CSharpKernelTestBase
{

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line

public XplotKernelExtensionTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public async Task When_a_chart_is_returned_the_value_produced_has_html_with_the_require_config_call()
{
var kernel = CreateKernel();
kernel.UseXplot();

await kernel.SendAsync(new SubmitCode("using XPlot.Plotly;"));
await kernel.SendAsync(new SubmitCode("new PlotlyChart()"));

KernelEvents
.ValuesOnly()
.OfType<ValueProduced>()
.Should().
ContainSingle(valueProduced =>
valueProduced.FormattedValues.Any(formattedValue =>
formattedValue.MimeType == "text/html" &&
formattedValue.Value.ToString().Contains("require([\'plotly\'], function(Plotly)")
&& formattedValue.Value.ToString().Contains("require.config({paths:{plotly:\'https://cdn.plot.ly/plotly-latest.min\'}});")
));
}
}
}
1 change: 1 addition & 0 deletions MLS.Agent/CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ private static CompositeKernel CreateKernel()
.UseNugetDirective()
.UseExtendDirective()
.UseKernelHelpers()
.UseXplot()
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion MLS.Agent/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public static int ToHttpStatusCode(this Exception exception)
}
}
}
}
}
20 changes: 20 additions & 0 deletions MLS.Agent/KernelExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 Clockwise;
using Microsoft.DotNet.Interactive;
using XPlot.DotNet.Interactive.KernelExtensions;

namespace MLS.Agent
{
public static class KernelExtensions
{
public static T UseXplot<T>(this T kernel)
where T : KernelBase
{
var extension = new XPlotKernelExtension();
extension.OnLoadAsync(kernel).Wait();
return kernel;
}
}
}
1 change: 1 addition & 0 deletions MLS.Agent/MLS.Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<ProjectReference Include="..\MLS.Blazor\MLS.Blazor.csproj" />
<ProjectReference Include="..\MLS.Repositories\MLS.Repositories.csproj" />
<ProjectReference Include="..\WorkspaceServer\WorkspaceServer.csproj" />
<ProjectReference Include="..\XPlot.DotNet.Interactive.KernelExtensions\XPlot.DotNet.Interactive.KernelExtensions.csproj" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Assent" Version="1.3.1" />
<PackageReference Include="FluentAssertions" Version="5.7.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.8" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
<PackageReference Include="Microsoft.Reactive.Testing" Version="4.1.5" />
<PackageReference Include="JsonSerializationExtensions" Version="0.1.8580001">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Assent" Version="1.3.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.8" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
<PackageReference Include="FluentAssertions" Version="5.7.0" />
<PackageReference Include="Markdig" Version="0.17.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion WorkspaceServer.Tests/Kernel/TimestampedExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace WorkspaceServer.Tests.Kernel
{
internal static class TimestampedExtensions
public static class TimestampedExtensions
{
public static IEnumerable<T> ValuesOnly<T>(this IEnumerable<Timestamped<T>> source)
{
Expand Down
3 changes: 2 additions & 1 deletion WorkspaceServer/Kernel/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ private void SetupScriptOptions()
typeof(Task<>).Assembly,
typeof(IKernel).Assembly,
typeof(CSharpKernel).Assembly,
typeof(PocketView).Assembly);
typeof(PocketView).Assembly,
typeof(XPlot.Plotly.PlotlyChart).Assembly);
}

private (bool shouldExecute, string completeSubmission) IsBufferACompleteSubmission(string input)
Expand Down
1 change: 1 addition & 0 deletions WorkspaceServer/WorkspaceServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="XPlot.Plotly" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
<PackageReference Include="XPlot.Plotly" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive\Microsoft.DotNet.Interactive.csproj" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions XPlot.DotNet.Interactive.KernelExtensions/XPlotKernelExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using HtmlAgilityPack;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Rendering;
using System;
using System.Text;
using System.Threading.Tasks;
using XPlot.Plotly;
using static Microsoft.DotNet.Interactive.Rendering.PocketViewTags;

namespace XPlot.DotNet.Interactive.KernelExtensions
{
public class XPlotKernelExtension : IKernelExtension
{
public Task OnLoadAsync(IKernel kernel)
{
Formatter<PlotlyChart>.Register((chart, writer) =>
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird indenting

writer.Write(GetChartHtml(chart));
}, "text/html");

return Task.CompletedTask;
}

public string GetChartHtml(PlotlyChart chart)
{
var document = new HtmlDocument();
document.LoadHtml(chart.GetInlineHtml());

var divNode = document.DocumentNode.SelectSingleNode("//div");
var scriptNode = document.DocumentNode.SelectSingleNode("//script");

var newHtmlDocument = new HtmlDocument();
newHtmlDocument.DocumentNode.ChildNodes.Add(divNode);
newHtmlDocument.DocumentNode.ChildNodes.Add(GetScriptNodeWithRequire(scriptNode));

return newHtmlDocument.DocumentNode.WriteContentTo();
}

private static HtmlNode GetScriptNodeWithRequire(HtmlNode scriptNode)
{
var newScript = new StringBuilder();

newScript.AppendLine("<script>");
newScript.Append(@"
require.config({paths:{plotly:'https://cdn.plot.ly/plotly-latest.min'}});
require(['plotly'], function(Plotly) {
");

newScript.Append(scriptNode.InnerText);
newScript.AppendLine(@"});");
newScript.AppendLine("</script>");
return HtmlNode.CreateNode(newScript.ToString());
}
}
}