forked from gopherdata/gophernotes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecution.go
93 lines (79 loc) · 2.64 KB
/
execution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"go/token"
repl "github.com/gopherds/gophernotes/internal/repl"
)
var (
// REPLSession manages the I/O to/from the notebook.
REPLSession *repl.Session
fset *token.FileSet
)
// ExecCounter is incremented each time we run user code in the notebook.
var ExecCounter int
// SetupExecutionEnvironment initializes the REPL session and set of tmp files.
func SetupExecutionEnvironment() {
var err error
REPLSession, err = repl.NewSession()
if err != nil {
panic(err)
}
fset = token.NewFileSet()
}
// OutputMsg holds the data for a pyout message.
type OutputMsg struct {
Execcount int `json:"execution_count"`
Data map[string]string `json:"data"`
Metadata map[string]interface{} `json:"metadata"`
}
// ErrMsg encodes the traceback of errors output to the notebook.
type ErrMsg struct {
EName string `json:"ename"`
EValue string `json:"evalue"`
Traceback []string `json:"traceback"`
}
// HandleExecuteRequest runs code from an execute_request method, and sends the various
// reply messages.
func HandleExecuteRequest(receipt MsgReceipt) {
reply := NewMsg("execute_reply", receipt.Msg)
content := make(map[string]interface{})
reqcontent := receipt.Msg.Content.(map[string]interface{})
code := reqcontent["code"].(string)
silent := reqcontent["silent"].(bool)
if !silent {
ExecCounter++
}
content["execution_count"] = ExecCounter
// Do the compilation/execution magic.
val, stderr, err := REPLSession.Eval(code)
if err == nil {
content["status"] = "ok"
content["payload"] = make([]map[string]interface{}, 0)
content["user_variables"] = make(map[string]string)
content["user_expressions"] = make(map[string]string)
if len(val) > 0 && !silent {
var outContent OutputMsg
out := NewMsg("pyout", receipt.Msg)
outContent.Execcount = ExecCounter
outContent.Data = make(map[string]string)
outContent.Data["text/plain"] = fmt.Sprint(val)
outContent.Metadata = make(map[string]interface{})
out.Content = outContent
receipt.SendResponse(receipt.Sockets.IOPubSocket, out)
}
} else {
content["status"] = "error"
content["ename"] = "ERROR"
content["evalue"] = err.Error()
content["traceback"] = []string{stderr.String()}
errormsg := NewMsg("pyerr", receipt.Msg)
errormsg.Content = ErrMsg{"Error", err.Error(), []string{stderr.String()}}
receipt.SendResponse(receipt.Sockets.IOPubSocket, errormsg)
}
// send the output back to the notebook
reply.Content = content
receipt.SendResponse(receipt.Sockets.ShellSocket, reply)
idle := NewMsg("status", receipt.Msg)
idle.Content = KernelStatus{"idle"}
receipt.SendResponse(receipt.Sockets.IOPubSocket, idle)
}