-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathoutput.lisp
162 lines (133 loc) · 5.42 KB
/
output.lisp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(in-package #:jupyter/widgets)
(defwidget output (dom-widget)
((msg-id
:initarg :msg-id
:initform ""
:accessor widget-msg-id
:documentation "Parent message id of messages to capture"
:trait :string)
(outputs
:initarg :outputs
:initform nil
:accessor widget-outputs
:documentation "The output messages synced from the frontend."
:trait :list))
(:default-initargs
:%model-name "OutputModel"
:%model-module +output-module+
:%model-module-version +output-module-version+
:%view-name "OutputView"
:%view-module +output-module+
:%view-module-version +output-module-version+)
(:documentation
"Widget used as a context manager to display output.
This widget can capture and display stdout, stderr, and rich output. To use it,
create an instance of it and display it.
You can then use the widget as a context manager: any output produced while in
the context will be captured and displayed in the widget instead of the standard
output area.
### Example
```common-lisp
(use-package :jupyter-widgets)
(defvar out (make-instance 'output))
(with-output out
(print \"prints to output area\")
```"))
(defwidget sidecar (output)
((title
:accessor widget-title
:initarg :title
:initform nil
:trait :string))
(:default-initargs
:%model-name "SidecarModel"
:%model-module +sidecar-module+
:%model-module-version +sidecar-module-version+
:%view-name "SidecarView"
:%view-module +sidecar-module+
:%view-module-version +sidecar-module-version+)
(:documentation
"Widget used as a context manager to display output.
This widget can capture and display stdout, stderr, and rich output. To use it,
create an instance of it and display it.
You can then use the widget as a context manager: any output produced while in
the context will be captured and displayed in the widget instead of the standard
output area.
### Example
```common-lisp
(use-package :jupyter-widgets)
(defvar out (make-instance 'output))
(with-output out
(print \"prints to output area\")
```"))
(defclass output-widget-stream (ngray:fundamental-character-output-stream)
((output
:initarg :output
:reader output-widget-stream-output)
(name
:initarg :name
:reader output-widget-stream-name)
(value
:initform (make-array 1024
:fill-pointer 0
:adjustable t
:element-type 'character)
:accessor output-widget-stream-value)
(column
:accessor output-widget-stream-column
:initform 0)))
(defun make-output-widget-stream (output &optional error-output)
(make-instance 'output-widget-stream :output output :name (if error-output "stderr" "stdout")))
(defmethod ngray:stream-write-char ((stream output-widget-stream) char)
(with-slots (output name value column) stream
(cond
((graphic-char-p char)
(incf column))
((or (char= #\newline)
(char= #\return))
(setf column 0))
((char= #\tab)
(incf column 8)))
(vector-push-extend char value)))
(defmethod ngray:stream-finish-output ((stream output-widget-stream))
(with-slots (output name value column) stream
(unless (zerop (length value))
(let* ((outputs (mapcan (lambda (output)
(unless (and (equal (gethash "output_type" output) "display_data")
(gethash "application/javascript" (gethash "data" output)))
(list output)))
(coerce (widget-outputs output) 'list)))
(last-output (car (last outputs))))
(cond
((and last-output
(equal (gethash "output_type" last-output) "stream")
(equal (gethash "name" last-output) name))
(setf (gethash "text" last-output) (concatenate 'string (gethash "text" last-output) value))
(notify-trait-change output :list :outputs nil (widget-outputs output) t))
(t
(setf (widget-outputs output)
(append outputs
(list (j:make-object "output_type" "stream"
"name" name
"text" (copy-seq value))
(j:make-object "output_type" "display_data"
"metadata" (j:make-object)
"data" (j:make-object "application/javascript" "for (let el of document.getElementsByClassName('widget-output')) el.scrollTo( { left: 0, top: el.scrollHeight - el.clientHeight, behavior: 'smooth' });")))))))
(adjust-array value (array-total-size value)
:fill-pointer 0)))))
(defmethod ngray:stream-line-column ((stream output-widget-stream))
(output-widget-stream-column stream))
; We should clean up after ourselves, but the messages are processed outside of this lexigraphic context.
(defmacro with-output (output &body body)
"Evaluate body with all output sent to the output widget."
`(with-slots (msg-id) ,output
(unwind-protect
(progn
(finish-output)
(finish-output *error-output*)
(setf msg-id (when jupyter::*message*
(gethash "msg_id" (jupyter::message-header jupyter::*message*))))
,@body)
(finish-output)
(finish-output *error-output*)
(setf msg-id ""))))