Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Microsoft.DotNet.Interactive
.ctor()
public System.Void Stamp(System.Uri uri)
public class FormattedValue
public static System.Collections.Generic.IReadOnlyList<FormattedValue> FromObject(System.Object value, System.String[] mimeTypes)
public static System.Collections.Generic.IReadOnlyList<FormattedValue> CreateManyFromObject(System.Object value, System.String[] mimeTypes)
public static FormattedValue CreateSingleFromObject(System.Object value, System.String mimeType = null)
.ctor(System.String mimeType, System.String value)
public System.String MimeType { get;}
public System.String Value { get;}
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.DotNet.Interactive.CSharp/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ Task IKernelCommandHandler<RequestValueInfos>.HandleAsync(RequestValueInfos comm
.GroupBy(v => v.Name)
.Select(g =>
{
var formattedValues = FormattedValue.FromObject(
var formattedValues = FormattedValue.CreateSingleFromObject(
g.LastOrDefault()?.Value,
command.MimeType);

return new KernelValueInfo(
g.Key,
formattedValues[0],
formattedValues,
g.Last().Type);
})
.ToArray() ??
Expand Down Expand Up @@ -348,7 +348,7 @@ await RunAsync(
{
if (ScriptState is not null && HasReturnValue)
{
var formattedValues = FormattedValue.FromObject(ScriptState.ReturnValue);
var formattedValues = FormattedValue.CreateManyFromObject(ScriptState.ReturnValue);
context.Publish(
new ReturnValueProduced(
ScriptState.ReturnValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ await kernel.SubmitCodeAsync($@"
#r ""{typeof(DataFrame).Assembly.Location}""
using Microsoft.Data.Analysis;
");
await kernel.SendAsync(new SendValue("frame", CreateDataFrame()));
var dataFrame = CreateDataFrame();

await kernel.SendAsync(new SendValue("frame", dataFrame, FormattedValue.CreateSingleFromObject(dataFrame)));

var result = await kernel.SubmitCodeAsync("#!linqify frame --show-code");

Expand Down Expand Up @@ -125,7 +127,9 @@ await kernel.SubmitCodeAsync($@"
using Microsoft.Data.Analysis;
");
var dataFrameVariableName = "myDataFrame";
await kernel.SendAsync(new SendValue(dataFrameVariableName, CreateDataFrame()));
var dataFrame = CreateDataFrame();

await kernel.SendAsync(new SendValue(dataFrameVariableName, dataFrame, FormattedValue.CreateSingleFromObject(dataFrame)));

var code = "#!linqify ";
var result = await kernel.SendAsync(new RequestCompletions(code, new LinePosition(0, code.Length)));
Expand All @@ -150,7 +154,9 @@ await kernel.SubmitCodeAsync($@"
#r ""{typeof(DataFrame).Assembly.Location}""
using Microsoft.Data.Analysis;
");
await kernel.SendAsync(new SendValue("frame", CreateDataFrame()));
var dataFrame = CreateDataFrame();

await kernel.SendAsync(new SendValue("frame", dataFrame, FormattedValue.CreateSingleFromObject(dataFrame)));

await kernel.SubmitCodeAsync(magicCommand);

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.DotNet.Interactive.FSharp/FSharpKernel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ type FSharpKernel () as this =
match result with
| Some(value) when value.ReflectionType <> typeof<unit> ->
let value = value.ReflectionValue
let formattedValues = FormattedValue.FromObject(value)
let formattedValues = FormattedValue.CreateManyFromObject(value)
context.Publish(ReturnValueProduced(value, codeSubmission, formattedValues))
Comment thread
shyamnamboodiripad marked this conversation as resolved.
| Some _
| None -> ()
Expand Down Expand Up @@ -377,7 +377,7 @@ type FSharpKernel () as this =
let valueInfos =
script.Value.Fsi.GetBoundValues()
|> List.filter (fun x -> x.Name <> "it") // don't report special variable `it`
|> List.map (fun x -> new KernelValueInfo(x.Name, FormattedValue.FromObject(x.Value.ReflectionValue, requestValueInfos.MimeType)[0], this.getValueType(x.Name)))
|> List.map (fun x -> new KernelValueInfo(x.Name, FormattedValue.CreateSingleFromObject(x.Value.ReflectionValue, requestValueInfos.MimeType), this.getValueType(x.Name)))
:> IReadOnlyCollection<KernelValueInfo>
context.Publish(new ValueInfosProduced(valueInfos, requestValueInfos))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ await Object.getPrototypeOf(async function() {{}}).constructor(
var executionCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
_tokenToInvocationContext[commandToken] = (context, executionCompletionSource);
IHtmlContent content = PocketViewTags.script[type: "text/javascript"](wrappedCode.ToHtmlContent());
var formattedValues = FormattedValue.FromObject(content);
var formattedValues = FormattedValue.CreateManyFromObject(content);
context.Publish(new DisplayedValueProduced(content, context.Command, formattedValues));
await executionCompletionSource.Task;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Task HandleAsync(RequestValue command, KernelInvocationContext context)
var valueProduced = new ValueProduced(
value,
command.Name,
FormattedValue.FromObject(value).FirstOrDefault(),
FormattedValue.CreateSingleFromObject(value),
command);
context.Publish(valueProduced);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<NamedTestSelector>
<TestName>Microsoft.DotNet.Interactive.Jupyter.Tests.ExecuteRequestHandlerTests.sends_InputRequest_message_when_submission_requests_user_input_in_powershell("Read-Host", "", "input-value")</TestName>
</NamedTestSelector>
<FixtureTestSelector>
<FixtureName>Microsoft.DotNet.Interactive.Jupyter.Tests.JupyterKernelVariableSharingTests</FixtureName>
</FixtureTestSelector>
<FixtureTestSelector>
<FixtureName>Microsoft.DotNet.Interactive.Jupyter.Tests.JupyterKernelCommandTests</FixtureName>
</FixtureTestSelector>
</IgnoredTests>
</Settings>
</ProjectConfiguration>
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive.Mermaid/MermaidKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Task IKernelCommandHandler<SubmitCode>.HandleAsync(SubmitCode command, KernelInv
Background = string.IsNullOrWhiteSpace(background) ? "white" : background
};

var formattedValues = FormattedValue.FromObject(markdown);
var formattedValues = FormattedValue.CreateManyFromObject(markdown);

context.Publish(
new DisplayedValueProduced(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ Task IKernelCommandHandler<RequestValueInfos>.HandleAsync(RequestValueInfos comm
.Where(v => !_suppressedValueInfoNames.Contains(v.Name))
.Select(v =>
{
var formattedValue = FormattedValue.FromObject(
var formattedValue = FormattedValue.CreateSingleFromObject(
v.Value,
command.MimeType)[0];
command.MimeType);

return new KernelValueInfo(
v.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public abstract class ToolsServiceKernel :
IKernelCommandHandler<RequestValue>,
IKernelCommandHandler<SendValue>
{

protected readonly Uri TempFileUri;
protected readonly TaskCompletionSource<ConnectionCompleteParams> ConnectionCompleted = new();
private Func<QueryCompleteParams, Task> _queryCompletionHandler;
Expand Down Expand Up @@ -374,12 +373,13 @@ public Task HandleAsync(RequestValueInfos command, KernelInvocationContext conte
{
var valueInfos = QueryResults.Keys.Select(key =>
{
var formattedValues = FormattedValue.FromObject(
var formattedValues = FormattedValue.CreateSingleFromObject(
_variables[key],
command.MimeType);

return new KernelValueInfo(
key, formattedValues[0],
key,
formattedValues,
type: typeof(IEnumerable<TabularDataResource>));
}).ToArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task String_is_rendered_as_specified_mime_type_DisplayAs(
var kernel = CreateKernel(language, openTestingNamespaces: true);

await kernel.FindKernelByName("csharp").As<CSharpKernel>()
.SendAsync(new SendValue(nameof(stringValue), stringValue));
.SendAsync(new SendValue(nameof(stringValue), stringValue, FormattedValue.CreateSingleFromObject(stringValue)));

var code = $"stringValue.DisplayAs(\"{mimeType}\");";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public async Task can_set_value_prompting_user()

composite.SetDefaultTargetKernelNameForCommand(typeof(RequestInput), composite.Name);


await composite.SendAsync(new SubmitCode("#!set --name x --value @input:input-please"));
var valueProduced = await kernel.RequestValueAsync("x");

Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.DotNet.Interactive/Commands/SendValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Text.Json.Serialization;
using Microsoft.DotNet.Interactive.Formatting;

namespace Microsoft.DotNet.Interactive.Commands;

Expand All @@ -14,6 +15,11 @@ public SendValue(
FormattedValue formattedValue = null,
string targetKernelName = null) : base(targetKernelName)
{
if (formattedValue is null)
{
formattedValue = FormattedValue.CreateSingleFromObject(value, JsonFormatter.MimeType);
Comment thread
shyamnamboodiripad marked this conversation as resolved.
}

if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(name));
Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.DotNet.Interactive/FormattedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ public FormattedValue(string mimeType, string value)

public string Value { get; }

public static IReadOnlyList<FormattedValue> FromObject(object value, params string[] mimeTypes)
public static FormattedValue CreateSingleFromObject(object value, string mimeType = null)
{
if (mimeType is null)
{
mimeType = Formatter.GetPreferredMimeTypesFor(value?.GetType()).First();
Comment thread
shyamnamboodiripad marked this conversation as resolved.
}

return new FormattedValue(mimeType, value.ToDisplayString(mimeType));
}

public static IReadOnlyList<FormattedValue> CreateManyFromObject(object value, params string[] mimeTypes)
{
if (mimeTypes is null ||
mimeTypes.Length == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ private static async Task DisplayValues(KernelInvocationContext context, bool de
new DisplayedValueProduced(
currentVariables,
context.Command,
FormattedValue.FromObject(currentVariables)));
FormattedValue.CreateManyFromObject(currentVariables)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static DisplayedValue Display(
{
var displayId = Guid.NewGuid().ToString();

var formattedValues = FormattedValue.FromObject(value, mimeTypes);
var formattedValues = FormattedValue.CreateManyFromObject(value, mimeTypes);

context.Publish(
new DisplayedValueProduced(
Expand Down