2017-02-15 14:18:16 -06:00
|
|
|
;;; ein-jupyter.el --- Manage the jupyter notebook server
|
|
|
|
|
2017-02-15 15:06:38 -06:00
|
|
|
;; Copyright (C) 2017 John M. Miller
|
2017-02-15 14:18:16 -06:00
|
|
|
|
|
|
|
;; Authors: John M. Miller <millejoh at mac.com>
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ein-jupyter.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.
|
|
|
|
|
|
|
|
;; ein-jupyter.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
|
|
|
|
;; along with ein-jupyter.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2017-02-16 09:19:40 -06:00
|
|
|
(require 'ein-core)
|
|
|
|
(require 'ein-notebooklist)
|
|
|
|
|
2017-02-15 14:18:16 -06:00
|
|
|
(defcustom ein:jupyter-server-buffer-name "*ein:jupyter-server*"
|
2017-02-15 15:02:07 -06:00
|
|
|
"The name of the buffer to run a jupyter notebook server
|
|
|
|
session in."
|
|
|
|
:group 'ein
|
|
|
|
:type 'string)
|
2017-02-15 14:18:16 -06:00
|
|
|
|
2017-02-16 08:11:58 -06:00
|
|
|
(defcustom ein:jupyter-default-server-command nil
|
|
|
|
"If you are tired of always being queried for the location of
|
|
|
|
the jupyter command, you can set it here for future calls to
|
|
|
|
`ein:jupyter-server-start'"
|
|
|
|
:group 'ein
|
|
|
|
:type '(file))
|
|
|
|
|
2017-02-20 18:07:17 -06:00
|
|
|
(defcustom ein:jupyter-server-args nil
|
|
|
|
"Add any additional command line options you wish to include
|
|
|
|
with the call to the jupyter notebook."
|
|
|
|
:group 'ein
|
|
|
|
:type '(repeat string))
|
|
|
|
|
2017-02-16 08:11:58 -06:00
|
|
|
(defcustom ein:jupyter-default-notebook-directory nil
|
|
|
|
"If you are tired of always being queried for the location of
|
|
|
|
the notebook directory, you can set it here for future calls to
|
|
|
|
`ein:jupyter-server-start'"
|
|
|
|
:group 'ein
|
|
|
|
:type '(directory))
|
|
|
|
|
2017-02-15 14:18:16 -06:00
|
|
|
(defvar *ein:jupyter-server-accept-timeout* 60)
|
|
|
|
(defvar %ein:jupyter-server-session% nil)
|
2017-02-16 08:11:58 -06:00
|
|
|
|
2017-02-15 14:18:16 -06:00
|
|
|
(defvar *ein:last-jupyter-command* nil)
|
|
|
|
(defvar *ein:last-jupyter-directory* nil)
|
|
|
|
|
|
|
|
(defun ein:jupyter-server--cmd (path dir)
|
2017-02-20 18:07:17 -06:00
|
|
|
(append (list path
|
|
|
|
"notebook"
|
|
|
|
(format "--notebook-dir=%s" dir))
|
|
|
|
ein:jupyter-server-args))
|
|
|
|
|
2017-02-15 14:18:16 -06:00
|
|
|
|
2017-02-15 15:06:38 -06:00
|
|
|
;;;###autoload
|
2017-02-15 15:02:07 -06:00
|
|
|
(defun ein:jupyter-server-start (server-path server-directory)
|
|
|
|
"Start the jupyter notebook server at the given path.
|
|
|
|
|
|
|
|
This command opens an asynchronous process running the jupyter
|
|
|
|
notebook server and then tries to detect the url and token to
|
|
|
|
generate automatic calls to `ein:notebooklist-login' and
|
|
|
|
`ein:notebooklist-open'.
|
|
|
|
|
|
|
|
On executing the command will prompt the user for the path to the
|
|
|
|
jupyter executable and the path for the root directory containing
|
|
|
|
the notebooks the user wants to access.
|
|
|
|
|
|
|
|
The buffer named by `ein:jupyter-server-buffer-name' will contain
|
|
|
|
the log of the running jupyter server."
|
2017-02-15 14:18:16 -06:00
|
|
|
(interactive (list
|
2017-02-16 08:11:58 -06:00
|
|
|
(read-file-name "Server Command: " default-directory nil nil (or *ein:last-jupyter-command*
|
|
|
|
ein:jupyter-default-server-command))
|
|
|
|
(read-directory-name "Notebook Directory: " (or *ein:last-jupyter-directory*
|
|
|
|
ein:jupyter-default-notebook-directory))))
|
2017-02-15 14:18:16 -06:00
|
|
|
(assert (and (file-exists-p server-path)
|
2017-02-16 08:11:58 -06:00
|
|
|
|
2017-02-15 14:18:16 -06:00
|
|
|
(file-executable-p server-path))
|
|
|
|
t "Command %s is not valid!" server-path)
|
|
|
|
(setf *ein:last-jupyter-command* server-path
|
|
|
|
*ein:last-jupyter-directory* server-directory)
|
|
|
|
(if (buffer-live-p (get-buffer ein:jupyter-server-buffer-name))
|
|
|
|
(message "Notebook session is already running, check the contents of %s"
|
|
|
|
ein:jupyter-server-buffer-name))
|
|
|
|
(message "Starting notebook server in directory: %s" server-directory)
|
|
|
|
(let* ((proc (make-process :name "EIN: Jupyter notebook server"
|
|
|
|
:buffer ein:jupyter-server-buffer-name
|
|
|
|
:command (ein:jupyter-server--cmd server-path server-directory))))
|
|
|
|
(setq %ein:jupyter-server-session% proc)
|
|
|
|
(if (accept-process-output proc *ein:jupyter-server-accept-timeout*)
|
2017-02-16 16:57:37 -06:00
|
|
|
(with-current-buffer (process-buffer proc)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(loop for x upfrom 0 by 1
|
|
|
|
until (or (search-forward "Notebook is running at:" nil t)
|
|
|
|
(> x 100))
|
|
|
|
do (progn (sit-for 0.1)
|
|
|
|
(goto-char (point-min)))
|
|
|
|
finally (ein:jupyter-server-login-and-open))))))
|
2017-02-15 14:18:16 -06:00
|
|
|
|
|
|
|
(defun ein:jupyter-server-login-and-open ()
|
2017-02-16 09:01:14 -06:00
|
|
|
"Log in and open a notebooklist buffer for a running jupyter notebook server.
|
|
|
|
|
|
|
|
Determine if there is a running jupyter server (started via a
|
|
|
|
call to `ein:jupyter-server-start') and then try to guess if
|
|
|
|
token authentication is enabled. If a token is found use it to generate a
|
|
|
|
call to `ein:notebooklist-login' and once authenticated open the notebooklist buffer
|
|
|
|
via a call to `ein:notebooklist-open'."
|
2017-02-15 14:18:16 -06:00
|
|
|
(interactive)
|
|
|
|
(when (buffer-live-p (get-buffer ein:jupyter-server-buffer-name))
|
|
|
|
(multiple-value-bind (url-or-port token) (ein:jupyter-server-conn-info)
|
|
|
|
(ein:notebooklist-login url-or-port token)
|
2017-02-15 18:58:30 -06:00
|
|
|
(sit-for 1.0) ;; FIXME: Do better!
|
2017-02-15 14:18:16 -06:00
|
|
|
(ein:notebooklist-open url-or-port))))
|
|
|
|
|
2017-02-15 15:06:38 -06:00
|
|
|
;;;###autoload
|
2017-02-15 14:18:16 -06:00
|
|
|
(defun ein:jupyter-server-stop ()
|
2017-02-15 15:02:07 -06:00
|
|
|
"Stop a running jupyter notebook server.
|
|
|
|
|
|
|
|
Use this command to stop a running jupyter notebook server. If
|
|
|
|
there is no running server then no action will be taken.
|
|
|
|
"
|
2017-02-15 14:18:16 -06:00
|
|
|
(interactive)
|
2017-02-15 15:02:07 -06:00
|
|
|
(when (and %ein:jupyter-server-session%
|
|
|
|
(y-or-n-p "Kill jupyter server and close all open notebooks?"))
|
|
|
|
(let ((unsaved (ein:notebook-opened-notebooks #'ein:notebook-modified-p))
|
|
|
|
(check-for-saved (make-hash-table :test #'equal)))
|
|
|
|
(when unsaved
|
|
|
|
(loop for nb in unsaved
|
|
|
|
when (y-or-n-p (format "Save notebook %s before stopping the server?" (ein:$notebook-notebook-name nb)))
|
|
|
|
do (progn
|
|
|
|
(setf (gethash (ein:$notebook-notebook-name nb) check-for-saved) t)
|
|
|
|
(ein:notebook-save-notebook nb 0
|
|
|
|
#'(lambda (name check-hash)
|
|
|
|
(remhash name check-hash))
|
|
|
|
(list (ein:$notebook-notebook-name nb) check-for-saved)))))
|
|
|
|
(loop for x upfrom 0 by 1
|
|
|
|
until (or (= (hash-table-count check-for-saved) 0)
|
|
|
|
(> x 1000000))
|
|
|
|
do (sit-for 0.1)))
|
|
|
|
(mapc #'ein:notebook-close (ein:notebook-opened-notebooks))
|
2017-02-15 14:18:16 -06:00
|
|
|
(delete-process %ein:jupyter-server-session%)
|
|
|
|
(kill-buffer ein:jupyter-server-buffer-name)
|
|
|
|
(setq %ein:jupyter-server-session% nil)
|
|
|
|
(message "Stopped Jupyter notebook server.")))
|
|
|
|
|
|
|
|
(defun ein:jupyter-server-conn-info ()
|
|
|
|
(with-current-buffer ein:jupyter-server-buffer-name
|
|
|
|
(goto-char (point-min))
|
|
|
|
(re-search-forward "\\(https?://.*:[0-9]+\\)/\\?token=\\(.*\\)" nil t)
|
|
|
|
(let ((url-or-port (match-string 1))
|
|
|
|
(token (match-string 2)))
|
|
|
|
(list url-or-port token))))
|
|
|
|
|
2017-02-15 15:06:38 -06:00
|
|
|
(provide 'ein-jupyter)
|