Skip to content

Commit

Permalink
Http request kernel and .NET 7 (jonsequitur#78)
Browse files Browse the repository at this point in the history
* .NET 7, package updates

* wip

* upgrading to .net 7 and latest interactive

* add kernel theme


revert test changes

* updated packages

* update assent file

* updated result file

Co-authored-by: Jon Sequeira <[email protected]>
  • Loading branch information
colombod and jonsequitur authored Dec 27, 2022
1 parent e05d7d5 commit 52661b0
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![REPL preview in C# and F#](https://user-images.githubusercontent.com/547415/121978422-02497600-cd3d-11eb-96c6-9725bda6dcaa.png)

This project is an experiment using [.NET Interactive](https://github.com/dotnet/interactive) and [Spectre.Console](https://github.com/spectreconsole) to create a polyglot .NET REPL for use on the command line.
This project is an experiment using [.NET Interactive / Polyglot Notebooks](https://github.com/dotnet/interactive) and [Spectre.Console](https://github.com/spectreconsole) to create a polyglot .NET REPL for use on the command line.

This is a personal project. Hopefully you enjoy it and find it useful. Contributions are welcome.

Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.300",
"version": "7.0.100",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
Expand All @@ -31,6 +34,9 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
Expand All @@ -54,6 +60,9 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
Expand All @@ -80,6 +89,9 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
Expand All @@ -105,13 +117,47 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div class=\"dni-plaintext\">An HTML display value</div>"
"<div class=\"dni-plaintext\"><pre>An HTML display value</pre></div><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
Expand All @@ -130,6 +176,9 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
Expand All @@ -155,6 +204,9 @@
"dotnet_repl_cellExecutionEndTime": "0001-01-01T00:00:00+00:00",
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
Expand All @@ -170,7 +222,38 @@
{
"data": {
"text/html": [
"<div class=\"dni-plaintext\">An HTML display value (in a multi-output cell)</div>"
"<div class=\"dni-plaintext\"><pre>An HTML display value (in a multi-output cell)</pre></div><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
Expand All @@ -196,6 +279,14 @@
"pygments_lexer": "csharp",
"version": "10.0"
},
"polyglot_notebook": {
"defaultKernelName": "csharp",
"items": [
{
"name": "csharp"
}
]
},
"dotnet_interactive": {
"defaultKernelName": "csharp",
"items": [
Expand Down
14 changes: 14 additions & 0 deletions src/dotnet-repl.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ public void When_there_are_no_results_it_doesnt_change_the_buffer()

buffer.Content.Should().Be("Console.Zzz");
}

[Fact(Skip = "WIP")]
public void It_moves_the_cursor_to_the_insertion_point_if_applicable()
{
var buffer = new LineBuffer("");
buffer.Insert("Console.WriteL");
buffer.MoveEnd();

var context = new LineEditorContext(buffer, ServiceProvider);
context.Execute(new CompletionCommand(AutoComplete.Next));

buffer.Content.Should().Be("Console.WriteLine()");
buffer.CursorPosition.Should().Be("Console.WriteLine(".Length);
}
}
6 changes: 3 additions & 3 deletions src/dotnet-repl.Tests/dotnet-repl.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>dotnet_repl.Tests</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Assent" Version="1.8.2" />
<PackageReference Include="fluentassertions" Version="6.7.0" />
<PackageReference Include="Assent" Version="2.1.0" />
<PackageReference Include="fluentassertions" Version="6.8.0" />
<PackageReference Include="microsoft.extensions.dependencyinjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="pocketlogger.for.xunit" Version="0.6.1">
Expand Down
2 changes: 2 additions & 0 deletions src/dotnet-repl/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Automation;
using Microsoft.DotNet.Interactive.Documents;
using Microsoft.DotNet.Interactive.Documents.Jupyter;
using Microsoft.DotNet.Interactive.HttpRequest;
using Pocket;
using Spectre.Console;

Expand Down Expand Up @@ -193,6 +194,7 @@ private static async Task<IDisposable> StartAsync(

var kernel = KernelBuilder.CreateKernel(options);


InteractiveDocument? notebook = default;

if (options.FileToRun is { } file)
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-repl/DocumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static KernelInfoCollection CreateKernelInfos(this CompositeKernel kernel
kernelAliases.Add(alias[2..]);
}

kernelInfos.Add(new KernelInfo(kernelChooser.Name[2..], kernelAliases));
kernelInfos.Add(new KernelInfo(kernelChooser.Name[2..], aliases: kernelAliases));
}

return kernelInfos;
Expand Down
13 changes: 10 additions & 3 deletions src/dotnet-repl/KernelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Browser;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.HttpRequest;
using Microsoft.DotNet.Interactive.PowerShell;
using Pocket;

Expand Down Expand Up @@ -82,8 +82,8 @@ public static CompositeKernel CreateKernel(StartupOptions? options = null)

var (htmlKernel, jsKernel) = Task.Run(async () =>
{
var htmlKernel = await playwrightConnector.CreateKernelAsync("html");
var jsKernel = await playwrightConnector.CreateKernelAsync("javascript");
var htmlKernel = await playwrightConnector.CreateKernelAsync("html", BrowserKernelLanguage.Html );
var jsKernel = await playwrightConnector.CreateKernelAsync("javascript", BrowserKernelLanguage.JavaScript);
return (htmlKernel, jsKernel);
}).Result;

Expand All @@ -100,6 +100,13 @@ public static CompositeKernel CreateKernel(StartupOptions? options = null)
typeof(RequestInput),
inputKernel.Name);


var httpRequestExtension = new HttpRequestKernelExtension();
Task.Run(async () =>
{
await httpRequestExtension.OnLoadAsync(compositeKernel);
}).Wait();

compositeKernel.DefaultKernelName = options.DefaultKernelName;

if (compositeKernel.DefaultKernelName == "fsharp")
Expand Down
2 changes: 0 additions & 2 deletions src/dotnet-repl/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using System.Collections.Generic;
using System.CommandLine;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Formatting;
using Recipes;
using Spectre.Console;
Expand Down
10 changes: 10 additions & 0 deletions src/dotnet-repl/KernelSpecificTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class KernelSpecificTheme : Theme
"javascript" => new JavaScriptTheme(),
"sql" => new SqlTheme(),
"html" => new HtmlTheme(),
"httpRequest" => new HttpRequestTheme(),
_ => null
};
}
Expand Down Expand Up @@ -65,6 +66,15 @@ public class PowerShellTheme : KernelSpecificTheme
public override string PromptText => "PS";
}

public class HttpRequestTheme : KernelSpecificTheme
{
public override Style AccentStyle => new(Color.Aqua);

public override string KernelDisplayName => "HTTP Request";

public override string PromptText => "HTTP";
}

public class JavaScriptTheme : KernelSpecificTheme
{
public override Style AccentStyle => new(Color.Yellow);
Expand Down
3 changes: 2 additions & 1 deletion src/dotnet-repl/MarkdownKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class MarkdownKernel :
Kernel,
IKernelCommandHandler<SubmitCode>
{
public MarkdownKernel() : base("markdown", "Markdown")
public MarkdownKernel() : base("markdown")
{
KernelInfo.LanguageName = "Markdown";
}

public Task HandleAsync(SubmitCode command, KernelInvocationContext context)
Expand Down
15 changes: 8 additions & 7 deletions src/dotnet-repl/dotnet-repl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand All @@ -25,12 +25,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="microsoft.dotnet.interactive" Version="1.0.0-beta.22519.6" />
<PackageReference Include="microsoft.dotnet.interactive.csharp" Version="1.0.0-beta.22519.6" />
<PackageReference Include="Microsoft.DotNet.Interactive.Documents" Version="1.0.0-beta.22519.6" />
<PackageReference Include="microsoft.dotnet.interactive.fsharp" Version="1.0.0-beta.22519.6" />
<PackageReference Include="Microsoft.Dotnet.Interactive.Browser" Version="1.0.0-beta.22519.6" />
<PackageReference Include="microsoft.dotnet.interactive.powershell" Version="1.0.0-beta.22519.6" />
<PackageReference Include="microsoft.dotnet.interactive" Version="1.0.0-beta.22606.2" />
<PackageReference Include="microsoft.dotnet.interactive.csharp" Version="1.0.0-beta.22606.2" />
<PackageReference Include="Microsoft.DotNet.Interactive.Documents" Version="1.0.0-beta.22606.2" />
<PackageReference Include="microsoft.dotnet.interactive.fsharp" Version="1.0.0-beta.22606.2" />
<PackageReference Include="Microsoft.Dotnet.Interactive.Browser" Version="1.0.0-beta.22606.2" />
<PackageReference Include="Microsoft.DotNet.Interactive.HttpRequest" Version="1.0.0-beta.22606.2" />
<PackageReference Include="microsoft.dotnet.interactive.powershell" Version="1.0.0-beta.22606.2" />
<PackageReference Include="radline" Version="0.6.0" />
<PackageReference Include="Serilog.Sinks.RollingFileAlternate" Version="2.0.9" />
<PackageReference Include="system.reactive" Version="5.0.0" />
Expand Down

0 comments on commit 52661b0

Please sign in to comment.