dotnet / interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
MIT License
2.85k stars 381 forks source link

Documentation on creating/using Formatters, using SandDance in PowerShell #1320

Open dfinke opened 3 years ago

dfinke commented 3 years ago

Is your feature request related to a problem? Please describe. There is no documentation on how to use these features in a PowerShell cell.

Describe the solution you'd like I would like to be able to create and use formatters in PowerShell. The RESTBook for example displays Postman type results of response, headers etc. Building custom formatters in/for PS would be advantageous.

SandDance

The ExploreWithSandDance extension method concept would be great to leverage in PS. Enabling it to be wrapped like the early xPlotly interface may be the way to do that. Possibly writing C# that is used in the kernel? Preferably if it be very useful if could be implemented as a PS function.

Describe alternatives you've considered

For the ExploreWithSandDance, creating datasets in PS and then sharing that variable to C# for use with SandDance, a cognitive shift, but doable.

Documentation on how to go about this would be great.

jonsequitur commented 3 years ago

Agreed. Here's a quick overview in case it helps fill in gaps for anyone reading this.

The formatters are based on .NET types. For any .NET type, a custom formatter can be registered. SandDance should be usable right now from PowerShell if you create an instance of SandDanceDataExplorer. The ExploreWithSandDance extension methods on various types (IEnumerable<T>, JsonDocument, JsonElement, IDataView, and TabularDataResource) get you an instance of SandDanceDataExplorer, give or take various transformations. The rendering of SandDanceDataExplorer (and NteractDataExplorer, and others to come) into the onscreen visualization happens via this one catch-all formatter registration:

Formatter.Register<DataExplorer<TData>>((explorer, writer) =>
{
    explorer.ToHtml().WriteTo(writer, HtmlEncoder.Default);
}, HtmlFormatter.MimeType);

The registration APIs might not be ideal for PowerShell, so we should look at how to make sure they're usable there, and also see if there are improvements we can make specifically for PowerShell.

jhoneill commented 3 years ago

We have several of these cases where the only way to load the components is with something like #r "nuget:Microsoft.DotNet.Interactive.ExtensionLab,*-*" This gives us Mermaid, and the nteract and SandDance explorers. or #r "nuget: xplot.plotly.interactive" In the plotly case opening a notebook (e.g. this one https://github.com/jhoneill/Notebooks/blob/master/notebooks/Plotly.ipynb ) renders charts that have previously been plotted and loading via nuget is only needed to plot new ones. I have a little plotly module (https://github.com/jhoneill/Notebooks/blob/master/notebooks/plotly.psm1) which now only works after nuget has done its thing. It doesn't need to download but the only way to tell the kernels it its there seems to be through nuget. If would be helpful if one could load the required parts from a PowerShell module. In the Mermaid case (https://github.com/jhoneill/Notebooks/blob/master/notebooks/Mermaid.ipynb) nothing renders before nuget has done it's thing and then pre-existing charts light up.

It would be good to (a) be able to add [Microsoft.DotNet.Interactive.ExtensionLab.SandDanceDataExplorer] / SandDanceExplorerExtensions / SandDanceKernelExtension and NteractDataExplorer/kernelExternsion or for plotly [xplot.plotly.*] in PowerShell without doing the nuget step in a different kernel and (b) have some more documentation for how we call the different data explorerers.