2018-03-08 09:07:31 -06:00
|
|
|
;; -*- lexical-binding: t -*-
|
2016-10-11 20:29:48 -05:00
|
|
|
;;; ob-ein.el --- org-babel functions for template evaluation
|
|
|
|
|
2016-10-25 18:00:51 -05:00
|
|
|
;; Copyright (C) John M. Miller
|
2016-10-11 20:29:48 -05:00
|
|
|
|
|
|
|
;; Author: John M. Miller <millejoh at mac.com>
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ob-ein.el is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; ob-ein.el is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2018-05-29 17:20:54 -04:00
|
|
|
;; along with ob-ein.el. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-11 20:29:48 -05:00
|
|
|
|
|
|
|
;;; Commentary:
|
2019-02-14 15:28:18 -05:00
|
|
|
|
2018-02-25 12:43:12 -06:00
|
|
|
;; Support executing org-babel source blocks using EIN worksheets.
|
2019-02-14 15:28:18 -05:00
|
|
|
;; Modelled after https://github.com/gregsexton/ob-ipython by Greg Sexton
|
2018-02-25 12:43:12 -06:00
|
|
|
;; Async support based on work by @khinsen on github in ob-ipython-async: https://github.com/khinsen/ob-ipython-async/blob/master/ob-ipython-async.el
|
2018-02-27 09:32:51 +01:00
|
|
|
;; which was in turn inspired by the scimax starter kit by @jkitchin: https://github.com/jkitchin/scimax
|
2016-10-11 20:29:48 -05:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'ob-python)
|
|
|
|
(require 'ein-utils)
|
2019-02-14 15:28:18 -05:00
|
|
|
(require 'ein-notebooklist)
|
|
|
|
(require 'ein-process)
|
2016-10-11 20:29:48 -05:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defvar *ob-ein-sentinel* "[....]"
|
|
|
|
"Placeholder string replaced after async cell execution")
|
2018-10-21 13:00:33 -04:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defcustom ob-ein-anonymous-path ".ob-ein.ipynb"
|
|
|
|
"When session header specifies only server, prosecute all ob-ein interactions in this single anonymous notebook."
|
|
|
|
:type '(string)
|
|
|
|
:group 'ein)
|
2018-02-25 12:43:12 -06:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defcustom ob-ein-inline-image-directory "ein-images"
|
2018-04-14 13:29:16 -05:00
|
|
|
"Default directory where to save images generated from ein org-babel source blocks."
|
2018-02-25 12:43:12 -06:00
|
|
|
:group 'ein
|
|
|
|
:type '(directory))
|
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defvar org-babel-default-header-args:ein nil)
|
2018-11-22 11:22:43 -08:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun ob-ein--inline-image-info (value)
|
2018-02-25 12:43:12 -06:00
|
|
|
(let* ((f (md5 value))
|
2019-02-14 15:28:18 -05:00
|
|
|
(d ob-ein-inline-image-directory)
|
2018-02-25 12:43:12 -06:00
|
|
|
(tf (concat d "/ob-ein-" f ".png")))
|
|
|
|
(unless (file-directory-p d)
|
2018-11-26 14:57:31 +08:00
|
|
|
(make-directory d 'parents))
|
2018-02-25 12:43:12 -06:00
|
|
|
tf))
|
2016-10-12 13:42:19 -05:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun ob-ein--write-base64-image (img-string file)
|
2016-10-12 13:42:19 -05:00
|
|
|
(with-temp-file file
|
2016-10-13 15:15:08 -05:00
|
|
|
(let ((buffer-read-only nil)
|
|
|
|
(buffer-file-coding-system 'binary)
|
|
|
|
(require-final-newline nil)
|
|
|
|
(file-precious-flag t))
|
2016-10-12 13:42:19 -05:00
|
|
|
(insert img-string)
|
|
|
|
(base64-decode-region (point-min) (point-max)))))
|
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun ob-ein--return-mime-type (json file)
|
2016-10-12 13:42:19 -05:00
|
|
|
(loop
|
2018-11-22 11:23:22 -08:00
|
|
|
for key in ein:output-types-text-preferred
|
2016-10-12 13:42:19 -05:00
|
|
|
for type = (intern (format ":%s" key)) ; something like `:text'
|
|
|
|
for value = (plist-get json type) ; FIXME: optimize
|
|
|
|
when (plist-member json type)
|
|
|
|
return
|
|
|
|
(case key
|
|
|
|
((svg image/svg)
|
2019-02-14 15:28:18 -05:00
|
|
|
(let ((file (or file (ob-ein--inline-image-info value))))
|
|
|
|
(ob-ein--write-base64-image value file)
|
2017-01-18 09:58:37 -06:00
|
|
|
(format "[[file:%s]]" file)))
|
2016-10-12 13:42:19 -05:00
|
|
|
((png image/png jpeg image/jpeg)
|
2019-02-14 15:28:18 -05:00
|
|
|
(let ((file (or file (ob-ein--inline-image-info value))))
|
|
|
|
(ob-ein--write-base64-image value file)
|
2017-01-18 09:58:37 -06:00
|
|
|
(format "[[file:%s]]" file)))
|
2016-10-12 13:42:19 -05:00
|
|
|
(t (plist-get json type)))))
|
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun ob-ein--process-outputs (outputs params)
|
2017-01-30 21:34:58 -06:00
|
|
|
(let ((file (cdr (assoc :image params))))
|
2017-01-18 09:58:37 -06:00
|
|
|
(ein:join-str "\n"
|
|
|
|
(loop for o in outputs
|
2019-02-14 15:28:18 -05:00
|
|
|
collecting (ob-ein--return-mime-type o file)))))
|
2018-02-25 12:43:12 -06:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun ob-ein--get-name-create (src-block-info)
|
2018-02-25 12:43:12 -06:00
|
|
|
"Get the name of a src block or add a uuid as the name."
|
2019-02-14 15:28:18 -05:00
|
|
|
(if-let ((name (fifth src-block-info)))
|
2018-02-25 12:43:12 -06:00
|
|
|
name
|
|
|
|
(save-excursion
|
|
|
|
(let ((el (org-element-context))
|
2019-02-14 15:28:18 -05:00
|
|
|
(id (org-id-new 'none)))
|
|
|
|
(goto-char (org-element-property :begin el))
|
|
|
|
(insert (format "#+NAME: %s\n" id))
|
|
|
|
id))))
|
2018-02-25 12:43:12 -06:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun ein:org-register-lang-mode (lang-name lang-mode)
|
|
|
|
"Define org+ein language LANG-NAME with syntax highlighting from LANG-MODE. Untested.
|
2016-12-28 09:41:02 -06:00
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
For example, call (ein:org-register-lang-mode \"ein-R\" 'R) to define a language \"ein-R\" with R syntax highlighting for use with org-babel and ein.
|
|
|
|
|
|
|
|
Based on ob-ipython--configure-kernel.
|
|
|
|
"
|
|
|
|
(add-to-list 'org-src-lang-modes `(,lang-name . ,lang-mode))
|
|
|
|
(defvaralias (intern (concat "org-babel-default-header-args:" lang-name))
|
|
|
|
'org-babel-default-header-args:ein)
|
|
|
|
(defalias (intern (concat "org-babel-execute:" lang-name))
|
|
|
|
'org-babel-execute:ein))
|
|
|
|
|
|
|
|
;;;###autoload
|
2016-10-11 20:29:48 -05:00
|
|
|
(defun org-babel-execute:ein (body params)
|
2019-02-14 15:28:18 -05:00
|
|
|
"This function is called by `org-babel-execute-src-block'."
|
|
|
|
(let* ((buffer (current-buffer))
|
|
|
|
(processed-params (org-babel-process-params params))
|
2019-02-17 21:10:08 -06:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
2019-02-14 15:28:18 -05:00
|
|
|
(session (format "%s" (cdr (assoc :session processed-params))))
|
2019-02-20 08:24:07 -05:00
|
|
|
(kernelspec (or (cdr (assoc :kernelspec processed-params)) "default"))
|
2019-02-14 15:28:18 -05:00
|
|
|
(name (ob-ein--get-name-create (org-babel-get-src-block-info)))
|
|
|
|
(full-body (org-babel-expand-body:generic
|
|
|
|
(encode-coding-string body 'utf-8)
|
|
|
|
params
|
|
|
|
(org-babel-variable-assignments:python params)))
|
|
|
|
(callback (lambda (notebook)
|
|
|
|
(ob-ein--execute-async
|
|
|
|
buffer
|
|
|
|
full-body
|
|
|
|
(ein:$notebook-kernel notebook)
|
|
|
|
processed-params
|
|
|
|
result-params
|
|
|
|
name))))
|
2019-02-20 08:24:07 -05:00
|
|
|
(message "got here org-babel-execute:ein %s" name)
|
2019-02-14 15:28:18 -05:00
|
|
|
(ob-ein--initiate-session session kernelspec callback))
|
|
|
|
*ob-ein-sentinel*)
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-06-09 11:59:42 -05:00
|
|
|
(defun org-babel-execute:ein-hy (body params)
|
|
|
|
(org-babel-execute:ein (ein:pytools-wrap-hy-code body) params))
|
|
|
|
|
2019-02-14 15:28:18 -05:00
|
|
|
(defsubst ob-ein--execute-async-callback (buffer params result-params name)
|
|
|
|
"Callback of 1-arity (the shared output cell) to update org buffer when
|
|
|
|
`ein:shared-output-eval-string' completes."
|
|
|
|
(apply-partially
|
|
|
|
(lambda (buffer* params* result-params* name* cell)
|
|
|
|
(let* ((raw (ein:aif (ein:oref-safe cell 'traceback)
|
|
|
|
(ansi-color-apply (ein:join-str "\n" it))
|
|
|
|
(ob-ein--process-outputs
|
|
|
|
(ein:oref-safe cell 'outputs) params*)))
|
|
|
|
(result (org-babel-result-cond result-params*
|
|
|
|
raw (org-babel-python-table-or-string raw))))
|
2019-02-20 08:24:07 -05:00
|
|
|
(message "got here ob-ein--execute-async-callback %s %s"
|
|
|
|
name* result)
|
2019-02-14 15:28:18 -05:00
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
(with-current-buffer buffer*
|
|
|
|
(org-babel-goto-named-src-block name*)
|
|
|
|
(org-babel-remove-result)
|
|
|
|
(org-babel-insert-result
|
|
|
|
result
|
|
|
|
(cdr (assoc :result-params
|
|
|
|
(third (org-babel-get-src-block-info)))))
|
2019-02-20 08:24:07 -05:00
|
|
|
(org-redisplay-inline-images)
|
|
|
|
(message "got here ob-ein--execute-async-callback %s"
|
|
|
|
(buffer-string)))))))
|
2019-02-14 15:28:18 -05:00
|
|
|
buffer params result-params name))
|
|
|
|
|
|
|
|
(defun ob-ein--execute-async (buffer body kernel params result-params name)
|
|
|
|
"As `ein:shared-output-get-cell' is a singleton, ob-ein can only execute blocks
|
|
|
|
one at a time. Further, we do not order the queued up blocks!"
|
|
|
|
(deferred:$
|
|
|
|
(deferred:next
|
|
|
|
(deferred:lambda ()
|
|
|
|
(let ((cell (ein:shared-output-get-cell)))
|
2019-02-20 08:24:07 -05:00
|
|
|
(message "got here ob-ein--execute-async %s %s" name (slot-value cell 'callback))
|
2019-02-14 15:28:18 -05:00
|
|
|
(if (eq (slot-value cell 'callback) #'ignore)
|
|
|
|
(let ((callback
|
|
|
|
(ob-ein--execute-async-callback buffer params
|
|
|
|
result-params name)))
|
2019-02-20 08:24:07 -05:00
|
|
|
(message "got here ob-ein--execute-async assigning %s" name)
|
2019-02-14 15:28:18 -05:00
|
|
|
(setf (slot-value cell 'callback) callback))
|
|
|
|
(deferred:nextc (deferred:wait 1200) self)))))
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (_x)
|
2019-02-20 08:24:07 -05:00
|
|
|
(message "got here ob-ein--execute-async running %s" name)
|
2019-02-14 15:28:18 -05:00
|
|
|
(ein:shared-output-eval-string kernel body nil)))))
|
|
|
|
|
|
|
|
(defun ob-ein--edit-ctrl-c-ctrl-c ()
|
|
|
|
"C-c C-c mapping in ein:connect-mode-map."
|
2018-06-04 08:10:50 -05:00
|
|
|
(interactive)
|
2018-10-19 13:41:13 -05:00
|
|
|
(org-edit-src-save)
|
2018-10-15 11:21:05 -04:00
|
|
|
(when (boundp 'org-src--beg-marker)
|
|
|
|
(let* ((beg org-src--beg-marker)
|
|
|
|
(buf (marker-buffer beg)))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(save-excursion
|
|
|
|
(goto-char beg)
|
|
|
|
(org-ctrl-c-ctrl-c))))))
|
2017-09-19 13:08:13 -05:00
|
|
|
|
2019-02-20 08:24:07 -05:00
|
|
|
;;;###autoload
|
2019-02-14 15:28:18 -05:00
|
|
|
(defun org-babel-edit-prep:ein (babel-info)
|
2019-02-20 08:24:07 -05:00
|
|
|
"C-c ' enters the lightly tested connect-to-notebook mode."
|
|
|
|
(let* ((buffer (current-buffer))
|
|
|
|
(processed-params (org-babel-process-params (third babel-info))))
|
2019-02-14 15:28:18 -05:00
|
|
|
(ob-ein--initiate-session
|
2019-02-20 08:24:07 -05:00
|
|
|
(format "%s" (cdr (assoc :session processed-params)))
|
|
|
|
(or (cdr (assoc :kernelspec processed-params)) "default")
|
2019-02-14 15:28:18 -05:00
|
|
|
(lambda (notebook)
|
|
|
|
(ein:connect-buffer-to-notebook notebook buffer t)
|
2019-02-20 08:24:07 -05:00
|
|
|
(define-key ein:connect-mode-map "\C-c\C-c" 'ob-ein--edit-ctrl-c-ctrl-c)))))
|
2019-02-14 15:28:18 -05:00
|
|
|
|
|
|
|
(defun ob-ein--parse-session (session)
|
|
|
|
(multiple-value-bind (url-or-port _password) (ein:jupyter-server-conn-info)
|
|
|
|
(let ((tokens (split-string session "/"))
|
|
|
|
(parsed-url (url-generic-parse-url session)))
|
|
|
|
(cond ((null (url-host parsed-url))
|
|
|
|
(let* ((candidate (apply #'ein:url (car tokens) (cdr tokens)))
|
|
|
|
(parsed-candidate (url-generic-parse-url candidate))
|
|
|
|
(missing (url-scheme-get-property
|
|
|
|
(url-type parsed-candidate)
|
|
|
|
'default-port)))
|
|
|
|
(if (and url-or-port
|
|
|
|
(= (url-port parsed-candidate) missing))
|
|
|
|
(apply #'ein:url url-or-port (cdr tokens))
|
|
|
|
candidate)))
|
|
|
|
(t (ein:url session))))))
|
|
|
|
|
|
|
|
(defun ob-ein--initiate-session (session kernelspec callback)
|
|
|
|
"Retrieve notebook based on SESSION path and KERNELSPEC, starting jupyter instance
|
|
|
|
if necessary. Install CALLBACK (i.e., cell execution) upon notebook retrieval."
|
|
|
|
(let* ((nbpath (ob-ein--parse-session session))
|
|
|
|
(parsed-url (url-generic-parse-url nbpath))
|
|
|
|
(slash-path (car (url-path-and-query parsed-url)))
|
|
|
|
(path (if (string= slash-path "")
|
|
|
|
ob-ein-anonymous-path
|
|
|
|
(substring slash-path 1)))
|
|
|
|
(url-or-port (if (string= slash-path "")
|
|
|
|
nbpath
|
|
|
|
(substring nbpath 0 (- (length slash-path)))))
|
|
|
|
(notebook (ein:notebook-get-opened-notebook url-or-port path))
|
|
|
|
(callback-nbopen (lambda (nb _created)
|
|
|
|
(loop repeat 50
|
|
|
|
for live-p = (ein:kernel-live-p (ein:$notebook-kernel nb))
|
|
|
|
until live-p
|
|
|
|
do (sleep-for 0 300)
|
|
|
|
finally
|
|
|
|
do (if (not live-p)
|
|
|
|
(ein:log 'error
|
|
|
|
"Kernel for %s failed to launch"
|
|
|
|
(ein:$notebook-notebook-name nb))
|
|
|
|
(funcall callback nb)))))
|
|
|
|
(errback-nbopen (lambda (url-or-port status-code)
|
|
|
|
(if (eq status-code 404)
|
|
|
|
(ein:notebooklist-new-notebook-with-name
|
|
|
|
url-or-port kernelspec path callback-nbopen t))))
|
|
|
|
(callback-login (lambda (_buffer url-or-port)
|
|
|
|
(ein:notebook-open url-or-port path kernelspec
|
|
|
|
callback-nbopen errback-nbopen t))))
|
2019-02-20 08:24:07 -05:00
|
|
|
(cond (notebook (message "got here ob-ein--initiate-session %s" (ein:$notebook-notebook-name notebook)) (funcall callback notebook))
|
2019-02-14 15:28:18 -05:00
|
|
|
((string= (url-host parsed-url) ein:url-localhost)
|
|
|
|
(ein:process-refresh-processes)
|
|
|
|
(ein:aif (ein:process-url-match nbpath)
|
|
|
|
(ein:notebooklist-login (ein:process-url-or-port it) callback-login)
|
|
|
|
(ein:jupyter-server-start
|
|
|
|
(executable-find ein:jupyter-default-server-command)
|
|
|
|
(read-directory-name "Notebook directory: " default-directory)
|
|
|
|
nil
|
|
|
|
callback-login
|
|
|
|
(let* ((port (url-port parsed-url))
|
|
|
|
(avoid (url-scheme-get-property (url-type parsed-url) 'default-port)))
|
|
|
|
(cond ((= port avoid) nil)
|
|
|
|
(t (url-port parsed-url)))))))
|
|
|
|
(t (ein:notebooklist-login url-or-port callback-login)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(with-eval-after-load "python"
|
|
|
|
(setq python-indent-guess-indent-offset-verbose nil))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(add-hook 'org-mode-hook (lambda ()
|
|
|
|
(add-to-list 'org-src-lang-modes '("ein" . python))
|
|
|
|
(add-to-list 'org-src-lang-modes '("ein-hy" . hy))))
|
2016-10-11 20:29:48 -05:00
|
|
|
|
|
|
|
(provide 'ob-ein)
|