millejoh / emacs-ipython-notebook

Jupyter notebook client in Emacs
http://millejoh.github.io/emacs-ipython-notebook/
GNU General Public License v3.0
1.47k stars 122 forks source link

Modified `ein:jupyter-server-use-subcommand` to be a list. #884

Closed jeeger closed 1 year ago

jeeger commented 1 year ago

This allows using more complex subcommands. For example, launching jupyter from poetry can be done by setting ein:jupyter-server-command to "poetry" and ein:jupyter-server-use-subcommand to '("run" "jupyter" "lab")`.

dickmao commented 1 year ago

There are zero to two guys out there whose init.el contains ,

(custom-set-variables '(ein:jupyter-server-use-subcommand "foo"))

which would collide with logic that assumes that variable is now always a list.

With ac92eb92 perhaps try setting the variable to "run jupyter lab".

dcguim commented 1 week ago

@jeeger, would you mind sharing your config for poetry, I tried something like:

(use-package ein
  :ensure t
  :init
  ;; Set default notebook directory to current directory
  (setq ein:jupyter-default-server-command "poetry")
  (setq ein:jupyter-default-server-subcommand '("run" "jupyter" "notebook")) ;; also tried (setq ein:jupyter-default-server-subcommand "run jupyter notebook"))
...

But unfortunately, jupyter is failing to start when running ein:run:

ein: [info] ein:jupyter-server--run: /opt/homebrew/bin/poetry notebook --notebook-dir=~/work/financeapp-back/ --no-browser
ein: [warn] Jupyter server failed to start, cancelling operation

Did you have to explicitly connect emacs with the poetry environment before ein:run-ning?

jeeger commented 1 week ago

I haven't used this in a long time, but this is what used to work.

  (defun ein-detect-poetry-file ()
    (when (and (executable-find "poetry")
               (file-exists-p (file-name-concat (file-name-directory (buffer-file-name)) "pyproject.toml")))
      (setq-local ein:jupyter-server-command "poetry"
                  ein:jupyter-server-use-subcommand '("run" "jupyter" "lab"))))
  (add-hook 'ein:ipynb-mode-hook #'ein-detect-poetry-file)
dcguim commented 1 week ago

Thanks, although I didn't manage to make this ein:jupyter-server-use-subcommand command work, I worked around it with pyvenv, and calling the usual jupyter command.

;; Automatically activate the Poetry virtual environment when starting ein:run
  (defun ein-activate-poetry-venv ()
    "Activate the Poetry virtual environment before running ein:run."
    (let ((venv-path (string-trim (shell-command-to-string "poetryenv"))))
      (when (and venv-path (file-exists-p (concat venv-path "/bin/activate")))
        (pyvenv-activate (concat venv-path "/bin/activate")))))
  (add-hook 'ein:notebooklist-login-hook 'ein-activate-poetry-venv)