5353 :read-only t ))
5454
5555(defvar jupyter--kernelspecs (make-hash-table :test #'equal :size 5 )
56- " A hash table mapping hosts to the kernelspecs available on them." )
56+ " A hash table mapping hosts to the kernelspecs available on them.
57+ The top level hash-table maps hosts to nested hash-tables keyed
58+ on virtual environment path or nil for a system-wide Jupyter
59+ install: hosts[hash-table] -> venv[hash-table] -> kernelspecs." )
60+
61+ (defun jupyter-kernelspecs-ensure-cache (host )
62+ " Return, creating if necessary, the hash-table for HOST."
63+ (let ((cache (gethash host jupyter--kernelspecs)))
64+ (if cache cache
65+ (puthash host (make-hash-table :test #'equal :size 5 )
66+ jupyter--kernelspecs))))
67+
68+ (defun jupyter-kernelspecs-cache-put (host kernelspecs )
69+ " Cache KERNELSPECS available on HOST.
70+ This takes into account any currently active virtual
71+ environment."
72+ (let ((venv (getenv " VIRTUAL_ENV" )))
73+ (let ((cache (jupyter-kernelspecs-ensure-cache host)))
74+ (puthash venv kernelspecs cache))))
75+
76+ (defun jupyter-kernelspecs-cache-get (host )
77+ " Retrieve cached KERNELSPECS available on HOST.
78+ This takes into account any currently active virtual
79+ environment."
80+ (let ((venv (getenv " VIRTUAL_ENV" )))
81+ (let ((cache (jupyter-kernelspecs-ensure-cache host)))
82+ (gethash venv cache))))
5783
5884(defun jupyter-available-kernelspecs (&optional refresh )
5985 " Return the available kernelspecs.
6086Return a list of `jupyter-kernelspec' s available on the host
6187associated with the `default-directory' . If `default-directory'
6288is a remote file name, return the list of available kernelspecs
6389on the remote system. The kernelspecs on the local system are
64- returned otherwise.
90+ returned otherwise (taking into account any currently active
91+ virtual environment).
6592
6693On any system, the list is formed by parsing the output of the
6794shell command
@@ -73,7 +100,7 @@ update of the cached kernelspecs, give a non-nil value to
73100REFRESH."
74101 (let* ((host (or (file-remote-p default-directory) " local" ))
75102 (kernelspecs
76- (or (and (not refresh) (gethash host jupyter-- kernelspecs))
103+ (or (and (not refresh) (jupyter-kernelspecs-cache-get host ))
77104 (let ((specs
78105 (plist-get
79106 (let ((json (or (jupyter-command " kernelspec" " list"
@@ -91,7 +118,7 @@ Jupyter kernelspecs couldn't be parsed from
91118To investiagate further, run that command in a shell and examine
92119why it isn't returning valid JSON. " ))))
93120 :kernelspecs )))
94- (puthash
121+ (jupyter-kernelspecs-cache-put
95122 host
96123 (sort
97124 (cl-loop
@@ -106,8 +133,7 @@ why it isn't returning valid JSON."))))
106133 :plist (plist-get spec :spec )))
107134 (lambda (x y )
108135 (string< (jupyter-kernelspec-name x)
109- (jupyter-kernelspec-name y))))
110- jupyter--kernelspecs)))))
136+ (jupyter-kernelspec-name y)))))))))
111137 kernelspecs))
112138
113139(cl-defgeneric jupyter-kernelspecs (host &optional refresh)
0 commit comments