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.9k stars 389 forks source link

example notebook is not working - is some config required? #2793

Open housten opened 1 year ago

housten commented 1 year ago

Discussed in https://github.com/dotnet/interactive/discussions/2756

Originally posted by **housten** February 21, 2023 I have newly installed polyglot in VS Code and the .net 7 sdk. As a notebook newbie, I am trying to get to grips with how it works so I am using some of the example files, but I don't get what I expect back. Is some configuration required or do I have false expectations or maybe I am looking in the wrong place? Here are some examples from [html.jpynb](https://github.com/dotnet/interactive/blob/main/samples/notebooks/powershell/Docs/HTML.ipynb): - for example, the first item, 'Displaying rich output', I expected it to render the html, but I get the literal html (tags and all, I mean) in the display cell after the code cell. Whereas when I run the equivalent using the magic command in the lower part of the page, it worked as I expected. - However, the javascript alert does not result in an alert window with either of the example syntaxes. The first one resulted in ""No formatter is registered for MIME type application/javascript. Out-Display: " while nothing appeared to happen when running the magic word one. - Regarding the custom svg MIME type I expected to get the picture but I get the xml in the display cell
jonsequitur commented 1 year ago

No config is needed here, but it looks like the PowerShell API is missing a concept that the other formatters gained, which is a distinction between the following APIs, which are available in C# and F#.

Out-Display corresponds to the former. We don't have a PowerShell equivalent to the latter.

housten commented 1 year ago

That means that the example notebook probably never worked? Seems a little strange really that someone would publish it without it working, I mean in a jupyter notebook, you write it and you try it out... But maybe stuff changed since that was written. It looks like the writer expected out-display to work the same way as other Powershell out-* commands. For example, Out-Gridview takes a collection of objects and displays it in a grid directly without other formatting or display calls required. It doesn't make sense from a Powershell perspective to have display and displayAs separated.

jonsequitur commented 1 year ago

The example notebook worked at one time, but the meaning of Out-Display was changed and the sample wasn't updated. It originally meant "format this object, of any type, as a string meant to be rendered as HTML, unless the object is a string", which powers things like this:

image

(Note that text/html is the default MIME type in these examples.)

We soon realized the special treatment of strings was confusing and left a gap: when you wanted to produce a visually correct rendering of a string, it needs to be HTML-encoded. We resolved this by saying that when you display a string, it should HTML-encode the string, just like the content is encoded when you render an object graph. What you're seeing here is the result of that encoding:

image

Here's the actual string emitted by the above Out-Display call:

<div class="dni-plaintext"><pre>&lt;b&gt;hello!&lt;/b&gt;</pre></div><style>
.dni-code-hint {
    font-style: italic;
    overflow: hidden;
    white-space: nowrap;
}
.dni-treeview {
    white-space: nowrap;
}
.dni-treeview td {
    vertical-align: top;
    text-align: start;
}
details.dni-treeview {
    padding-left: 1em;
}
table td {
    text-align: start;
}
table tr { 
    vertical-align: top; 
    margin: 0em 0px;
}
table tr td pre 
{ 
    vertical-align: top !important; 
    margin: 0em 0px !important;
} 
table th {
    text-align: start;
}
</style>

Hope that helps clarify what's going on.

housten commented 1 year ago

Thanks for the clarification! Is there a way to output a rendered html string instead of the html-encoded string using powershell?

jonsequitur commented 1 year ago

You can wrap it in a type that signals it shouldn't be re-encoded, e.g. the HtmlString class or anything implementing IHtmlContent. This method is a convenience for that: https://github.com/dotnet/interactive/blob/main/src/Microsoft.DotNet.Interactive/Kernel.Static.cs#L34