Pass the metadata to jupyter-repl-insert-data

This commit is contained in:
Nathaniel Nicandro 2018-05-26 17:43:35 -05:00
parent 5fd7ccf74f
commit aa509d6bee

View file

@ -391,6 +391,16 @@ can contain the following keywords along with their values:
;;; Handling rich output ;;; Handling rich output
(defvar jupyter-repl-graphic-mimetypes '(:image/png :image/svg+xml :text/latex)
"Mimetypes that display graphics in the REPL buffer.")
(defun jupyter-repl-graphic-data-p (msg)
"Check to see if MSG has mimetypes for graphics."
(cl-loop
with graphic-types = jupyter-repl-graphic-mimetypes
for (mimetype _value) on (jupyter-message-get msg :data) by #'cddr
thereis (memq mimetype graphic-types)))
(defun jupyter-repl-insert-html (html) (defun jupyter-repl-insert-html (html)
"Parse and insert the HTML string using `shr-insert-document'." "Parse and insert the HTML string using `shr-insert-document'."
(jupyter-repl-insert (jupyter-repl-insert
@ -495,7 +505,7 @@ image."
(add-text-properties 0 (length text) '(syntax-table (3)) text) (add-text-properties 0 (length text) '(syntax-table (3)) text)
(jupyter-repl-insert text)) (jupyter-repl-insert text))
(defun jupyter-repl-insert-data (data) (defun jupyter-repl-insert-data (data metadata)
"Insert DATA into the REPL buffer in order of decreasing richness. "Insert DATA into the REPL buffer in order of decreasing richness.
DATA should be plist mapping mimetypes to their content. Attempt DATA should be plist mapping mimetypes to their content. Attempt
to insert a recognized mimetype, trying each one in order of to insert a recognized mimetype, trying each one in order of
@ -508,13 +518,18 @@ decreasing richness of the mimetype. The current order is
- image/svg+xml - image/svg+xml
- text/plain - text/plain
When no valid mimetype is present in DATA, a warning is shown." When no valid mimetype is present in DATA, a warning is shown.
METADATA is a plist similar to data, but with values describing
extra information for inserting each kind of mimetype. For
example the value of `image/png' can be a plist with the keys
`:width', `:height'."
(let ((mimetypes (cl-loop (let ((mimetypes (cl-loop
with graphic-types = '(:image/png :image/svg+xml :text/latex)
for (k d) on data by #'cddr for (k d) on data by #'cddr
when (and d (not (equal d "")) when
(or (display-graphic-p) (and d (not (equal d ""))
(not (memq k graphic-types)))) (or (display-graphic-p)
(not (memq k jupyter-repl-graphic-mimetypes))))
collect k))) collect k)))
(cond (cond
((and (memq :text/html mimetypes) ((and (memq :text/html mimetypes)
@ -532,18 +547,18 @@ When no valid mimetype is present in DATA, a warning is shown."
(jupyter-repl-insert-latex (plist-get data :text/latex)) (jupyter-repl-insert-latex (plist-get data :text/latex))
(jupyter-repl-newline)) (jupyter-repl-newline))
((memq :image/png mimetypes) ((memq :image/png mimetypes)
(insert-image (cl-destructuring-bind (&key width height)
(create-image (plist-get metadata :image/png)
(base64-decode-string (let* ((data (base64-decode-string (plist-get data :image/png)))
(plist-get data :image/png)) (img (create-image data nil 'data :width width :height height)))
nil 'data) (insert-image img (propertize " " 'read-only t)))))
(propertize " " 'read-only t)))
((and (memq :image/svg+xml mimetypes) ((and (memq :image/svg+xml mimetypes)
(image-type-available-p 'svg)) (image-type-available-p 'svg))
(insert-image (cl-destructuring-bind (&key width height)
(create-image (plist-get metadata :image/svg+xml)
(plist-get data :image/svg+xml) 'svg) (let* ((data (plist-get data :image/svg+xml))
(propertize " " 'read-only t))) (img (create-image data 'svg nil :width width :height height)))
(insert-image img (propertize " " 'read-only t)))))
((memq :text/plain mimetypes) ((memq :text/plain mimetypes)
(jupyter-repl-insert-ansi-coded-text (jupyter-repl-insert-ansi-coded-text
(plist-get data :text/plain)) (plist-get data :text/plain))
@ -997,17 +1012,16 @@ lines then truncate it to something less than
req req
_execution-count _execution-count
data data
_metadata) metadata)
;; Only handle our results ;; Only handle our results
(when req (when req
(jupyter-repl-do-at-request client req (jupyter-repl-do-at-request client req
(jupyter-repl-insert-prompt 'out) (jupyter-repl-insert-prompt 'out)
(jupyter-repl-insert-data data)))) (jupyter-repl-insert-data data metadata))))
(cl-defmethod jupyter-handle-display-data ((client jupyter-repl-client) (cl-defmethod jupyter-handle-display-data ((client jupyter-repl-client)
req req
data data
_metadata metadata
transient) transient)
(let ((clear (prog1 (oref client wait-to-clear) (let ((clear (prog1 (oref client wait-to-clear)
(oset client wait-to-clear nil))) (oset client wait-to-clear nil)))
@ -1049,9 +1063,9 @@ lines then truncate it to something less than
;; dedicated to errors. Use an overlay to display errors in the ;; dedicated to errors. Use an overlay to display errors in the
;; REPL buffer. ;; REPL buffer.
(with-jupyter-repl-doc-buffer (format "display-%d" display_id) (with-jupyter-repl-doc-buffer (format "display-%d" display_id)
(jupyter-repl-insert-data data)) (jupyter-repl-insert-data data metadata))
(when clear (jupyter-repl-clear-last-cell-output client)) (when clear (jupyter-repl-clear-last-cell-output client))
(jupyter-repl-insert-data data))))))))) (jupyter-repl-insert-data data metadata)))))))))
(defun jupyter-repl-clear-last-cell-output (client) (defun jupyter-repl-clear-last-cell-output (client)
"In CLIENT's REPL buffer, clear the output of the last completed cell." "In CLIENT's REPL buffer, clear the output of the last completed cell."
@ -1587,16 +1601,16 @@ respond before returning nil."
:code code :pos pos) :code code :pos pos)
timeout))) timeout)))
(when msg (when msg
(cl-destructuring-bind (&key status found data &allow-other-keys) (cl-destructuring-bind (&key status found data metadata &allow-other-keys)
(jupyter-message-content msg) (jupyter-message-content msg)
(when (and (equal status "ok") found) (when (and (equal status "ok") found)
(if buffer (if buffer
(with-current-buffer buffer (with-current-buffer buffer
(prog1 buffer (prog1 buffer
(jupyter-repl-insert-data data) (jupyter-repl-insert-data data metadata)
(goto-char (point-min)))) (goto-char (point-min))))
(with-temp-buffer (with-temp-buffer
(jupyter-repl-insert-data data) (jupyter-repl-insert-data data metadata)
(buffer-string)))))))) (buffer-string))))))))
(defun jupyter-repl-inspect-at-point () (defun jupyter-repl-inspect-at-point ()
@ -1686,7 +1700,9 @@ displayed without anything showing up in the REPL buffer."
(with-current-buffer (with-current-buffer
(get-buffer-create "*jupyter-repl-result*") (get-buffer-create "*jupyter-repl-result*")
(erase-buffer) (erase-buffer)
(jupyter-repl-insert-data (jupyter-message-get msg :data)) (jupyter-repl-insert-data
(jupyter-message-get msg :data)
(jupyter-message-get msg :metadata))
(goto-char (point-min)) (goto-char (point-min))
(switch-to-buffer-other-window (current-buffer))))))) (switch-to-buffer-other-window (current-buffer)))))))
req))) req)))