Open housten opened 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#.
Display()
: Format an object
as a string
using the formatter associated with its type and a specified (or default) MIME type.
DisplayAs()
Render a string
as the specified MIME type.
Out-Display
corresponds to the former. We don't have a PowerShell equivalent to the latter.
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.
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:
(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:
Here's the actual string emitted by the above Out-Display
call:
<div class="dni-plaintext"><pre><b>hello!</b></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.
Thanks for the clarification! Is there a way to output a rendered html string instead of the html-encoded string using powershell?
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
Discussed in https://github.com/dotnet/interactive/discussions/2756