Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 066d6aa

Browse files
Chris GranadeChris Granade
Chris Granade
authored and
Chris Granade
committed
Merged PR 1192: Adopted @anpaz's suggestion for a serialization workflow.
This PR abstracts execution result and display data formatting into a new interface `IDisplaySerializer` that formats objects for displaying to clients. To demonstrate functionality that will be needed with IQ#, this PR also modifies the `moon-kernel` example to serialize all appropriate Lua objects to JSON. Using this mechanism, clients have full access to the structured output of kernels that opt-in to this behavior, making for a pretty generic way of interoperating different languages together.
1 parent d311662 commit 066d6aa

9 files changed

+315
-56
lines changed

Diff for: examples/echo-kernel/Echo Kernel.ipynb

+44-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
]
1313
},
1414
"execution_count": 1,
15-
"metadata": {},
15+
"metadata": {
16+
"text/plain": null
17+
},
1618
"output_type": "execute_result"
1719
}
1820
],
@@ -22,40 +24,73 @@
2224
},
2325
{
2426
"cell_type": "code",
25-
"execution_count": null,
27+
"execution_count": 2,
2628
"metadata": {},
27-
"outputs": [],
29+
"outputs": [
30+
{
31+
"data": {
32+
"text/plain": [
33+
"bar"
34+
]
35+
},
36+
"execution_count": 2,
37+
"metadata": {
38+
"text/plain": null
39+
},
40+
"output_type": "execute_result"
41+
}
42+
],
2843
"source": [
2944
"bar"
3045
]
3146
},
3247
{
3348
"cell_type": "code",
34-
"execution_count": null,
49+
"execution_count": 3,
3550
"metadata": {},
36-
"outputs": [],
51+
"outputs": [
52+
{
53+
"data": {
54+
"text/plain": [
55+
"baz"
56+
]
57+
},
58+
"execution_count": 3,
59+
"metadata": {
60+
"text/plain": null
61+
},
62+
"output_type": "execute_result"
63+
}
64+
],
3765
"source": [
3866
"baz"
3967
]
4068
},
4169
{
4270
"cell_type": "code",
43-
"execution_count": 2,
71+
"execution_count": 4,
4472
"metadata": {},
4573
"outputs": [
4674
{
4775
"data": {
4876
"text/html": [
4977
"<ul><li>%dne</li>\n",
78+
"<li>bar</li>\n",
79+
"<li>baz</li>\n",
5080
"<li>%history</li></ul>"
5181
],
5282
"text/plain": [
5383
"%dne\n",
84+
"bar\n",
85+
"baz\n",
5486
"%history"
5587
]
5688
},
57-
"execution_count": 2,
58-
"metadata": {},
89+
"execution_count": 4,
90+
"metadata": {
91+
"text/html": null,
92+
"text/plain": null
93+
},
5994
"output_type": "execute_result"
6095
}
6196
],
@@ -66,9 +101,7 @@
66101
{
67102
"cell_type": "code",
68103
"execution_count": null,
69-
"metadata": {
70-
"collapsed": true
71-
},
104+
"metadata": {},
72105
"outputs": [],
73106
"source": []
74107
}

Diff for: examples/echo-kernel/KernelProperties.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal static class Constants
2626
// The MIME type of the language implemented by the kernel.
2727
// This property is used mainly for providing "plain" downloads from
2828
// Jupyter clients.
29-
LanguageMimeType = "text/plain",
29+
LanguageMimeType = MimeTypes.PlainText,
3030
// The file extension for source files written in the language
3131
// implemented by the kernel.
3232
// This property is used mainly for providing "plain" downloads from

Diff for: examples/moon-kernel/DynValueConverter.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using MoonSharp.Interpreter;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
6+
7+
namespace Microsoft.Jupyter.Core
8+
{
9+
public class DynValueConverter : JsonConverter<DynValue>
10+
{
11+
public override DynValue ReadJson(JsonReader reader, Type objectType, DynValue existingValue, bool hasExistingValue, JsonSerializer serializer)
12+
{
13+
throw new NotImplementedException();
14+
}
15+
16+
public override void WriteJson(JsonWriter writer, DynValue value, JsonSerializer serializer)
17+
{
18+
var obj = value.ToObject();
19+
JToken token;
20+
switch (obj)
21+
{
22+
case MoonSharp.Interpreter.Closure closure:
23+
token = JToken.FromObject(new Dictionary<string, object>
24+
{
25+
["@closure"] = closure.ToString()
26+
});
27+
token.WriteTo(writer);
28+
return;
29+
30+
default:
31+
// See https://github.com/JamesNK/Newtonsoft.Json/issues/386#issuecomment-421161191
32+
// for why this works to pass through.
33+
token = JToken.FromObject(obj);
34+
token.WriteTo(writer);
35+
return;
36+
}
37+
}
38+
}
39+
}

Diff for: examples/moon-kernel/MoonEngine.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using MoonSharp.Interpreter.REPL;
99
using Microsoft.Extensions.Options;
1010
using Microsoft.Extensions.Logging;
11+
using Newtonsoft.Json;
1112

1213
namespace Microsoft.Jupyter.Core
1314
{
@@ -23,6 +24,9 @@ public MoonEngine(
2324
ILogger<MoonEngine> logger
2425
) : base(shell, context, logger)
2526
{
27+
RegisterJsonEncoder(
28+
new DynValueConverter()
29+
);
2630
var script = new Script();
2731
script.Options.DebugPrint = str => printFn?.Invoke(str);
2832
interp = new ReplInterpreter(script);
@@ -42,7 +46,7 @@ public override ExecutionResult ExecuteMundane(string input, Action<string> stdo
4246
}
4347
else if (result.ToObject() != null)
4448
{
45-
return result.ToDebugPrintString().ToExecutionResult();
49+
return result.ToExecutionResult();
4650
}
4751
else
4852
{

Diff for: examples/moon-kernel/MoonScript Kernel.ipynb

+34-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
"outputs": [
88
{
99
"data": {
10+
"application/json": "4.0",
1011
"text/plain": [
1112
"4"
1213
]
1314
},
1415
"execution_count": 1,
15-
"metadata": {},
16+
"metadata": {
17+
"application/json": null,
18+
"text/plain": null
19+
},
1620
"output_type": "execute_result"
1721
}
1822
],
@@ -27,12 +31,16 @@
2731
"outputs": [
2832
{
2933
"data": {
34+
"application/json": "120.0",
3035
"text/plain": [
3136
"120"
3237
]
3338
},
3439
"execution_count": 2,
35-
"metadata": {},
40+
"metadata": {
41+
"application/json": null,
42+
"text/plain": null
43+
},
3644
"output_type": "execute_result"
3745
}
3846
],
@@ -56,12 +64,16 @@
5664
"outputs": [
5765
{
5866
"data": {
67+
"application/json": "{\"@closure\":\"MoonSharp.Interpreter.Closure\"}",
5968
"text/plain": [
60-
"function: 00000099"
69+
"(Function 00000074)"
6170
]
6271
},
6372
"execution_count": 3,
64-
"metadata": {},
73+
"metadata": {
74+
"application/json": null,
75+
"text/plain": null
76+
},
6577
"output_type": "execute_result"
6678
}
6779
],
@@ -76,6 +88,7 @@
7688
"outputs": [
7789
{
7890
"data": {
91+
"application/json": "[\"return 1 + 3\",\"-- defines a factorial function\\n function fact (n)\\n if (n == 0) then\\n return 1\\n else\\n return n*fact(n - 1)\\n end\\n end\\n\\n return fact(5)\",\"return fact\",\"%history\"]",
7992
"text/html": [
8093
"<ul><li>return 1 + 3</li>\n",
8194
"<li>-- defines a factorial function\n",
@@ -108,7 +121,11 @@
108121
]
109122
},
110123
"execution_count": 4,
111-
"metadata": {},
124+
"metadata": {
125+
"application/json": null,
126+
"text/html": null,
127+
"text/plain": null
128+
},
112129
"output_type": "execute_result"
113130
}
114131
],
@@ -123,12 +140,16 @@
123140
"outputs": [
124141
{
125142
"data": {
143+
"application/json": "720.0",
126144
"text/plain": [
127145
"720"
128146
]
129147
},
130148
"execution_count": 5,
131-
"metadata": {},
149+
"metadata": {
150+
"application/json": null,
151+
"text/plain": null
152+
},
132153
"output_type": "execute_result"
133154
}
134155
],
@@ -152,6 +173,13 @@
152173
"source": [
153174
"print('Hello, world')"
154175
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": []
155183
}
156184
],
157185
"metadata": {

0 commit comments

Comments
 (0)