@@ -147,12 +147,42 @@ CLIENT must be communicating with a `jupyter-server-kernel', see
147147
148148(defvar jupyter-notebook-procs nil )
149149
150- (defun jupyter-launch-notebook (&optional authentication )
151- " Launch a Jupyter notebook with optional AUTHENTICATION.
152- The notebook is launched in the ~ directory."
153- (let ((port (car (jupyter-available-local-ports 1 ))))
150+ (defvar jupyter-default-notebook-port 8888 )
151+
152+ (defun jupyter-port-available-p (port )
153+ " Return non-nil if PORT is available."
154+ (let ((proc
155+ (condition-case nil
156+ (make-network-process
157+ :name " jupyter-port-available-p"
158+ :server t
159+ :host " 127.0.0.1"
160+ :service port)
161+ (file-error nil ))))
162+ (when proc
163+ (prog1 t
164+ (delete-process proc)))))
165+
166+ (defun jupyter-launch-notebook (&optional port authentication )
167+ " Launch a Jupyter notebook on PORT with AUTHENTICATION.
168+ If PORT is nil, launch the notebook on the
169+ `jupyter-default-notebook-port' if available. Launch the
170+ notebook on a random port otherwise. Return the actual port
171+ used.
172+
173+ If AUTHENTICATION is t, use the default, token, authentication of
174+ a Jupyter notebook. If AUTHENTICATION is a string, it is
175+ interpreted as the password to the notebook. Any other value of
176+ AUTHENTICATION means the notebook is not authenticated."
177+ (let ((port (if port
178+ (if (jupyter-port-available-p port)
179+ port
180+ (error " Port %s not available " port))
181+ (if (jupyter-port-available-p jupyter-default-notebook-port)
182+ jupyter-default-notebook-port
183+ (car (jupyter-available-local-ports 1 ))))))
154184 (prog1 port
155- (let ((buffer (generate-new-buffer " *jupyter-notebook-proc *" ))
185+ (let ((buffer (generate-new-buffer " *jupyter-notebook*" ))
156186 (args (append
157187 (list " notebook" " --no-browser" " --debug"
158188 (format " --NotebookApp.port=%s " port))
@@ -171,13 +201,16 @@ The notebook is launched in the ~ directory."
171201 (setq jupyter-notebook-procs
172202 (cl-loop for (port . proc) in jupyter-notebook-procs
173203 if (process-live-p proc) collect (cons port proc)))
174- (message " Launching notebook process... " )
175204 (push
176205 (cons port
177- (apply #'start-process
206+ (apply #'start-file- process
178207 " jupyter-notebook" buffer " jupyter" args))
179208 jupyter-notebook-procs)
180- (sleep-for 5 )))))
209+ (with-current-buffer buffer
210+ (jupyter-with-timeout ((format " Launching notebook process on port %s ... " port) 5 )
211+ (save-excursion
212+ (goto-char (point-min ))
213+ (re-search-forward " Jupyter Notebook.+running at:$" nil t ))))))))
181214
182215(defun jupyter-notebook-process (server )
183216 " Return a process object for the notebook associated with SERVER.
0 commit comments