Skip to content

Commit 9e18fed

Browse files
committed
if the only non-nil result returned by REPL is an image.Image or display.Data, publish it with "execute_result" Jupyter message, not with "display_data"
1 parent bd0dab5 commit 9e18fed

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

image.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import (
44
"bytes"
5+
"fmt"
56
"image"
67
"image/png"
7-
"log"
88
)
99

1010
// Image converts an image.Image to DisplayData containing PNG []byte,
@@ -69,43 +69,35 @@ func (receipt *msgReceipt) PublishImage(img image.Image) error {
6969
return receipt.PublishDisplayData(data)
7070
}
7171

72-
// if vals[] contain a single non-nil value which is an image.Image
73-
// or a display.Data, publishImageOrDisplayData sends a "display_data"
74-
// broadcast message for such value, then returns nil to avoid overloading
75-
// the front-end with huge amounts of output text:
76-
// fmt.Sprint(val) is often very large for an image and other multimedia data.
77-
func (receipt *msgReceipt) PublishImageOrDisplayData(vals []interface{}) []interface{} {
72+
// if vals[] contain a single non-nil value which is an image.Image,
73+
// convert it to Data and return it.
74+
// if instead the single non-nil value is a Data, return it.
75+
// otherwise return MakeData("text/plain", fmt.Sprint(vals...))
76+
func renderResults(vals []interface{}) Data {
7877
var nilcount int
79-
var data interface{}
78+
var obj interface{}
8079
for _, val := range vals {
81-
switch obj := val.(type) {
80+
switch val.(type) {
8281
case image.Image, Data:
83-
data = obj
82+
obj = val
8483
case nil:
8584
nilcount++
8685
}
8786
}
88-
if data != nil && nilcount == len(vals)-1 {
89-
switch obj := data.(type) {
87+
if obj != nil && nilcount == len(vals)-1 {
88+
switch val := obj.(type) {
9089
case image.Image:
91-
err := receipt.PublishImage(obj)
92-
if err != nil {
93-
log.Printf("Error publishing image.Image: %v\n", err)
94-
} else {
95-
nilcount++
90+
data, err := image0(val)
91+
if err == nil {
92+
return data
9693
}
9794
case Data:
98-
err := receipt.PublishDisplayData(obj)
99-
if err != nil {
100-
log.Printf("Error publishing Data: %v\n", err)
101-
} else {
102-
nilcount++
103-
}
95+
return val
10496
}
10597
}
10698
if nilcount == len(vals) {
107-
// if all values are nil, return empty slice
108-
return nil
99+
// if all values are nil, return empty Data
100+
return Data{}
109101
}
110-
return vals
102+
return MakeData("text/plain", fmt.Sprint(vals...))
111103
}

kernel.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,15 @@ func handleExecuteRequest(ir *interp.Interp, receipt msgReceipt) error {
398398
writersWG.Wait()
399399

400400
if executionErr == nil {
401-
// if one or more value is image.Image or DisplayData, display it instead
402-
vals = receipt.PublishImageOrDisplayData(vals)
401+
// if the only non-nil value is image.Image or Data, render it
402+
data := renderResults(vals)
403403

404404
content["status"] = "ok"
405405
content["user_expressions"] = make(map[string]string)
406406

407-
if !silent && vals != nil {
407+
if !silent && len(data.Data) != 0 {
408408
// Publish the result of the execution.
409-
if err := receipt.PublishExecutionResult(ExecCounter, fmt.Sprint(vals...)); err != nil {
409+
if err := receipt.PublishExecutionResult(ExecCounter, data); err != nil {
410410
log.Printf("Error publishing execution result: %v\n", err)
411411
}
412412
}

messages.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,19 @@ func (receipt *msgReceipt) PublishExecutionInput(execCount int, code string) err
242242
}
243243

244244
// PublishExecuteResult publishes the result of the `execCount` execution as a string.
245-
func (receipt *msgReceipt) PublishExecutionResult(execCount int, output string) error {
245+
func (receipt *msgReceipt) PublishExecutionResult(execCount int, data Data) error {
246+
if data.Metadata == nil {
247+
data.Metadata = make(BundledMIMEData)
248+
}
246249
return receipt.Publish("execute_result",
247250
struct {
248251
ExecCount int `json:"execution_count"`
249252
Data BundledMIMEData `json:"data"`
250253
Metadata BundledMIMEData `json:"metadata"`
251254
}{
252255
ExecCount: execCount,
253-
Data: BundledMIMEData{
254-
"text/plain": output,
255-
},
256-
Metadata: make(BundledMIMEData),
256+
Data: data.Data,
257+
Metadata: data.Metadata,
257258
},
258259
)
259260
}

0 commit comments

Comments
 (0)