-
Notifications
You must be signed in to change notification settings - Fork 563
Add xplot support #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add xplot support #365
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a6e6325
Add basic project
akshita31 f094153
add new test
akshita31 0500c2e
Merge remote-tracking branch 'upstream/master' into add_xplot
akshita31 5ac320f
do nothing
akshita31 8797eb2
move the kernelextensions class
akshita31 3fcba8a
move from mls.agent to workspace server
akshita31 ed26a5a
add send async in the interactive project
akshita31 5d4fada
add reference to the xplot project
akshita31 f313f10
add test for the xplot kernel extension
akshita31 df5adc0
the test passes
akshita31 7a5f03a
remove unnecessary change
akshita31 7ee1c7e
split into multiple assertions
akshita31 53b20a9
make tests pass
akshita31 39bb1b4
move to mls.agent project
akshita31 29fad69
rename the function and add tests for GetChartHtml function
akshita31 64fa375
use htmlagility pack
akshita31 4b55732
merge upstream
akshita31 c9f90cb
change version
akshita31 2e3dc3f
move to separate class
akshita31 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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);"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
|
|
||
| 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 => | ||
akshita31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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\'}});") | ||
| )); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,4 +30,4 @@ public static int ToHttpStatusCode(this Exception exception) | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
XPlot.DotNet.Interactive.KernelExtensions/XPlot.DotNet.Interactive.KernelExtensions.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
55
XPlot.DotNet.Interactive.KernelExtensions/XPlotKernelExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) => | ||
| { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blank line