astoff / comint-mime

Display graphics and other MIME attachments in Emacs shells
67 stars 6 forks source link

user-error: ‘comint-mime’ only makes sense in comint buffers #2

Closed hongyi-zhao closed 3 years ago

hongyi-zhao commented 3 years ago

I tried with the following example:

import matplotlib.pyplot as plt
from numpy import arange, sin, pi
t = arange(0, 2*pi, 0.01)
plt.plot(sin(4*t), sin(5*t))

The corresponding Emacs configuration:

(use-package comint-mime
  :straight (:host github :type git :repo "astoff/comint-mime"
             :files (:defaults "*.py" "*.sh"))
  :after elpy
  ;https://github.com/jwiegley/use-package#hooks
  ;https://github.com/astoff/comint-mime#usage
  ;https://mail.google.com/mail/u/0?ik=7b73d6af10&view=om&permmsgid=msg-a%3Ar-1233702610310931433
  ;https://mail.google.com/mail/u/0?ik=7b73d6af10&view=om&permmsgid=msg-a%3Ar5550893479419332762
  :hook (
        ((shell-mode inferior-python-mode) . comint-mime-setup)
        )
  :config
  (when (executable-find "ipython3")
    (setq python-shell-interpreter "ipython3"
          ;python-shell-interpreter-args "--simple-prompt --classic"
          ;https://elpy.readthedocs.io/en/latest/ide.html#interpreter-setup
          python-shell-interpreter-args "-i --simple-prompt --classic --pprint")))

When I run the python code, I meet the following error:

File mode specification error: (user-error ‘comint-mime’ only makes sense in comint buffers)
blank line at end of file [W391] [2 times]
Sent: import matplotlib.pyplot as plt...
Shell native completion is enabled.
Can’t guess python-indent-offset, using defaults: 4
user-error: ‘comint-mime’ only makes sense in comint buffers

Any hints for this problem?

Regards, HZ

astoff commented 3 years ago

comint-mime only makes sense in comint buffers, and you added it to python-mode-hook :-)

hongyi-zhao commented 3 years ago

you added it to python-mode-hook

Thank you for pointing this out. I've removed the corresponding setting.

Then how to reproduce the example shown here? I tried by both M-x eshell and M-x term, and then run the corresponding python codes with ipython and python, but none of them shown the plot in Emacs' frame:

image

astoff commented 3 years ago

You need the good old Python shell, M-x run-python. And by the way, the package needs Emacs 28. Both Emacs 28 and this package are unreleased as of yet.

hongyi-zhao commented 3 years ago

Still failed to do the trick by the following steps:

M-x run-python RET
M-x comint-mime-setup RET

Then input the python codes as following:

image

astoff commented 3 years ago

Are you using Emacs 28, with a freshly compiled master branch?

hongyi-zhao commented 3 years ago

Yes. I use the self compiled git master version: GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0) of 2021-09-09.

astoff commented 3 years ago

Does the other example of the screenshot (with LaTeX output) work? I've found the Matplotlib part of the setup to be finicky. Sometimes typing %matplotlib inline in the shell helps. Otherwise, let me know if you figure this out, because I'm not sure either.

hongyi-zhao commented 3 years ago

Thank you again for your comment. According to the suggestion and documentation described here and here, I created the following ipython configuration file:

$ cat ~/.ipython/profile_default/ipython_config.py
c.InteractiveShellApp.matplotlib = 'inline'

Then I restart Emacs with pyvenv installed, hit C-c C-p followed by C-c C-c, and the example you give is reproduced:

import matplotlib.pyplot as plt
from numpy import arange, pi, sin
from sympy import Symbol, integrate

t = arange(0, 2 * pi, .01)
plt.plot(sin(4 * t), sin(5 * t))
x = Symbol('x')
integrate(1/(1+x ** 3))

image

The complete configuration of the above test is as follows, and see here for relevant discussion:

(use-package comint-mime
  :straight (:host github :type git :repo "astoff/comint-mime"
             :files (:defaults "*.py" "*.sh"))
  :hook ((shell-mode inferior-python-mode) . comint-mime-setup)
  :custom
  (org-format-latex-options '(plist-put org-format-latex-options :scale 1.5))

(use-package pyvenv
  :init
  (setenv "WORKON_HOME" "~/.pyenv/versions")
  ;https://lists.gnu.org/archive/html/help-gnu-emacs/2021-09/msg00535.html
  (defun try/pyvenv-workon ()
    (when (buffer-file-name)
      (let* ((python-version ".python-version")
             (project-dir (locate-dominating-file (buffer-file-name) python-version)))
        (when project-dir
          (pyvenv-workon
            (with-temp-buffer
              (insert-file-contents (expand-file-name python-version project-dir))
              (car (split-string (buffer-string)))))))))
  :config
  (pyvenv-mode 1)

  (setq pyvenv-post-activate-hooks
        (list (lambda ()
                (when (executable-find "ipython3")
                  (setq python-shell-interpreter "ipython3"
                        ;;python-shell-interpreter-args "-i --automagic --simple-prompt --pprint"
                        ;; Or use the following args, without the corresponding setting in `~/.ipython/profile_default/ipython_config.py`
                        python-shell-interpreter-args "-i --matplotlib=inline --automagic --simple-prompt --pprint")))))
  (setq pyvenv-post-deactivate-hooks
        (list (lambda ()
                (setq python-shell-interpreter "python3"))))
  )