emacs-ipython-notebook/lisp/ein-notebooklist.el

880 lines
39 KiB
EmacsLisp
Raw Normal View History

2012-05-07 14:41:15 +02:00
;;; ein-notebooklist.el --- Notebook list buffer
;; Copyright (C) 2018- John M. Miller
2012-05-07 14:41:15 +02:00
;; Authors: Takafumi Arakaki <aka.tkf at gmail.com>
;; John M. Miller <millejoh at mac.com>
2012-05-07 14:41:15 +02:00
;; This file is NOT part of GNU Emacs.
;; ein-notebooklist.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-notebooklist.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-notebooklist.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'widget)
(require 'cus-edit)
(require 'ein-core)
2012-05-07 14:41:15 +02:00
(require 'ein-notebook)
(require 'ein-file)
(require 'ein-contents-api)
(require 'deferred)
(require 'dash)
(require 'ido)
2020-01-07 16:44:22 -05:00
(declare-function ein:jupyterhub-connect "ein-jupyterhub")
(declare-function ein:jupyter-crib-token "ein-jupyter")
(declare-function ein:jupyter-server-conn-info "ein-jupyter")
(declare-function ein:jupyter-get-default-kernel "ein-jupyter")
(declare-function ein:jupyter-crib-running-servers "ein-jupyter")
(defcustom ein:notebooklist-login-timeout (truncate (* 6.3 1000))
"Timeout in milliseconds for logging into server"
:group 'ein
2020-01-02 09:15:27 -05:00
:type 'integer)
2012-05-07 14:41:15 +02:00
2012-07-05 00:13:12 +02:00
(defcustom ein:notebooklist-first-open-hook nil
"Hooks to run when the notebook list is opened at first time.
Example to open a notebook named _scratch_ when the notebook list
is opened at first time.::
(add-hook
'ein:notebooklist-first-open-hook
(lambda () (ein:notebook-open (ein:$notebooklist-url-or-port ein:%notebooklist%) \"main.ipynb\")))
2012-07-05 00:13:12 +02:00
"
:type 'hook
:group 'ein)
(defstruct ein:$notebooklist
"Hold notebooklist variables.
`ein:$notebooklist-url-or-port'
URL or port of IPython server.
`ein:$notebooklist-path'
The path for the notebooklist.
`ein:$notebooklist-data'
JSON data sent from the server.
`ein:$notebooklist-api-version'
Major version of the IPython notebook server we are talking to."
url-or-port
path
data
api-version)
(ein:deflocal ein:%notebooklist% nil
"Buffer local variable to store an instance of `ein:$notebooklist'.")
(ein:deflocal ein:%notebooklist-new-kernel% nil
"Buffer local variable to store kernel type for newly created notebooks.")
(defcustom ein:notebooklist-sort-field :name
"The notebook list sort field."
:type '(choice (const :tag "Name" :name)
(const :tag "Last modified" :last_modified))
:group 'ein)
(make-variable-buffer-local 'ein:notebooklist-sort-field)
(put 'ein:notebooklist-sort-field 'permanent-local t)
(defcustom ein:notebooklist-sort-order :ascending
"The notebook list sort order."
:type '(choice (const :tag "Ascending" :ascending)
(const :tag "Descending" :descending))
:group 'ein)
(make-variable-buffer-local 'ein:notebooklist-sort-order)
(put 'ein:notebooklist-sort-order 'permanent-local t)
(defmacro ein:make-sorting-widget (tag custom-var)
"Create the sorting widget."
;; assume that custom-var has type `choice' of `const's.
`(widget-create
'menu-choice :tag ,tag
:value ,custom-var
2020-01-07 11:40:26 -05:00
:notify (lambda (widget &rest _ignore)
(run-at-time 1 nil #'ein:notebooklist-reload)
(setq ,custom-var (widget-value widget)))
,@(mapcar (lambda (const)
`'(item :tag ,(third const) :value ,(fourth const)))
(rest (custom-variable-type custom-var)))))
(define-obsolete-variable-alias 'ein:notebooklist 'ein:%notebooklist% "0.1.2")
2012-05-07 14:41:15 +02:00
(defvar ein:notebooklist-buffer-name-template "*ein:notebooklist %s*")
(defvar ein:notebooklist-map (make-hash-table :test 'equal)
"Data store for `ein:notebooklist-list'.
Mapping from URL-OR-PORT to an instance of `ein:$notebooklist'.")
(defun ein:notebooklist-keys ()
"Get a list of registered server urls."
(hash-table-keys ein:notebooklist-map))
(defun ein:notebooklist-list ()
"Get a list of opened `ein:$notebooklist'."
(hash-table-values ein:notebooklist-map))
(defun ein:notebooklist-list-remove (url-or-port)
(remhash url-or-port ein:notebooklist-map))
(defun ein:notebooklist-list-add (nblist)
"Register notebook list instance NBLIST for global lookup.
This function adds NBLIST to `ein:notebooklist-map'."
(puthash (ein:$notebooklist-url-or-port nblist)
nblist
ein:notebooklist-map))
2012-05-07 14:41:15 +02:00
2012-07-05 00:13:12 +02:00
(defun ein:notebooklist-list-get (url-or-port)
"Get an instance of `ein:$notebooklist' by URL-OR-PORT as a key."
(gethash url-or-port ein:notebooklist-map))
(defun ein:notebooklist-url (url-or-port version &optional path)
(let ((base-path (cond ((= version 2) "api/notebooks")
((>= version 3) "api/contents"))))
(ein:url url-or-port base-path path)))
(defun ein:notebooklist-sentinel (url-or-port process event)
"Remove URL-OR-PORT from ein:notebooklist-map when PROCESS dies"
(when (not (string= "open" (substring event 0 4)))
(ein:log 'info "Process %s %s %s"
(car (process-command process))
(replace-regexp-in-string "\n$" "" event)
url-or-port)
(ein:notebooklist-list-remove url-or-port)))
2012-05-12 00:34:00 +02:00
(defun ein:notebooklist-get-buffer (url-or-port)
2012-05-12 00:34:00 +02:00
(get-buffer-create
(format ein:notebooklist-buffer-name-template url-or-port)))
2012-05-12 00:34:00 +02:00
(defun ein:notebooklist-token-or-password (url-or-port)
2020-01-02 20:09:42 -05:00
"Return token or password for URL-OR-PORT.
Jupyter requires one or the other but not both.
Return empty string token if all authentication disabled.
Return nil if unclear what, if any, authentication applies."
2020-01-07 16:44:22 -05:00
(multiple-value-bind (password-p token) (ein:jupyter-crib-token url-or-port)
(multiple-value-bind (my-url-or-port my-token) (ein:jupyter-server-conn-info)
(cond ((eq password-p t) (read-passwd (format "Password for %s: " url-or-port)))
((and (stringp token) (eql password-p :json-false)) token)
((equal url-or-port my-url-or-port) my-token)
(t nil)))))
(defun ein:notebooklist-ask-url-or-port ()
2019-11-24 00:17:52 -05:00
(let* ((default (ein:url (aif (ein:get-notebook)
(ein:$notebook-url-or-port it)
2019-11-24 00:17:52 -05:00
(aif ein:%notebooklist%
2020-01-05 18:16:05 -05:00
(ein:$notebooklist-url-or-port it)))))
(url-or-port-list
(-distinct (mapcar #'ein:url
2020-01-05 18:16:05 -05:00
(append (when default
(list default))
(if (atom ein:url-or-port)
(list ein:url-or-port)
ein:url-or-port)
2020-01-07 16:44:22 -05:00
(ein:jupyter-crib-running-servers)))))
2020-01-05 18:16:05 -05:00
(url-or-port (let (ido-report-no-match ido-use-faces)
(ein:completing-read "URL or port: "
url-or-port-list
nil nil nil nil
2020-01-05 18:16:05 -05:00
(car-safe url-or-port-list)))))
(ein:url url-or-port)))
(defun ein:notebooklist-open* (url-or-port &optional path resync restore-point-p callback errback)
"The main entry to server at URL-OR-PORT. Users should not directly call this, but instead `ein:notebooklist-login'.
A \"notebooklist\" can be opened from any PATH within the server root hierarchy. PATH is empty at the root. RESYNC is requery server attributes such as ipython version and kernelspecs. CALLBACK takes two arguments, the resulting buffer and URL-OR-PORT. ERRBACK takes one argument, the resulting buffer.
TODO: going to maintain jupyterhub hooks here
"
(unless path (setq path ""))
(setq url-or-port (ein:url url-or-port)) ;; should work towards not needing this
(lexical-let* ((url-or-port url-or-port)
(path path)
(success (apply-partially #'ein:notebooklist-open--finish
url-or-port restore-point-p callback))
2018-10-28 14:27:09 -04:00
(failure errback))
(if (and (not resync) (ein:notebooklist-list-get url-or-port))
(ein:content-query-contents url-or-port path success failure)
(ein:query-notebook-version
url-or-port
(lambda ()
(ein:query-kernelspecs
url-or-port
(lambda ()
(deferred:$
(deferred:next
(lambda ()
(ein:content-query-hierarchy url-or-port #'ignore))))
(ein:content-query-contents url-or-port path success failure))))))))
2012-05-07 14:41:15 +02:00
(defcustom ein:notebooklist-keepalive-refresh-time 1
"When the notebook keepalive is enabled, the frequency, IN
HOURS, with which to make calls to the jupyter content API to
refresh the notebook connection."
:type 'float
:group 'ein)
(defcustom ein:enable-keepalive nil
"When non-nil, will cause EIN to automatically call
`ein:notebooklist-enable-keepalive' after any call to
`ein:notebooklist-open'."
:type 'boolean
:group 'ein)
2020-01-07 11:40:26 -05:00
(defcustom ein:notebooklist-date-format "%F"
"The format spec for date in notebooklist mode.
See `ein:format-time-string'."
:type '(or string function)
:group 'ein)
(defvar ein:notebooklist--keepalive-timer nil)
;;;###autoload
(defun ein:notebooklist-enable-keepalive (&optional url-or-port)
"Enable periodic calls to the notebook server to keep long running sessions from expiring.
By long running we mean sessions to last days, or weeks. The
frequency of the refresh (which is very similar to a call to
`ein:notebooklist-open`) is controlled by
`ein:notebooklist-keepalive-refresh-time`, and is measured in
terms of hours. If `ein:enable-keepalive' is non-nil this will
automatically be called during calls to `ein:notebooklist-open`."
(interactive (list (ein:notebooklist-ask-url-or-port)))
(unless ein:notebooklist--keepalive-timer
(message "Enabling notebooklist keepalive...")
(let ((success
(lambda (content)
(ein:log 'info "Refreshing notebooklist connection.")))
(refresh-time (* ein:notebooklist-keepalive-refresh-time 60 60)))
(setq ein:notebooklist--keepalive-timer
2018-10-28 14:27:09 -04:00
(run-at-time 0.1 refresh-time #'ein:content-query-contents url-or-port "" success nil)))))
;;;###autoload
(defun ein:notebooklist-disable-keepalive ()
"Disable the notebooklist keepalive calls to the jupyter notebook server."
(interactive)
(message "Disabling notebooklist keepalive...")
(cancel-timer ein:notebooklist--keepalive-timer)
(setq ein:notebooklist--keepalive-timer nil))
(defun ein:notebooklist-open--finish (url-or-port restore-point-p callback content)
"Called via `ein:notebooklist-open*'."
(let ((path (ein:$content-path content))
(nb-version (ein:$content-notebook-version content))
(data (ein:$content-raw-content content)))
(with-current-buffer (ein:notebooklist-get-buffer url-or-port)
(let ((already-opened-p (ein:notebooklist-list-get url-or-port))
(orig-point (if restore-point-p
(point)
(point-min))))
(setq ein:%notebooklist%
(make-ein:$notebooklist :url-or-port url-or-port
:path path
:data data
:api-version nb-version))
(ein:notebooklist-list-add ein:%notebooklist%)
2019-04-03 14:27:32 -05:00
(ein:notebooklist-render nb-version orig-point)
(ein:log 'verbose "Opened notebooklist at %s" (ein:url url-or-port path))
(unless already-opened-p
(run-hooks 'ein:notebooklist-first-open-hook))
(when ein:enable-keepalive
(ein:notebooklist-enable-keepalive url-or-port))
(when callback
(funcall callback (current-buffer) url-or-port)))
(current-buffer))))
2012-05-12 00:34:00 +02:00
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-open-error (url-or-port path
&key error-thrown &allow-other-keys)
(ein:log 'error
"ein:notebooklist-open-error %s: ERROR %s DATA %s" (concat (file-name-as-directory url-or-port) path) (car error-thrown) (cdr error-thrown)))
2012-09-01 20:51:55 +02:00
;;;###autoload
(defun ein:notebooklist-reload (&optional nblist resync callback)
2012-05-13 05:18:38 +02:00
"Reload current Notebook list."
(interactive)
(unless nblist
(setq nblist ein:%notebooklist%))
(when nblist
(ein:notebooklist-open* (ein:$notebooklist-url-or-port nblist)
(ein:$notebooklist-path nblist) resync t
callback)))
2012-05-13 05:18:38 +02:00
(defun ein:notebooklist-refresh-related ()
"Reload notebook list in which current notebook locates.
This function is called via `ein:notebook-after-rename-hook'."
(ein:notebooklist-open* (ein:$notebook-url-or-port ein:%notebook%)
(ein:$notebook-notebook-path ein:%notebook%)))
(add-hook 'ein:notebook-after-rename-hook 'ein:notebooklist-refresh-related)
2017-04-02 14:33:56 -05:00
;;;###autoload
(defun ein:notebooklist-upload-file (upload-path)
(interactive "fSelect file to upload:")
(unless ein:%notebooklist%
(error "Only works when called from an ein:notebooklist buffer."))
(let ((nb-path (ein:$notebooklist-path ein:%notebooklist%)))
(ein:content-upload nb-path upload-path)))
2012-09-01 20:51:55 +02:00
;;;###autoload
2019-02-19 15:29:18 -05:00
(defun ein:notebooklist-new-notebook (url-or-port kernelspec &optional callback no-pop retry)
(interactive (list (ein:notebooklist-ask-url-or-port)
(ein:completing-read
"Select kernel: "
(ein:list-available-kernels
(ein:$notebooklist-url-or-port ein:%notebooklist%))
nil t nil nil "default" nil)))
(let* ((notebooklist (ein:notebooklist-list-get url-or-port))
(path (ein:$notebooklist-path notebooklist))
(version (ein:$notebooklist-api-version notebooklist))
(url (ein:notebooklist-url url-or-port version path)))
(ein:query-singleton-ajax
url
:type "POST"
:data (json-encode '((:type . "notebook")))
Appveyor iterating (WIP) (#573) * ob-ein: Bring back old functionality. Bring back some old features to babel edit buffers while trying to respect recent addition of polymode support. * Override polymode if the user really wants. Polymode is really for notebook buffers in any case, but this will override whatever completion backmode a user has configured for python-mode. * Install cask using python2. For now python2 is the easiest option for testing on Windows since cask does not properly support python3 when in Windows. * Let's throw in the ert-runner, see what happens. * Can I use my fork of cask? Work around smartrep weirdness, try to live without command line wildcard expansion. * Get the url for the fork right. * Experiment with python37, use test_script. * Unstick appveyor, I hope. * Fix parsing error. * test_script is not executing. Why? * Add ert testing. But why are the test_script commands not executing? * tasks: Automate building and testing using invoke. Invoke leverages Python, which I hope will allow us to abstract out differences in platforms when it comes to building and testing ein. * Use invoke on appveyor. * appveyor: Use the environment python. So we can test versions other than python 2.7. * Parsing error. Is https://packaging.python.org/guides/supporting-windows-using-appveyor/#setting-up lying to me? * Quote commands, just in case. * Get python onto the path. * Appveyor is catching up to travis. * Parsing error. * Update pip, try to get quoted syntax right. * Still not liking my pip call. Last try, next step we go to a requirements.txt file. * Go to using a requirements file for pip. * ecukes needs bash to work. * Cleaning up and fiddling. Seems like the emacs-jupyter guy has his act together - maybe we can take some inspiration for our appveyor config. * Syntax error in environment. * More syntax errros. * Maybe we need quoting. * I give up. * Formatting and cleanup. * Add customization, yet another syntax error. New customizable variable `ob-ein-babel-edit-polymode-ignore' to override keybinding for \C-c\C-c in an org source code edit buffer. * John learned some Powershell today. * Fix the executable path. Sometimes there is more than one curl installed on the system, make sure we can account for that in testing. * Handle updating the path inside invoke. * Report which curl we are using before starting functional tests. * Enable RDP so we can see error logs. * Keep the build alive even when it finishes. * Fix #568. Apparently we need to specify the user agent when on windows, otherwise tornado will start throwing 403 responses. Currently using Mozilla/4.0 as the agent, but might be a good idea to make this value customizable. * Clean up emacs config. * Why is appveyor dropping the xsrf token? * xsrf cookie found, what does the header look like? * Try different user-agent header, reenable rdp. * JSON encoding issues on Python side, it appears. Let's try an older Python. Login works, contents query to get notebooklist works (i.e. GET on /contents/api), but creating a notebook (i.e. POST on /contents/api) fails with invalid JSON. ein and emacs-request appear to be generating the proper json, but jupyter notebook does not see the same thing that is being written. Could be bytes vs. text issue with modern v3.x python, so let us see how this all works with Python 2.7. * Python27 does not have pathlib out of the box. * Make amends with Python27 * Back to python37. Tornado/notebook still isn't reading the POST'ed json correctly. * Do we need to specify content type? * Must be selective in specifying application/json content. * Re-enable rdp. * Let's try a different curl. * Ensure most recent curl is on path * Try a different path. * Try to warn user if suspicious curl detected. * Remove debugging statements. * EVM depends on trusty for 26.x See issue #125 (https://github.com/rejeep/evm/issues/125). Let's hope I got the travis.yml syntax right. * Minimal support for ecukes from invoke. * Cleaner server shutdowns, better ecukes support from invoke. Use the /api/shutdown REST API call now to shutdown running server. Also support more command line options for ecukes from invoke. * Almost full support of ecukes using invoke. But! Also disabling integration testing for the time being until I understand why ecukes fails even though everything else is working. * Just do integration and functional testing on appveyor. Better than nothing while I work out what is breaking the integration tests.
2019-07-28 18:20:13 -06:00
:headers (list (cons "Content-Type" "application/json"))
:parser #'ein:json-read
:error (apply-partially #'ein:notebooklist-new-notebook-error
2019-02-19 15:29:18 -05:00
url-or-port kernelspec path callback no-pop retry)
:success (apply-partially #'ein:notebooklist-new-notebook-success
url-or-port kernelspec path callback no-pop))))
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-new-notebook-success (url-or-port
kernelspec
path
callback
no-pop
&key data
&allow-other-keys)
(let ((nbname (plist-get data :name))
(nbpath (plist-get data :path)))
(when (< (ein:notebook-version-numeric url-or-port) 3)
(if (string= nbpath "")
(setq nbpath nbname)
(setq nbpath (format "%s/%s" nbpath nbname))))
(ein:notebook-open url-or-port nbpath kernelspec callback nil no-pop)
(ein:notebooklist-open* url-or-port path nil t)))
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-new-notebook-error
2019-02-19 15:29:18 -05:00
(url-or-port kernelspec path callback no-pop retry
&key symbol-status error-thrown &allow-other-keys)
(let ((notice (format "ein:notebooklist-new-notebook-error: %s %s"
symbol-status error-thrown)))
(if retry
(ein:log 'error notice)
(ein:log 'info notice)
(sleep-for 0 1500)
(ein:notebooklist-new-notebook url-or-port kernelspec callback no-pop t))))
2012-09-01 20:51:55 +02:00
;;;###autoload
(defun ein:notebooklist-new-notebook-with-name
(url-or-port kernelspec name &optional callback no-pop)
"Upon notebook-open, rename the notebook, then funcall CALLBACK."
(interactive
(let* ((url-or-port (or (ein:get-url-or-port)
2020-01-05 18:16:05 -05:00
(if (atom ein:url-or-port)
ein:url-or-port
(car-safe ein:url-or-port))))
(kernelspec (ein:completing-read
"Select kernel: "
(ein:list-available-kernels url-or-port)
nil t nil nil "default" nil))
(name (read-from-minibuffer
(format "Notebook name (at %s): " url-or-port))))
(list url-or-port kernelspec name)))
(unless callback
(setq callback #'ignore))
(add-function :before callback
(apply-partially
(lambda (name* notebook _created)
(with-current-buffer (ein:notebook-buffer notebook)
(ein:notebook-rename-command name*)))
name))
(ein:notebooklist-new-notebook url-or-port kernelspec callback no-pop))
2020-01-12 23:01:26 -05:00
(defun ein:notebooklist-delete-notebook (notebooklist url-or-port path &optional callback)
"CALLBACK with no arguments, e.g., semaphore"
2020-01-12 23:01:26 -05:00
(declare (indent defun))
(unless callback (setq callback #'ignore))
(dolist (buf (seq-filter (lambda (b)
(with-current-buffer b
(aif (ein:get-notebook)
(string= path (ein:$notebook-notebook-path it)))))
(buffer-list)))
(cl-letf (((symbol-function 'y-or-n-p) (lambda (&rest _args) nil)))
(kill-buffer buf)))
(if (ein:notebook-opened-notebooks (lambda (nb)
(string= path
(ein:$notebook-notebook-path nb))))
(ein:log 'error "ein:notebooklist-delete-notebook: cannot close %s" path)
(let ((delete-nb
(apply-partially
(lambda (url* settings* _kernel)
(apply #'ein:query-singleton-ajax url* settings*))
(ein:notebook-url-from-url-and-id
url-or-port (ein:$notebooklist-api-version notebooklist) path)
(list :type "DELETE"
:complete (apply-partially
#'ein:notebooklist-delete-notebook--complete
(ein:url url-or-port path) callback)))))
(ein:message-whir
"Ending session" delete-nb
(ein:kernel-delete-session delete-nb
:url-or-port url-or-port
:path path)))))
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-delete-notebook--complete
(url callback
&key data response symbol-status
&allow-other-keys
&aux (resp-string (format "STATUS: %s DATA: %s" (request-response-status-code response) data)))
(ein:log 'debug "ein:notebooklist-delete-notebook--complete %s" resp-string)
(when callback (funcall callback)))
2012-05-12 00:59:15 +02:00
(defun generate-breadcrumbs (path)
"Given notebooklist path, generate alist of breadcrumps of form (name . path)."
(let* ((paths (split-string path "/" t))
(current-path "/")
(pairs (list (cons "Home" ""))))
(dolist (p paths pairs)
(setf current-path (concat current-path "/" p)
pairs (append pairs (list (cons p current-path)))))))
2019-11-13 17:46:50 -05:00
(cl-defun ein:nblist--sort-group (group by-param order)
(sort group #'(lambda (x y)
(cond ((eql order :ascending)
(string-lessp (plist-get x by-param)
(plist-get y by-param)))
((eql order :descending)
(string-greaterp (plist-get x by-param)
(plist-get y by-param)))))))
(defun ein:notebooklist--order-data (nblist-data sort-param sort-order)
"Try to sanely sort the notebooklist data for the current path."
(let* ((groups (-group-by #'(lambda (x) (plist-get x :type))
nblist-data))
(dirs (ein:nblist--sort-group (cdr (assoc "directory" groups))
sort-param
sort-order))
(nbs (ein:nblist--sort-group (cdr (assoc "notebook" groups))
sort-param
sort-order))
(files (ein:nblist--sort-group (-flatten-n 1 (-map #'cdr (-group-by
#'(lambda (x) (car (last (s-split "\\." (plist-get x :name)))))
(cdr (assoc "file" groups)))))
sort-param
sort-order)))
(-concat dirs nbs files)))
2020-01-07 11:40:26 -05:00
(defun render-header (url-or-port &rest args)
(with-current-buffer (ein:notebooklist-get-buffer url-or-port)
(widget-insert
2020-01-07 11:40:26 -05:00
(format "Contents API %s (%s)\n\n"
(ein:need-notebook-version url-or-port)
url-or-port))
(let ((breadcrumbs (generate-breadcrumbs
(ein:$notebooklist-path ein:%notebooklist%))))
(dolist (p breadcrumbs)
(lexical-let ((url-or-port url-or-port)
(name (car p))
(path (cdr p)))
(widget-insert " | ")
(widget-create
'link
2020-01-07 11:40:26 -05:00
:notify (lambda (&rest _ignore)
(ein:notebooklist-open* url-or-port path nil nil
(lambda (buffer url-or-port)
(pop-to-buffer buffer))))
name)))
(widget-insert " |\n\n"))
(lexical-let* ((url-or-port url-or-port)
(kernels (ein:list-available-kernels url-or-port)))
(widget-create
'link
2020-01-07 11:40:26 -05:00
:notify (lambda (&rest _ignore) (ein:notebooklist-new-notebook
2020-01-07 16:44:22 -05:00
url-or-port
ein:%notebooklist-new-kernel%))
"New Notebook")
(widget-insert " ")
(widget-create
'link
2020-01-07 11:40:26 -05:00
:notify (lambda (&rest _ignore) (ein:notebooklist-reload nil t))
"Resync")
(widget-insert " ")
(widget-create
'link
2020-01-07 11:40:26 -05:00
:notify (lambda (&rest _ignore)
(browse-url (ein:url url-or-port)))
"Open In Browser")
(widget-insert "\n\nCreate New Notebooks Using Kernel:\n")
(let ((radio-widget
(widget-create
'radio-button-choice
:notify (lambda (widget &rest _args)
2020-01-07 11:40:26 -05:00
(let ((update (ein:get-kernelspec url-or-port
(widget-value widget))))
(unless (equal ein:%notebooklist-new-kernel% update)
(when ein:%notebooklist-new-kernel%
(message "New notebooks started with %s kernel"
(ein:$kernelspec-display-name update)))
(setq ein:%notebooklist-new-kernel% update)))))))
(if kernels
2020-01-07 16:44:22 -05:00
(let ((initial (ein:jupyter-get-default-kernel kernels)))
2020-01-07 11:40:26 -05:00
(dolist (k kernels)
(let ((child (widget-radio-add-item
radio-widget
(list 'item
:value (car k)
:format (format "%s\n" (cdr k))))))
(when (string= initial (car k))
(widget-apply-action (widget-get child :button)))))
(widget-insert "\n"))
(widget-insert "\n No kernels found."))))))
(defun ein:format-nbitem-data (name last-modified)
(let ((dt (date-to-time last-modified)))
(format "%-40s%+20s" name
(ein:format-time-string ein:notebooklist-date-format dt))))
(defun render-directory (url-or-port sessions)
;; SESSIONS is a hashtable of path to (session-id . kernel-id) pairs
(with-current-buffer (ein:notebooklist-get-buffer url-or-port)
(widget-insert "\n------------------------------------------\n\n")
(ein:make-sorting-widget "Sort by" ein:notebooklist-sort-field)
(ein:make-sorting-widget "In Order" ein:notebooklist-sort-order)
(widget-insert "\n")
2020-01-12 23:01:26 -05:00
(cl-loop with reloader = (apply-partially (lambda (nblist _kernel)
(ein:notebooklist-reload nblist))
ein:%notebooklist%)
for note in (ein:notebooklist--order-data
2020-01-10 23:58:51 -05:00
(ein:$notebooklist-data ein:%notebooklist%)
ein:notebooklist-sort-field
ein:notebooklist-sort-order)
for name = (plist-get note :name)
for path = (plist-get note :path)
for last-modified = (plist-get note :last_modified)
for type = (plist-get note :type)
for opened-notebook-maybe = (ein:notebook-get-opened-notebook
url-or-port path)
do (widget-insert " ")
if (string= type "directory")
do (progn (widget-create
'link
:notify (lexical-let ((url-or-port url-or-port)
(name name))
(lambda (&rest _ignore)
;; each directory creates a whole new notebooklist
(ein:notebooklist-open* url-or-port
(concat (file-name-as-directory
(ein:$notebooklist-path ein:%notebooklist%))
name)
nil nil
(lambda (buffer url-or-port) (pop-to-buffer buffer)))))
"Dir")
(widget-insert " : " name)
(widget-insert "\n"))
end
if (and (string= type "file") (> (ein:notebook-version-numeric url-or-port) 2))
do (progn (widget-create
'link
:notify (apply-partially #'ein:file-open url-or-port path)
"Open")
(widget-insert " ------ ")
(widget-create
'link
:notify (lexical-let ((path path))
(lambda (&rest _ignore)
(message "[EIN]: NBlist delete file command. Implement me!")))
"Delete")
(widget-insert " : " (ein:format-nbitem-data name last-modified))
(widget-insert "\n"))
end
if (string= type "notebook")
do (progn (widget-create
'link
2020-01-12 23:01:26 -05:00
:notify (apply-partially
(lambda (url-or-port* path* reloader* &rest _args)
(ein:notebook-open url-or-port* path* nil reloader*))
url-or-port path
(apply-partially (lambda (reloader* &rest _args)
(funcall reloader* nil))
reloader))
2020-01-10 23:58:51 -05:00
"Open")
(widget-insert " ")
(if (gethash path sessions)
(widget-create
'link
2020-01-12 23:01:26 -05:00
:notify
(apply-partially
(lambda (callback* url-or-port* path* &rest _ignore)
(ein:message-whir
"Ending session" callback*
(ein:kernel-delete-session callback*
:url-or-port url-or-port*
:path path*)))
reloader url-or-port path)
2020-01-10 23:58:51 -05:00
"Stop")
(widget-insert "------"))
(widget-insert " ")
(widget-create
'link
2020-01-12 23:01:26 -05:00
:notify (apply-partially
(lambda (notebooklist* url-or-port* path* callback*
&rest _args)
(when (or noninteractive
(y-or-n-p (format "Delete notebook %s?" path*)))
(ein:notebooklist-delete-notebook
notebooklist* url-or-port* path*
(apply-partially callback* nil))))
ein:%notebooklist% url-or-port path reloader)
2020-01-10 23:58:51 -05:00
"Delete")
(widget-insert " : " (ein:format-nbitem-data name last-modified))
(widget-insert "\n"))
end)))
2019-04-03 14:27:32 -05:00
(defun ein:notebooklist-render (nb-version &optional restore-point)
"Render notebook list widget.
Notebook list data is passed via the buffer local variable
`ein:notebooklist-data'."
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer))
(remove-overlays)
(let ((url-or-port (ein:$notebooklist-url-or-port ein:%notebooklist%)))
2018-10-28 14:27:09 -04:00
(ein:content-query-sessions url-or-port
2019-04-03 14:27:32 -05:00
(apply-partially #'ein:notebooklist-render--finish nb-version url-or-port restore-point)
2018-10-28 14:27:09 -04:00
nil)))
2019-04-03 14:27:32 -05:00
(defun ein:notebooklist-render--finish (nb-version url-or-port restore-point sessions)
2020-01-07 11:40:26 -05:00
(render-header url-or-port sessions)
(render-directory url-or-port sessions)
(with-current-buffer (ein:notebooklist-get-buffer url-or-port)
(ein:notebooklist-mode)
(widget-setup)
2019-04-03 14:27:32 -05:00
(goto-char (or restore-point (point-min)))))
2012-05-07 14:41:15 +02:00
;;;###autoload
(defun ein:notebooklist-list-paths (&optional content-type)
"Return all files of CONTENT-TYPE for all sessions"
(apply #'append
2020-01-02 20:09:42 -05:00
(cl-loop for nblist in (ein:notebooklist-list)
for url-or-port = (ein:$notebooklist-url-or-port nblist)
collect
2020-01-02 20:09:42 -05:00
(cl-loop for content in (ein:content-need-hierarchy url-or-port)
when (or (null content-type)
(string= (ein:$content-type content) content-type))
collect (ein:url url-or-port (ein:$content-path content))))))
(defun ein:notebooklist-parse-nbpath (nbpath)
"Return `(,url-or-port ,path) from URL-OR-PORT/PATH"
2020-01-02 20:09:42 -05:00
(cl-loop for url-or-port in (ein:notebooklist-keys)
if (cl-search url-or-port nbpath :end2 (length url-or-port))
return (list (substring nbpath 0 (length url-or-port))
(substring nbpath (1+ (length url-or-port))))
end
finally (ein:display-warning
(format "%s not among: %s" nbpath (ein:notebooklist-keys))
:error)))
(defsubst ein:notebooklist-ask-path (&optional content-type)
(ein:completing-read (format "Open %s: " content-type)
(ein:notebooklist-list-paths content-type)
nil t))
2012-09-01 20:51:55 +02:00
;;;###autoload
2012-08-26 22:12:49 +02:00
(defun ein:notebooklist-load (&optional url-or-port)
"Load notebook list but do not pop-up the notebook list buffer.
For example, if you want to load notebook list when Emacs starts,
add this in the Emacs initialization file::
(add-to-hook 'after-init-hook 'ein:notebooklist-load)
or even this (if you want fast Emacs start-up)::
;; load notebook list if Emacs is idle for 3 sec after start-up
(run-with-idle-timer 3 nil #'ein:notebooklist-load)
2020-01-10 23:58:51 -05:00
You should setup `ein:url-or-port' in order to make this code work."
(ein:notebooklist-open* url-or-port))
2020-01-10 23:58:51 -05:00
2012-12-17 16:51:31 +01:00
;;; Login
2018-10-17 19:34:19 -04:00
(defun ein:notebooklist-login--iteration (url-or-port callback errback token iteration response-status)
(ein:log 'debug "Login attempt #%d in response to %s from %s."
2018-10-17 19:34:19 -04:00
iteration response-status url-or-port)
(unless callback
(setq callback #'ignore))
(unless errback
(setq errback #'ignore))
(ein:query-singleton-ajax
(ein:url url-or-port "login")
;; do not use :type "POST" here (see git history)
:timeout ein:notebooklist-login-timeout
:data (if token (concat "password=" (url-hexify-string token)))
:parser #'ein:notebooklist-login--parser
:complete (apply-partially #'ein:notebooklist-login--complete url-or-port)
:error (apply-partially #'ein:notebooklist-login--error url-or-port token
callback errback iteration)
:success (apply-partially #'ein:notebooklist-login--success url-or-port callback
errback token iteration)))
;;;###autoload
(defun ein:notebooklist-open (url-or-port callback)
"This is now an alias for ein:notebooklist-login"
(interactive `(,(ein:notebooklist-ask-url-or-port)
,(lambda (buffer url-or-port) (pop-to-buffer buffer))))
(ein:notebooklist-login url-or-port callback))
(make-obsolete 'ein:notebooklist-open 'ein:notebooklist-login "0.14.2")
;;;###autoload
(defalias 'ein:login 'ein:notebooklist-login)
(defun ein:notebooklist-ask-user-pw-pair (user-prompt pw-prompt)
"Currently used for cookie and jupyterhub additional inputs. If we need more than one cookie, we first need to ask for how many. Returns list of name and content."
(plist-put nil (intern (read-no-blanks-input (format "%s: " user-prompt)))
(read-no-blanks-input (format "%s: " pw-prompt))))
;;;###autoload
(defun ein:notebooklist-login (url-or-port callback &optional cookie-plist)
2018-10-17 17:44:11 -04:00
"Deal with security before main entry of ein:notebooklist-open*.
CALLBACK takes two arguments, the buffer created by ein:notebooklist-open--success
and the url-or-port argument of ein:notebooklist-open*."
(interactive `(,(ein:notebooklist-ask-url-or-port)
,(lambda (buffer url-or-port) (pop-to-buffer buffer))
,(if current-prefix-arg (ein:notebooklist-ask-user-pw-pair "Cookie name" "Cookie content"))))
(unless callback (setq callback (lambda (buffer url-or-port))))
(when cookie-plist
(let* ((parsed-url (url-generic-parse-url (file-name-as-directory url-or-port)))
(domain (url-host parsed-url))
(securep (string-match "^wss://" url-or-port)))
2020-01-02 20:09:42 -05:00
(cl-loop for (name content) on cookie-plist by (function cddr)
2020-01-11 16:03:54 -05:00
for line = (mapconcat #'identity (list domain "FALSE" (car (url-path-and-query parsed-url)) (if securep "TRUE" "FALSE") "0" (symbol-name name) (concat content "\n")) "\t")
do (write-region line nil (request--curl-cookie-jar) 'append))))
2018-12-02 15:23:20 -05:00
(let ((token (ein:notebooklist-token-or-password url-or-port)))
(cond ((null token) ;; don't know
(ein:notebooklist-login--iteration url-or-port callback nil nil -1 nil))
((string= token "") ;; all authentication disabled
(ein:log 'verbose "Skipping login %s" url-or-port)
(ein:notebooklist-open* url-or-port nil nil nil callback nil))
2018-12-02 15:23:20 -05:00
(t (ein:notebooklist-login--iteration url-or-port callback nil token 0 nil)))))
(defun ein:notebooklist-login--parser ()
(goto-char (point-min))
(list :bad-page (re-search-forward "<input type=.?password" nil t)))
2018-10-28 14:27:09 -04:00
(defun ein:notebooklist-login--success-1 (url-or-port callback errback)
(ein:log 'info "Login to %s complete." url-or-port)
(ein:notebooklist-open* url-or-port nil nil nil callback errback))
(defun ein:notebooklist-login--error-1 (url-or-port error-thrown response errback)
(ein:log 'error "Login to %s failed, error-thrown %s, raw-header %s"
url-or-port
(subst-char-in-string ?\n ?\ (format "%s" error-thrown))
(request-response--raw-header response))
(funcall errback))
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-login--complete (url-or-port
&key data response
&allow-other-keys
&aux (resp-string (format "STATUS: %s DATA: %s" (request-response-status-code response) data)))
(ein:log 'debug "ein:notebooklist-login--complete %s" resp-string))
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-login--success (url-or-port callback errback token iteration
&key data response error-thrown
&allow-other-keys
&aux (response-status (request-response-status-code response)))
(cond ((plist-get data :bad-page)
(if (>= iteration 0)
(ein:notebooklist-login--error-1 url-or-port error-thrown response errback)
(setq token (read-passwd (format "Password for %s: " url-or-port)))
(ein:notebooklist-login--iteration url-or-port callback errback token (1+ iteration) response-status)))
((request-response-header response "x-jupyterhub-version")
(let ((pam-plist (ein:notebooklist-ask-user-pw-pair "User" "Password")))
(destructuring-bind (user pw)
2020-01-02 20:09:42 -05:00
(cl-loop for (user pw) on pam-plist by (function cddr)
return (list (symbol-name user) pw))
(ein:jupyterhub-connect url-or-port user pw callback))))
(t (ein:notebooklist-login--success-1 url-or-port callback errback))))
2019-11-13 17:46:50 -05:00
(cl-defun ein:notebooklist-login--error
(url-or-port token callback errback iteration
&key data symbol-status response error-thrown
&allow-other-keys
&aux (response-status (request-response-status-code response)))
(cond ((and response-status (< iteration 0))
(setq token (read-passwd (format "Password for %s: " url-or-port)))
2018-10-17 19:34:19 -04:00
(ein:notebooklist-login--iteration url-or-port callback errback token (1+ iteration) response-status))
((and (eq response-status 403) (< iteration 1))
(ein:notebooklist-login--iteration url-or-port callback errback token (1+ iteration) response-status))
((and (eq symbol-status 'timeout) ;; workaround for url-retrieve backend
(eq response-status 302)
(request-response-header response "set-cookie"))
2018-10-28 14:27:09 -04:00
(ein:notebooklist-login--success-1 url-or-port callback errback))
(t (ein:notebooklist-login--error-1 url-or-port error-thrown response errback))))
2018-12-11 08:48:40 -05:00
(defun ein:get-url-or-port--notebooklist ()
(when (ein:$notebooklist-p ein:%notebooklist%)
(ein:$notebooklist-url-or-port ein:%notebooklist%)))
2012-05-15 03:41:08 +02:00
;;; Notebook list mode
2012-05-15 04:16:06 +02:00
(defun ein:notebooklist-prev-item () (interactive) (move-beginning-of-line 0))
(defun ein:notebooklist-next-item () (interactive) (move-beginning-of-line 2))
(defvar ein:notebooklist-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map (make-composed-keymap widget-keymap
special-mode-map))
(define-key map "\C-c\C-r" 'ein:notebooklist-reload)
(define-key map "\C-c\C-f" 'ein:file-open)
(define-key map "\C-c\C-o" 'ein:notebook-open)
(define-key map "p" 'ein:notebooklist-prev-item)
(define-key map "n" 'ein:notebooklist-next-item)
map)
"Keymap for ein:notebooklist-mode.")
(easy-menu-define ein:notebooklist-menu ein:notebooklist-mode-map
"EIN Notebook List Mode Menu"
`("EIN Notebook List"
,@(ein:generate-menu
'(("Reload" ein:notebooklist-reload)
("New Notebook" ein:notebooklist-new-notebook)
("New Notebook (with name)"
ein:notebooklist-new-notebook-with-name)))))
2012-05-15 03:41:08 +02:00
(define-derived-mode ein:notebooklist-mode special-mode "ein:notebooklist"
"IPython notebook list mode.
Commands:
2018-09-04 11:54:37 -04:00
\\{ein:notebooklist-mode-map}"
(set (make-local-variable 'revert-buffer-function)
2020-01-07 11:40:26 -05:00
(lambda (&rest _args) (ein:notebooklist-reload))))
2012-05-07 14:41:15 +02:00
(provide 'ein-notebooklist)
;;; ein-notebooklist.el ends here