2018-01-22 20:37:12 -06:00
|
|
|
;;; ob-jupyter.el --- Jupyter integration with org-mode -*- lexical-binding: t -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2018 Nathaniel Nicandro
|
|
|
|
|
|
|
|
;; Author: Nathaniel Nicandro <nathanielnicandro@gmail.com>
|
|
|
|
;; Created: 21 Jan 2018
|
2019-03-14 09:08:31 -05:00
|
|
|
;; Version: 0.7.3
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
;; This program 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 2, or (at
|
|
|
|
;; your option) any later version.
|
|
|
|
|
|
|
|
;; This program 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
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2019-01-16 20:14:21 -06:00
|
|
|
;; Interact with a Jupyter kernel via `org-mode' src-block's.
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(defgroup ob-jupyter nil
|
2019-01-12 23:12:56 -06:00
|
|
|
"Jupyter integration with org-mode"
|
2018-02-03 21:10:21 -06:00
|
|
|
:group 'org-babel)
|
2018-01-22 20:37:12 -06:00
|
|
|
|
2018-06-03 22:55:06 -05:00
|
|
|
(require 'jupyter-org-client)
|
2019-03-14 08:26:30 +01:00
|
|
|
(require 'jupyter-org-extensions)
|
2019-03-10 19:53:54 -05:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'jupyter-repl) ; For `jupyter-with-repl-buffer'
|
|
|
|
(require 'subr-x))
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
(declare-function org-element-at-point "org-element")
|
2018-06-03 22:55:06 -05:00
|
|
|
(declare-function org-link-set-parameters "org" (type &rest parameters))
|
2018-05-30 22:15:17 -05:00
|
|
|
(declare-function org-in-src-block-p "org" (&optional inside))
|
2018-01-22 20:37:12 -06:00
|
|
|
(declare-function org-element-property "org-element" (property element))
|
|
|
|
(declare-function org-element-type "org-element" (element))
|
|
|
|
(declare-function org-element-context "org-element" (&optional element))
|
|
|
|
(declare-function org-babel-variable-assignments:python "ob-python" (params))
|
|
|
|
(declare-function org-babel-expand-body:generic "ob-core" (body params &optional var-lines))
|
2019-02-12 11:49:17 -06:00
|
|
|
(declare-function org-export-derived-backend-p "ox" (backend &rest backends))
|
2018-01-22 20:37:12 -06:00
|
|
|
|
2018-06-03 22:55:06 -05:00
|
|
|
(defvaralias 'org-babel-jupyter-resource-directory
|
|
|
|
'jupyter-org-resource-directory)
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
(defvar org-babel-jupyter-session-clients (make-hash-table :test #'equal)
|
2019-02-06 17:46:14 -06:00
|
|
|
"A hash table mapping session names to Jupyter clients.")
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
(defvar org-babel-header-args:jupyter '((kernel . :any)
|
|
|
|
(async . ((yes no))))
|
|
|
|
"Available header arguments for Jupter src-blocks.")
|
|
|
|
|
|
|
|
(defvar org-babel-default-header-args:jupyter '((:kernel . "python")
|
|
|
|
(:async . "no"))
|
|
|
|
"Default header arguments for Jupyter src-blocks.")
|
|
|
|
|
2019-01-16 20:14:02 -06:00
|
|
|
(defun org-babel-jupyter--src-block-kernel-language ()
|
|
|
|
(when (org-in-src-block-p)
|
|
|
|
(let ((info (org-babel-get-src-block-info)))
|
|
|
|
(save-match-data
|
2019-02-12 08:55:40 -06:00
|
|
|
(string-match "^jupyter-\\(.+\\)$" (car info))
|
2019-01-16 20:14:02 -06:00
|
|
|
(match-string 1 (car info))))))
|
2018-01-23 13:42:10 -06:00
|
|
|
|
|
|
|
(defun org-babel-variable-assignments:jupyter (params &optional lang)
|
2018-01-22 20:37:12 -06:00
|
|
|
"Assign variables in PARAMS according to the Jupyter kernel language.
|
2018-02-12 11:03:41 -06:00
|
|
|
LANG is the kernel language of the source block. If LANG is nil,
|
|
|
|
get the kernel language from the current source block.
|
2018-01-22 20:37:12 -06:00
|
|
|
|
2018-02-12 11:03:41 -06:00
|
|
|
The variables are assigned by looking for the function
|
|
|
|
`org-babel-variable-assignments:LANG'. If this function does not
|
|
|
|
exist or if LANG cannot be determined, assign variables using
|
|
|
|
`org-babel-variable-assignments:python'."
|
2019-01-16 20:14:02 -06:00
|
|
|
(or lang (setq lang (org-babel-jupyter--src-block-kernel-language)))
|
|
|
|
(let ((fun (when lang
|
2019-03-07 22:22:51 -06:00
|
|
|
(intern (format "org-babel-variable-assignments:%s" lang)))))
|
2018-01-22 20:37:12 -06:00
|
|
|
(if (functionp fun) (funcall fun params)
|
|
|
|
(org-babel-variable-assignments:python params))))
|
|
|
|
|
2019-01-13 02:20:38 -06:00
|
|
|
(cl-defgeneric org-babel-jupyter-transform-code (code _changelist)
|
|
|
|
"Transform CODE according to CHANGELIST, return the transformed CODE.
|
|
|
|
CHANGELIST is a property list containing the requested changes. The default
|
|
|
|
implementation returns CODE unchanged.
|
|
|
|
|
|
|
|
This is useful for kernel languages to extend using the
|
|
|
|
jupyter-lang method specializer, e.g. to return new code to change
|
|
|
|
directories before evaluating CODE.
|
|
|
|
|
|
|
|
See `org-babel-expand-body:jupyter' for possible changes that can
|
|
|
|
be in CHANGELIST."
|
|
|
|
code)
|
|
|
|
|
2018-01-23 13:42:10 -06:00
|
|
|
(defun org-babel-expand-body:jupyter (body params &optional var-lines lang)
|
2018-01-22 20:37:12 -06:00
|
|
|
"Expand BODY according to PARAMS.
|
|
|
|
|
|
|
|
BODY is the code to expand, PARAMS should be the header arguments
|
|
|
|
of the src block with BODY as its code, and VAR-LINES should be
|
|
|
|
the list of strings containing the variables to evaluate before
|
2018-02-12 11:03:41 -06:00
|
|
|
executing body. LANG is the kernel language of the source block.
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
This function is similar to
|
2018-02-12 11:03:41 -06:00
|
|
|
`org-babel-variable-assignments:jupyter' in that it attempts to
|
|
|
|
find the kernel language of the source block if LANG is not
|
|
|
|
provided.
|
2018-01-22 20:37:12 -06:00
|
|
|
|
2018-02-12 11:03:41 -06:00
|
|
|
BODY is expanded by calling the function
|
|
|
|
`org-babel-expand-body:LANG'. If this function doesn't exist or
|
|
|
|
if LANG cannot be determined, fall back to
|
2019-01-13 02:20:38 -06:00
|
|
|
`org-babel-expand-body:generic'.
|
|
|
|
|
|
|
|
If PARAMS has a :dir parameter, the expanded code is passed to
|
|
|
|
`org-babel-jupyter-transform-code' with a changelist that
|
|
|
|
includes the :dir parameter with the directory being an absolute
|
|
|
|
path."
|
2019-01-16 20:14:02 -06:00
|
|
|
(or lang (setq lang (org-babel-jupyter--src-block-kernel-language)))
|
|
|
|
(let* ((expander (when lang
|
2019-03-07 22:22:51 -06:00
|
|
|
(intern (format "org-babel-expand-body:%s" lang))))
|
2019-01-13 02:20:38 -06:00
|
|
|
(expanded (if (functionp expander)
|
|
|
|
(funcall expander body params var-lines)
|
|
|
|
(org-babel-expand-body:generic body params var-lines)))
|
|
|
|
(changelist nil))
|
2019-01-16 17:50:17 -06:00
|
|
|
(when-let* ((dir (alist-get :dir params)))
|
|
|
|
(setq changelist (plist-put changelist :dir (expand-file-name dir))))
|
2019-01-13 02:20:38 -06:00
|
|
|
(if changelist (org-babel-jupyter-transform-code expanded changelist)
|
|
|
|
expanded)))
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
(defun org-babel-edit-prep:jupyter (info)
|
|
|
|
"Prepare the edit buffer according to INFO."
|
|
|
|
(let* ((params (nth 2 info))
|
|
|
|
(session (alist-get :session params)))
|
|
|
|
(jupyter-repl-associate-buffer
|
|
|
|
(org-babel-jupyter-initiate-session session params))))
|
|
|
|
|
2018-09-03 00:50:36 -05:00
|
|
|
(defun org-babel-prep-session:jupyter (session params &optional delay-eval)
|
2018-01-22 20:37:12 -06:00
|
|
|
"Prepare a Jupyter SESSION according to PARAMS.
|
2018-09-03 00:50:36 -05:00
|
|
|
If DELAY-EVAL is non-nil, delay the evaluation of the header
|
|
|
|
variables in PARAMS."
|
2018-01-22 20:37:12 -06:00
|
|
|
(let ((buffer (org-babel-jupyter-initiate-session session params))
|
|
|
|
(var-lines (org-babel-variable-assignments:jupyter params)))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(goto-char (point-max))
|
|
|
|
(when var-lines
|
|
|
|
(jupyter-repl-replace-cell-code
|
|
|
|
(mapconcat #'identity var-lines "\n"))
|
2018-02-12 11:03:41 -06:00
|
|
|
;; For `org-babel-load-session:jupyter', ensure that the loaded code
|
|
|
|
;; starts on a new line.
|
2018-09-03 00:50:36 -05:00
|
|
|
(when delay-eval
|
2018-01-22 20:37:12 -06:00
|
|
|
(insert "\n")))
|
2018-09-03 00:50:36 -05:00
|
|
|
(unless delay-eval
|
2018-09-16 23:01:54 -05:00
|
|
|
(jupyter-send-execute-request jupyter-current-client))
|
2018-01-22 20:37:12 -06:00
|
|
|
(current-buffer))))
|
|
|
|
|
|
|
|
(defun org-babel-load-session:jupyter (session body params)
|
|
|
|
"In a Jupyter SESSION, load BODY according to PARAMS."
|
|
|
|
(save-window-excursion
|
2018-09-03 00:50:36 -05:00
|
|
|
(let ((buffer (org-babel-prep-session:jupyter session params 'delay-eval)))
|
2018-01-22 20:37:12 -06:00
|
|
|
(with-current-buffer buffer
|
2019-01-13 02:20:38 -06:00
|
|
|
(insert (org-babel-expand-body:jupyter (org-babel-chomp body) params))
|
2018-01-22 20:37:12 -06:00
|
|
|
(current-buffer)))))
|
|
|
|
|
|
|
|
(defun org-babel-jupyter-initiate-session-by-key (session params)
|
2019-02-06 17:46:14 -06:00
|
|
|
"Return the Jupyter REPL buffer for SESSION.
|
2018-01-22 20:37:12 -06:00
|
|
|
If SESSION does not have a client already, one is created based
|
|
|
|
on SESSION and PARAMS. If SESSION ends with \".json\" then
|
|
|
|
SESSION is interpreted as a kernel connection file and a new
|
2018-09-09 21:33:05 -05:00
|
|
|
kernel connected to SESSION is created.
|
|
|
|
|
|
|
|
Otherwise a kernel is started based on the `:kernel' parameter
|
|
|
|
in PARAMS which should be either a valid kernel name or a prefix
|
|
|
|
of one. The first kernel that is returned by
|
|
|
|
`jupyter-find-kernelspecs' when passed the value of the `:kernel'
|
|
|
|
parameter will be used."
|
2018-01-22 20:37:12 -06:00
|
|
|
(let* ((kernel (alist-get :kernel params))
|
2018-05-06 11:44:10 -05:00
|
|
|
(key (concat session "-" kernel))
|
|
|
|
(client
|
|
|
|
(or (gethash key org-babel-jupyter-session-clients)
|
2018-06-03 22:55:06 -05:00
|
|
|
(let ((client
|
|
|
|
(if (string-suffix-p ".json" session)
|
2018-10-17 03:44:19 -05:00
|
|
|
(jupyter-connect-repl session nil nil 'jupyter-org-client)
|
|
|
|
(jupyter-run-repl kernel nil nil 'jupyter-org-client))))
|
2018-01-22 20:37:12 -06:00
|
|
|
(jupyter-set client 'jupyter-include-other-output nil)
|
2018-10-02 22:09:59 -05:00
|
|
|
(jupyter-with-repl-buffer client
|
2018-01-22 20:37:12 -06:00
|
|
|
(let ((name (buffer-name)))
|
|
|
|
(when (string-match "^\\*\\(.+\\)\\*" name)
|
|
|
|
(rename-buffer
|
|
|
|
(concat "*" (match-string 1 name) "-" session "*")
|
|
|
|
'unique)))
|
|
|
|
(add-hook
|
|
|
|
'kill-buffer-hook
|
|
|
|
(lambda ()
|
|
|
|
(remhash key org-babel-jupyter-session-clients))
|
|
|
|
nil t))
|
2018-05-06 11:44:10 -05:00
|
|
|
(puthash key client org-babel-jupyter-session-clients)))))
|
|
|
|
(oref client buffer)))
|
2018-01-22 20:37:12 -06:00
|
|
|
|
|
|
|
(defun org-babel-jupyter-initiate-session (&optional session params)
|
|
|
|
"Initialize a Jupyter SESSION according to PARAMS."
|
|
|
|
(if (equal session "none") (error "Need a session to run")
|
|
|
|
(org-babel-jupyter-initiate-session-by-key session params)))
|
|
|
|
|
2018-05-13 12:02:59 -05:00
|
|
|
(defun org-babel-jupyter-scratch-buffer ()
|
|
|
|
"Display a scratch buffer connected to the current block's session."
|
|
|
|
(interactive)
|
|
|
|
(let (buffer)
|
|
|
|
(org-babel-do-in-edit-buffer
|
|
|
|
(setq buffer (jupyter-repl-scratch-buffer)))
|
|
|
|
(if buffer (pop-to-buffer buffer)
|
|
|
|
(user-error "No source block at point"))))
|
|
|
|
|
2019-03-13 23:39:15 -05:00
|
|
|
(defvar org-bracket-link-regexp)
|
|
|
|
|
2019-01-13 10:33:21 -06:00
|
|
|
(defun org-babel-jupyter-cleanup-file-links ()
|
|
|
|
"Delete the files of image links for the current source block result.
|
2019-01-12 19:47:38 -06:00
|
|
|
Do this only if the file exists in
|
|
|
|
`org-babel-jupyter-resource-directory'."
|
2019-01-16 20:30:44 -06:00
|
|
|
(when-let* ((result-pos (org-babel-where-is-src-block-result))
|
|
|
|
(link-re (format "^[ \t]*%s[ \t]*$" org-bracket-link-regexp)))
|
2019-01-13 10:33:21 -06:00
|
|
|
(save-excursion
|
|
|
|
(goto-char result-pos)
|
2019-01-15 16:05:48 -06:00
|
|
|
(forward-line)
|
2019-01-14 19:00:36 -06:00
|
|
|
(let ((bound (org-babel-result-end)))
|
2019-01-15 16:05:48 -06:00
|
|
|
;; This assumes that `jupyter-org-client' only emits bracketed links as
|
|
|
|
;; images
|
2019-01-14 19:00:36 -06:00
|
|
|
(while (re-search-forward link-re bound t)
|
2019-01-26 16:15:21 -06:00
|
|
|
(when-let* ((link-path
|
|
|
|
(org-element-property :path (org-element-context)))
|
|
|
|
(link-dir
|
|
|
|
(when (file-name-directory link-path)
|
|
|
|
(expand-file-name (file-name-directory link-path))))
|
|
|
|
(resource-dir
|
|
|
|
(expand-file-name org-babel-jupyter-resource-directory)))
|
2019-01-14 19:00:36 -06:00
|
|
|
(when (and (equal link-dir resource-dir)
|
|
|
|
(file-exists-p link-path))
|
|
|
|
(delete-file link-path))))))))
|
2019-01-12 19:47:38 -06:00
|
|
|
|
2019-02-20 13:28:13 -06:00
|
|
|
;; We remove the :file parameter in the async case because `org-babel' will
|
|
|
|
;; insert a file link instead of the request ID which we don't want. In the
|
|
|
|
;; synchronous case, in combination with ":results link", that makes sense to
|
|
|
|
;; do so currently the file parameter is kept in the synchronous case.
|
|
|
|
;;
|
|
|
|
;; TODO: What is a better way to handle discrepancies between how `org-mode'
|
|
|
|
;; views header arguments and how `emacs-jupyter' views them? Should the
|
|
|
|
;; strategy be to always try to emulate the `org-mode' behavior?
|
|
|
|
(defun org-babel-jupyter--remove-file-param (params)
|
2019-01-26 18:44:23 -06:00
|
|
|
"Destructively remove the file result parameter from PARAMS.
|
2019-02-20 13:28:13 -06:00
|
|
|
These parameters are handled internally."
|
2019-01-26 18:44:23 -06:00
|
|
|
(let* ((result-params (assq :result-params params))
|
|
|
|
(fresult (member "file" result-params))
|
|
|
|
(fparam (assq :file params)))
|
2019-02-22 08:29:00 -06:00
|
|
|
(setcar fresult "")
|
2019-02-20 13:28:13 -06:00
|
|
|
(delq fparam params)))
|
2019-01-26 18:44:23 -06:00
|
|
|
|
2018-01-22 20:37:12 -06:00
|
|
|
(defun org-babel-execute:jupyter (body params)
|
|
|
|
"Execute BODY according to PARAMS.
|
2018-05-12 14:52:35 -05:00
|
|
|
BODY is the code to execute for the current Jupyter `:session' in
|
|
|
|
the PARAMS alist."
|
2019-01-13 02:20:38 -06:00
|
|
|
(let* ((jupyter-current-client (with-current-buffer
|
|
|
|
(org-babel-jupyter-initiate-session
|
|
|
|
(alist-get :session params) params)
|
|
|
|
jupyter-current-client))
|
|
|
|
(kernel-lang (jupyter-kernel-language jupyter-current-client))
|
2018-06-03 22:55:06 -05:00
|
|
|
(vars (org-babel-variable-assignments:jupyter params kernel-lang))
|
|
|
|
(code (org-babel-expand-body:jupyter body params vars kernel-lang))
|
|
|
|
(req (progn
|
|
|
|
;; This needs to be set to the same parameter object used
|
|
|
|
;; internally by org-babel since insertion of results will
|
|
|
|
;; manipulate it.
|
2019-01-13 02:20:38 -06:00
|
|
|
(oset jupyter-current-client block-params params)
|
|
|
|
(jupyter-send-execute-request jupyter-current-client
|
|
|
|
:code code))))
|
2019-01-12 19:47:38 -06:00
|
|
|
(when (member "replace" (assq :result-params params))
|
2019-01-13 10:33:21 -06:00
|
|
|
(org-babel-jupyter-cleanup-file-links))
|
2018-06-03 22:55:06 -05:00
|
|
|
(cond
|
2018-11-18 22:11:12 -06:00
|
|
|
((or (equal (alist-get :async params) "yes")
|
|
|
|
(plist-member params :async))
|
2019-02-20 13:28:13 -06:00
|
|
|
(when (member "file" (assq :result-params params))
|
|
|
|
(org-babel-jupyter--remove-file-param params))
|
2019-01-12 23:29:59 -06:00
|
|
|
(if (jupyter-org-request-inline-block-p req) ""
|
|
|
|
(jupyter-org-insert-async-id req)))
|
2018-06-03 22:55:06 -05:00
|
|
|
(t
|
|
|
|
(jupyter-wait-until-idle req most-positive-fixnum)
|
2019-01-12 23:29:59 -06:00
|
|
|
(if (jupyter-org-request-inline-block-p req)
|
|
|
|
;; In the case of synchronous inline results, only the result of the
|
|
|
|
;; execute-result message will be added to
|
|
|
|
;; `jupyter-org-request-results', stream results and any display data
|
|
|
|
;; messages will be displayed in a separate buffer.
|
|
|
|
(car (jupyter-org-request-results req))
|
|
|
|
(prog1 (jupyter-org-sync-results req)
|
|
|
|
;; Add after since the initial result params are used in
|
|
|
|
;; `jupyter-org-client'
|
|
|
|
(nconc (alist-get :result-params params) (list "raw"))))))))
|
2018-01-22 20:37:12 -06:00
|
|
|
|
2019-03-01 17:52:59 -06:00
|
|
|
(defvar org-babel-jupyter--babel-ops
|
|
|
|
'("execute" "expand-body" "prep-session" "edit-prep"
|
|
|
|
"variable-assignments" "load-session"))
|
|
|
|
|
|
|
|
(defun org-babel-jupyter--override-restore-src-block (lang restore)
|
|
|
|
(let ((fun (if restore
|
|
|
|
(lambda (sym _ function &rest __)
|
|
|
|
(advice-remove sym function))
|
|
|
|
#'advice-add)))
|
|
|
|
(dolist (fn (cl-set-difference
|
|
|
|
org-babel-jupyter--babel-ops
|
|
|
|
'("variable-assignments" "expand-body")
|
|
|
|
:test #'equal))
|
|
|
|
(let ((sym (intern (concat "org-babel-" fn ":" lang))))
|
|
|
|
;; If a language doesn't have a function assigned, set one so it can be
|
|
|
|
;; overridden
|
|
|
|
(unless (fboundp sym)
|
|
|
|
(fset sym #'ignore))
|
|
|
|
(funcall fun sym
|
|
|
|
:override (intern (concat "org-babel-" fn ":jupyter-" lang)))))
|
|
|
|
(funcall fun (intern (concat "org-babel-" lang "-initiate-session"))
|
|
|
|
:override #'org-babel-jupyter-initiate-session)
|
|
|
|
(dolist (prefix '("org-babel-header-args:"
|
|
|
|
"org-babel-default-header-args:"))
|
|
|
|
(when-let* ((var (intern-soft (concat prefix lang))))
|
|
|
|
(if restore
|
|
|
|
(set var (get var 'jupyter-restore-value))
|
|
|
|
(let ((jupyter-var (intern (concat prefix "jupyter-" lang))))
|
|
|
|
(put var 'jupyter-restore-value (symbol-value var))
|
|
|
|
(set var (symbol-value jupyter-var))))))))
|
|
|
|
|
|
|
|
(defun org-babel-jupyter-override-src-block (lang)
|
|
|
|
"Override the built-in `org-babel' functions for LANG.
|
|
|
|
This overrides functions like `org-babel-execute:LANG' and
|
|
|
|
`org-babel-LANG-initiate-session' to use the machinery of
|
|
|
|
jupyter-LANG source blocks."
|
|
|
|
(org-babel-jupyter--override-restore-src-block lang nil))
|
|
|
|
|
|
|
|
(defun org-babel-jupyter-restore-src-block (lang)
|
|
|
|
"Restore the overridden `org-babel' functions for LANG.
|
|
|
|
See `org-babel-jupyter-override-src-block'."
|
|
|
|
(org-babel-jupyter--override-restore-src-block lang t))
|
|
|
|
|
2018-02-03 23:48:14 -06:00
|
|
|
(defun org-babel-jupyter-make-language-alias (kernel lang)
|
2019-01-12 23:12:56 -06:00
|
|
|
"Similar to `org-babel-make-language-alias' but for Jupyter src-blocks.
|
2018-02-12 11:03:41 -06:00
|
|
|
KERNEL should be the name of the default kernel to use for kernel
|
|
|
|
LANG. All necessary org-babel functions for a language with the
|
2019-02-12 08:55:40 -06:00
|
|
|
name jupyter-LANG will be aliased to the Jupyter functions."
|
2019-03-01 17:52:59 -06:00
|
|
|
(dolist (fn org-babel-jupyter--babel-ops)
|
2018-01-23 13:42:10 -06:00
|
|
|
(let ((sym (intern-soft (concat "org-babel-" fn ":jupyter"))))
|
|
|
|
(when (and sym (fboundp sym))
|
2019-02-12 08:55:40 -06:00
|
|
|
(defalias (intern (concat "org-babel-" fn ":jupyter-" lang)) sym))))
|
|
|
|
(defalias (intern (concat "org-babel-jupyter-" lang "-initiate-session"))
|
2018-02-03 23:48:14 -06:00
|
|
|
'org-babel-jupyter-initiate-session)
|
2019-02-12 08:55:40 -06:00
|
|
|
(set (intern (concat "org-babel-header-args:jupyter-" lang))
|
2018-02-03 23:48:14 -06:00
|
|
|
org-babel-header-args:jupyter)
|
2019-02-12 08:55:40 -06:00
|
|
|
(set (intern (concat "org-babel-default-header-args:jupyter-" lang))
|
2018-02-03 23:48:14 -06:00
|
|
|
`((:kernel . ,kernel)
|
|
|
|
(:async . "no"))))
|
2018-01-23 13:42:10 -06:00
|
|
|
|
2018-02-04 17:35:34 -06:00
|
|
|
(defun org-babel-jupyter-aliases-from-kernelspecs (&optional refresh)
|
|
|
|
"Make language aliases based on the available kernelspecs.
|
|
|
|
For all kernels returned by `jupyter-available-kernelspecs', make
|
|
|
|
a language alias for the kernel language if one does not already
|
|
|
|
exist. The alias is created with
|
|
|
|
`org-babel-jupyter-make-language-alias'.
|
|
|
|
|
|
|
|
Optional argument REFRESH has the same meaning as in
|
|
|
|
`jupyter-available-kernelspecs'."
|
|
|
|
(cl-loop
|
|
|
|
for (kernel . (_dir . spec)) in (jupyter-available-kernelspecs refresh)
|
|
|
|
for lang = (plist-get spec :language)
|
2018-02-12 11:03:41 -06:00
|
|
|
unless (member lang languages) collect lang into languages and
|
2018-02-04 17:35:34 -06:00
|
|
|
do (org-babel-jupyter-make-language-alias kernel lang)
|
2018-10-18 12:49:33 -05:00
|
|
|
(when (assoc lang org-babel-tangle-lang-exts)
|
|
|
|
(add-to-list 'org-babel-tangle-lang-exts
|
2019-02-12 08:55:40 -06:00
|
|
|
(cons (concat "jupyter-" lang)
|
2018-10-18 12:49:33 -05:00
|
|
|
(cdr (assoc lang org-babel-tangle-lang-exts)))))
|
2018-02-04 17:35:34 -06:00
|
|
|
(add-to-list 'org-src-lang-modes
|
2019-02-12 08:55:40 -06:00
|
|
|
(cons (concat "jupyter-" lang)
|
2018-02-04 17:35:34 -06:00
|
|
|
(intern (or (cdr (assoc lang org-src-lang-modes))
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"[0-9]*" "" lang)))))))
|
|
|
|
|
2019-02-12 11:49:17 -06:00
|
|
|
(defvar org-latex-minted-langs)
|
|
|
|
|
|
|
|
(defun org-babel-jupyter-setup-export (backend)
|
|
|
|
"Ensure that Jupyter src-blocks are integrated with BACKEND.
|
|
|
|
Currently this makes sure that Jupyter src-block languages are
|
|
|
|
mapped to their appropriate minted language in
|
|
|
|
`org-latex-minted-langs' if BACKEND is latex."
|
|
|
|
(cond
|
|
|
|
((org-export-derived-backend-p backend 'latex)
|
2019-02-14 16:53:54 -06:00
|
|
|
(cl-loop
|
|
|
|
for (_kernel . (_dir . spec)) in (jupyter-available-kernelspecs)
|
|
|
|
for lang = (plist-get spec :language)
|
|
|
|
do (cl-pushnew (list (intern (concat "jupyter-" lang)) lang)
|
|
|
|
org-latex-minted-langs :test #'equal)))))
|
2019-02-12 11:49:17 -06:00
|
|
|
|
2018-02-04 17:35:34 -06:00
|
|
|
(org-babel-jupyter-aliases-from-kernelspecs)
|
2019-02-12 11:49:17 -06:00
|
|
|
(add-hook 'org-export-before-processing-hook #'org-babel-jupyter-setup-export)
|
2018-01-23 13:42:10 -06:00
|
|
|
|
2018-01-22 20:37:12 -06:00
|
|
|
(provide 'ob-jupyter)
|
|
|
|
|
|
|
|
;;; ob-jupyter.el ends here
|