Skip to content
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

text/plain+summary formatter #2614

Merged
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
43 changes: 29 additions & 14 deletions DEVELOPER-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This project depends on .NET 7.0. Before working on the project, check that the
- [Prerequisites for .NET on Linux](https://learn.microsoft.com/en-us/dotnet/core/install/linux?tabs=net70#dependencies)
- [Prerequisites for .NET on macOS](https://learn.microsoft.com/en-us/dotnet/core/install/macos?tabs=net70#dependencies)

## First build
### First build

The first build must be executed on the command line (see below for details), but subsequent builds can be done directly within Visual Studio / Visual Studio Code.

Expand All @@ -34,49 +34,63 @@ You can also build this project on the command line by running the following scr

Windows:

```console
> .\build.cmd
```

Linux or macOS:

```console
$ ./build.sh
```

You can both build and run the tests for the project by running the scripts with the following option:

Windows:

```console
> .\build.cmd -test
```

Linux or macOS:

```console
$ ./build.sh --test
```

For additional options, you can get help as follows:

Windows:

```console
> .\build.cmd -help
```

Linux or macOS:

```console
$ ./build.sh --help
```

### Install your local build

To build and then install your developer build of the `dotnet-interactive` global tool, you can run the PowerShell script
To build and then install your developer build of the `dotnet-interactive` tool, you can run the PowerShell script

```console
pwsh src/dotnet-interactive/build-and-install-dotnet-interactive.ps1
```

Powershell for .NET Core is required. This will uninstall any previous version of `dotnet-interactive` you might have installed.
PowerShell for .NET Core is required. This will uninstall any previous version of `dotnet-interactive` you might have installed.

### Arcade build system

.NET Interactive is built with the support of the [Arcade](https://github.com/dotnet/arcade) build system. The Arcade tools provide common infrastructure for building, testing, and publishing .NET Foundation projects. This build system is not required for local development work, but using it will provide a higher fidelity

If you prefer a development environment that's more consistent with the out-of-the-box .NET experience, you can set the environment variable `DisableArcade` to `1`.

## Building the Visual Studio Code extension
## Building the Polyglot Notebooks extension for Visual Studio Code

In order to build the .NET Interactive Notebooks Visual Studio Code extension, please follow the instructions below. Note that it's not necessary to use a local build of `dotnet-interactive` in order to work on the Visual Studio Code extension.
In order to build the Polyglot Notebooks extension for Visual Studio Code, please follow the instructions below. Note that it's not necessary to use a local build of `dotnet-interactive` in order to work on the Visual Studio Code extension.

### Prerequisites

Expand Down Expand Up @@ -114,7 +128,7 @@ If you've made changes to `dotnet-interactive` and want to try them out with Vis

b. In the file that opens, add the following:

```js
```json
"dotnet-interactive.kernelTransportArgs": [
"{dotnet_path}",
"/PATH/TO/REPO/ROOT/artifacts/bin/dotnet-interactive/Debug/net7.0/Microsoft.DotNet.Interactive.App.dll",
Expand All @@ -140,27 +154,27 @@ If you've made changes to `dotnet-interactive` and want to try them out with Vis

5. To revert back to the original settings, delete the settings added in step 2 above and restart VS Code.

### Use a local build of a `dotnet-interactive` extension
### Use a local build of a Polyglot Notebooks extension

If you've made changes to one of the `dotnet-interactive` extensions and want to use them locally, follow these steps:
If you've made changes to one of the Polyglot Notebooks extensions and want to use them locally, follow these steps:

1. Run `build.cmd -pack`/`./build.sh --pack` to create the Nuget packages for the extensions

2. Ensure that there aren't any kernels running for the extension in question. It's generally best to close all Notebooks opened in VS Code to accomplish this.

3. Run the `.NET Interactive: Create a new blank notebook` command in VS Code. Select `.dib` or `.ipynb` as the extension and any language as default.
3. Run the `Polyglot Notebook: Create a new blank notebook` command in VS Code. Select `.dib` or `.ipynb` as the extension and any language as default.

4. Save the Notebook anywhere you like
4. Save the notebook anywhere you like

5. Run the `.NET Interactive: Restart the current Notebook's kernel` command
5. Run the `Polyglot Notebook: Restart the current Notebook's kernel` command

6. In the first code cell, paste this code
- In the FolderName, give the path to the nuget cache. This should be `%userprofile%\.nuget\packages` on Windows and `~/.nuget/packages` on Mac/Linux
- Also replace `EXTENSIONNAME` with the name of the extension (e.g. `microsoft.dotnet.interactive.sqlserver`)
- On the #i line, fill in the path to the `dotnet-interactive` repo root
- On the #r line, use the same `EXTENSIONNAME` above, and then look in the `artifacts\packages\Debug\Shipping` folder for the package you're using and get the version number from the name. e.g. a package named `Microsoft.DotNet.Interactive.SqlServer.1.0.0-dev.nupkg` would result in this line `#r "nuget: Microsoft.DotNet.Interactive.SqlServer, 1.0.0-dev"`
- On the `#i` line, fill in the path to the `dotnet-interactive` repo root
- On the `#r` line, use the same `EXTENSIONNAME` above, and then look in the `artifacts\packages\Debug\Shipping` folder for the package you're using and get the version number from the name. e.g. a package named `Microsoft.DotNet.Interactive.SqlServer.1.0.0-dev.nupkg` would result in this line `#r "nuget: Microsoft.DotNet.Interactive.SqlServer, 1.0.0-dev"`

```text
```powershell
#!powershell

$FolderName = "\PATH\TO\NUGET\CACHE\packages\microsoft.dotnet.interactive.<EXTENSIONNAME>"
Expand All @@ -182,4 +196,5 @@ else

7. Run the cell
- If you get an error about access being denied, ensure that all other Notebooks are closed and then restart the kernel again as in step 5

8. Now, use the kernel as you normally would. You should see your local changes being used by the extension.
1 change: 1 addition & 0 deletions eng/perf-tests/perf-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
<RootNamespace>PerfTests</RootNamespace>
<NoWarn>$(NoWarn);CS8032</NoWarn><!-- An instance of analyzer...cannot be created -->
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,13 @@ Microsoft.DotNet.Interactive.Formatting
public static ITypeFormatter GetPreferredFormatterFor<T>()
public static System.Void WriteStartProperty(FormatContext context)
public class PlainTextFormatter<T> : TypeFormatter<T>, ITypeFormatter<T>, ITypeFormatter
public static PlainTextFormatter<T> CreateForAnyEnumerable(System.Boolean includeInternals = False)
public static PlainTextFormatter<T> CreateForAnyObject(System.Boolean includeInternals = False)
public static PlainTextFormatter<T> CreateForMembers(Expression<Func<T,System.Object>> members)
.ctor(FormatDelegate<T> format)
.ctor(Action<T,FormatContext> format)
.ctor(Func<T,System.String> format)
public System.String MimeType { get;}
public System.Boolean Format(T value, FormatContext context)
public static class PlainTextSummaryFormatter
public static ITypeFormatter GetPreferredFormatterFor(System.Type type)
public class PocketView : System.Dynamic.DynamicObject, Microsoft.AspNetCore.Html.IHtmlContent, System.Dynamic.IDynamicMetaObjectProvider
public static System.Object Transform(System.Action<HtmlTag,System.Object> transform)
public static System.Object Transform(System.Action<HtmlTag,System.Object,FormatContext> transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Microsoft.DotNet.Interactive
.ctor()
public System.Void Stamp(System.Uri uri)
public class FormattedValue
public static System.Collections.Generic.IReadOnlyCollection<FormattedValue> FromObject(System.Object value, System.String[] mimeTypes)
public static System.Collections.Generic.IReadOnlyList<FormattedValue> FromObject(System.Object value, System.String[] mimeTypes)
.ctor(System.String mimeType, System.String value)
public System.String MimeType { get;}
public System.String Value { get;}
Expand Down Expand Up @@ -429,9 +429,10 @@ Microsoft.DotNet.Interactive.Commands
public System.String MimeType { get;}
public System.String Name { get;}
public class RequestValueInfos : KernelCommand
.ctor(System.String targetKernelName = null)
.ctor(System.String targetKernelName = null, System.String mimeType = text/plain+summary)
public System.String MimeType { get;}
public class SendEditableCode : KernelCommand
.ctor(System.String kernelName, System.String code, System.String targetKernelName = vscode)
.ctor(System.String kernelName, System.String code, System.String targetKernelName = null)
public System.String Code { get;}
public System.String KernelName { get;}
public class SendValue : KernelCommand
Expand Down Expand Up @@ -944,10 +945,6 @@ Microsoft.DotNet.Interactive.ValueSharing
public System.Collections.Generic.IReadOnlyCollection<System.String> PreferredMimeTypes { get; set;}
public System.Type Type { get;}
public System.String TypeName { get; set;}
public class KernelValues, System.Collections.Generic.IEnumerable<KernelValue>, System.Collections.IEnumerable
.ctor(System.Collections.Generic.IEnumerable<KernelValue> variables, System.Boolean detailed)
public System.Boolean Detailed { get;}
public System.Collections.Generic.IEnumerator<KernelValue> GetEnumerator()
System
public static class DisplayExtensions
public static Microsoft.DotNet.Interactive.DisplayedValue Display(String[] mimeTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Microsoft.DotNet.Interactive.CSharp
public System.Collections.Generic.IEnumerable<Microsoft.DotNet.Interactive.ResolvedPackageReference> ResolvedPackageReferences { get;}
public System.Collections.Generic.IEnumerable<System.String> RestoreSources { get;}
public Microsoft.CodeAnalysis.Scripting.ScriptState ScriptState { get;}
public System.Collections.Generic.IReadOnlyCollection<Microsoft.DotNet.Interactive.ValueSharing.KernelValueInfo> GetValueInfos()
public System.Threading.Tasks.Task<System.Boolean> IsCompleteSubmissionAsync(System.String code)
public System.Threading.Tasks.Task SetValueAsync(System.String name, System.Object value, System.Type declaredType)
public System.Boolean TryGetValue<T>(System.String name, ref T& value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ Microsoft.DotNet.Interactive.SqlServer
public System.Threading.Tasks.Task ConnectAsync()
protected System.String CreateVariableDeclaration(System.String name, System.Object value)
protected System.Collections.Generic.Dictionary<System.String,System.Collections.Generic.IReadOnlyCollection<Microsoft.DotNet.Interactive.Formatting.TabularData.TabularDataResource>> get_QueryResults()
public System.Collections.Generic.IReadOnlyCollection<Microsoft.DotNet.Interactive.ValueSharing.KernelValueInfo> GetValueInfos()
public System.Threading.Tasks.Task HandleAsync(Microsoft.DotNet.Interactive.Commands.SubmitCode command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
public System.Threading.Tasks.Task HandleAsync(Microsoft.DotNet.Interactive.Commands.RequestCompletions command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
public System.Threading.Tasks.Task HandleAsync(Microsoft.DotNet.Interactive.Commands.RequestValue command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ Microsoft.DotNet.Interactive.SqlServer
public System.Threading.Tasks.Task ConnectAsync()
protected System.String CreateVariableDeclaration(System.String name, System.Object value)
protected System.Collections.Generic.Dictionary<System.String,System.Collections.Generic.IReadOnlyCollection<Microsoft.DotNet.Interactive.Formatting.TabularData.TabularDataResource>> get_QueryResults()
public System.Collections.Generic.IReadOnlyCollection<Microsoft.DotNet.Interactive.ValueSharing.KernelValueInfo> GetValueInfos()
public System.Threading.Tasks.Task HandleAsync(Microsoft.DotNet.Interactive.Commands.SubmitCode command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
public System.Threading.Tasks.Task HandleAsync(Microsoft.DotNet.Interactive.Commands.RequestCompletions command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
public System.Threading.Tasks.Task HandleAsync(Microsoft.DotNet.Interactive.Commands.RequestValue command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Microsoft.DotNet.Interactive.PowerShell
public System.Func<System.String,Microsoft.DotNet.Interactive.PasswordString> ReadPassword { get; set;}
public System.Void AddAccelerator(System.String name, System.Type type)
public System.Void AddModulePath(System.String modulePath)
public System.Collections.Generic.IReadOnlyCollection<Microsoft.DotNet.Interactive.ValueSharing.KernelValueInfo> GetValueInfos()
public System.Boolean TryGetValue<T>(System.String name, ref T& value)
public static class PowerShellKernelExtensions
public static PowerShellKernel UseProfiles()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Task<Kernel> CreateKernelAsync(string kernelName, BrowserKernelLanguage l
{
context.Publish(new ValueInfosProduced(new KernelValueInfo[]
{
new(":root", new FormattedValue(PlainTextFormatter.MimeType,"document root"),typeof(ILocator))
new(":root", new FormattedValue(PlainTextFormatter.MimeType, "document root"), typeof(ILocator))
}, request));
return Task.CompletedTask;
});
Expand Down
26 changes: 18 additions & 8 deletions src/Microsoft.DotNet.Interactive.CSharp/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,25 @@ public Task<bool> IsCompleteSubmissionAsync(string code)

Task IKernelCommandHandler<RequestValueInfos>.HandleAsync(RequestValueInfos command, KernelInvocationContext context)
{
var valueInfos = GetValueInfos();
var valueInfos =
ScriptState?.Variables
.GroupBy(v => v.Name)
.Select(g =>
{
var formattedValues = FormattedValue.FromObject(
g.LastOrDefault()?.Value,
command.MimeType);

return new KernelValueInfo(
g.Key,
formattedValues[0],
g.Last().Type);
})
.ToArray() ??
Array.Empty<KernelValueInfo>();

context.Publish(new ValueInfosProduced(valueInfos, command));

return Task.CompletedTask;
}

Expand All @@ -135,13 +152,6 @@ Task IKernelCommandHandler<RequestValue>.HandleAsync(RequestValue command, Kerne
return Task.CompletedTask;
}

public IReadOnlyCollection<KernelValueInfo> GetValueInfos() =>
ScriptState?.Variables
.GroupBy(v => v.Name)
.Select(g => new KernelValueInfo(g.Key, new FormattedValue(PlainTextFormatter.MimeType, g.LastOrDefault().Value?.ToDisplayString(PlainTextFormatter.MimeType)), g.Last().Type))
.ToArray() ??
Array.Empty<KernelValueInfo>();

public bool TryGetValue<T>(
string name,
out T value)
Expand Down
Loading