astoff / comint-mime Goto Github PK
View Code? Open in Web Editor NEWDisplay graphics and other MIME attachments in Emacs shells
Display graphics and other MIME attachments in Emacs shells
A few small adaptations:
I wanted to try out the package and managed to get it working with the regular shell-mode
, but in the case of python I am not able to get inline images. I set it up as described in the readme.
When trying to produce a plot as follows
>>> import matplotlib.pyplot as plt
>>> plt.plot([1, 1], [0, 1])
I get the following error and result
[<matplotlib.lines.Line2D object at 0x7f7c58f87b50>]
Traceback (most recent call last):
File "/usr/lib/python3.9/base64.py", line 510, in _input_type_check
m = memoryview(s)
TypeError: memoryview: a bytes-like object is required, not 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py", line 41, in show
display(
File "/usr/lib/python3.9/site-packages/IPython/core/display.py", line 327, in display
publish_display_data(data=format_dict, metadata=md_dict, **kwargs)
File "/usr/lib/python3.9/site-packages/IPython/core/display.py", line 119, in publish_display_data
display_pub.publish(
File "/usr/lib/python3.9/site-packages/IPython/core/displaypub.py", line 112, in publish
handler(data[mime], metadata.get(mime, None))
File "<string>", line 33, in print_osc
File "/usr/lib/python3.9/base64.py", line 527, in encodebytes
_input_type_check(s)
File "/usr/lib/python3.9/base64.py", line 513, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str
In the case of the sympy
example, I do not get an error, but the TeX output is not rendered in any form. The example
>>> import sympy
>>> from sympy import Symbol, integrate
>>> x = Symbol("x")
>>> integrate(1 / x)
produces only $\displaystyle \log{\left(x \right)}$
and no inline math. I am running emacs-version
28.0.60 and the comint-mime
version corresponds to the last commit here. For python it's
Python 3.9.9 (main, Nov 20 2021, 21:30:06)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.30.0 -- An enhanced Interactive Python. Type '?' for help.
The comint-mime script can be sent to the python process within Emacs and it does not produce any errors, so if I am missing something I'd be glad about any pointers.
Currently, I'm using the comint-mime
package with the following configuration:
(use-package comint-mime
:straight (:host github :repo "astoff/comint-mime"
:files (:defaults "*.py" "*.sh"))
:hook ((shell-mode inferior-python-mode) . comint-mime-setup))
With the help of this package, I can display python matplotlib plot and LaTeX formula in Emacs comint buffer, as shown in the screenshot below:
But I want to know if I can customize the ipython shell displayed plot and font size in comint buffer. Any hints will be appreciated.
Regards, HZ
On Ubuntu 20.04.3 LTS, I use self-compiled git master Emacs, and installed comint-mime
via straight
's use-package
integration as follows:
(use-package comint-mime
:straight ( :host github
:repo "astoff/comint-mime"
:files (:defaults "*.py" "*.sh"))
;;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)
:custom
;; https://emacs.stackexchange.com/a/33992
;;https://github.com/astoff/comint-mime/issues/4#issuecomment-938384059
(org-format-latex-options '(plist-put org-format-latex-options :scale 1.5))
)
When I issue the M-x shell RET
command, I found that comint-mime
causes the shell prompt to be printed twice as follows:
Are there any tips for solving this problem?
Regards,
HZ
Thanks delivering that interesting package.
For now, when calling M-x comint-mime-setup RET in a shell-buffer, get the error
"Symbol’s value as variable is void: comint-osc-handlers"
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
When using comint-mime in shell-mode, it complains that the command hostname
cannot be found
bash: hostname: command not found
Is there perhaps a more portable way to retrieve the value for hostname? I suppose the variable $HOSTNAME
could be checked (which bash
sets) or if hostname
is not available maybe hostnamectl hostname
could be executed, which does the same on systemd-based systems. If all that fails maybe it could just pass along any string? The error above didn't change the outcome as far as I could see.
Otherwise, it might be sensible to specify that this command is a dependency of the project.
It works when line 99 of comint-mime.el is changed: (url-filename url) --> (url-host url)
Is there a way to set the width/size of the image? Relative to the size of the window?
On Ubuntu 20.04.3 LTS, I installed comint-mime
with the following configuration:
(use-package comint-mime
:straight ( :host github
:repo "astoff/comint-mime"
:files (:defaults "*.py" "*.sh"))
;;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)
:custom
;; https://emacs.stackexchange.com/a/33992
;;https://github.com/astoff/comint-mime/issues/4#issuecomment-938384059
(org-format-latex-options '(plist-put org-format-latex-options :scale 1.5))
)
When I type some python code in the IPython console with the major mode inferior-python-mode
enabled, I find that the Python code can't be indented automatically, as shown below:
Any hints for this problem?
Regards,
HZ
Hi,
I'm trying to use your package with spacemacs, but installing the package and activating it via comint-mime-setup
in the REPL doesn't seem to have any effect. I also tried adding hooks as in your readme. It does work perfectly in emacs -q
(after installing the package).
Do you have any suggestions about how to debug this? So far I've tried to debug by checking that comint-mime-setup
has the desired effect; I checked that comint-osc-handlers
contains ("5151" . comint-mime-osc-handler)
and that comint-output-filter-functions
contains comint-osc-process-output
. These look fine. But I'm not sure what to look at next.
Thanks!
I installed comint-mime
with the following configuration:
(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))
But when I start the python interpreter by C-c C-p
, the following message will be triggered:
run-hooks: Symbol’s value as variable is void: comint-osc-handlers
Shell native completion is disabled, using fallback
Any hints for this problem?
Regards,
HZ
On Ubuntu 20.04.2 LTS, I test this package with the following 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
: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"
)))
I find that it will trigger a very slow auto-completion of company-mode.
Regards,
HZ
In the comint-mime.sh script, line 22:
file=$(realpath -e "$1") || return 1
the -e flag on linux indicates that 'all components of the path must exist'
This -e flag doesn't exist on Mac version of realpath in /bin. Instead realpath will error if the file does not exist. So on Mac, the -e flag can be deleted to have the same effect as realpath on linux (it seems).
On Ubuntu 20.04.3 LTS, I use comint-mime
with the following configuration:
(use-package comint-mime
:straight ( :host github
:repo "astoff/comint-mime"
:files (:defaults "*.py" "*.sh"))
;;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)
:custom
;; https://emacs.stackexchange.com/a/33992
;;https://github.com/astoff/comint-mime/issues/4#issuecomment-938384059
(org-format-latex-options '(plist-put org-format-latex-options :scale 1.5))
)
But I find that ipython console with inferior python mode can't complete many python keywords/hints. See the following for detailed information:
In the normal python mode, the completion will be shown as follows:
Any hints for this problem?
Regards,
HZ
First of all, thanks for creating this nice package!
I have been trying it out with python-mode
via ipython
on Emacs Mac port (v29.1) using the configuration below and it works quite well. However, the resolution of the Matplotlib plots is unfortunately not great; it might be because I'm using a hiDPI / Retina screen (and I know this is handled differently on different Emacs ports), but the text in the Matplotlib figure is quite blurry.
One solution is to use SVG figures instead of PNG figures for the Matplotlib plots. I believe e.g. VSCode offers this as a setting when working with IPython / Jupyter. For comparison, I have added a screenshot comparing how a Matplotlib figure looks in the Inferior Python buffer with comint-mime
(top right) vs. how it looks when I run plt.savefig("test.svg")
and open the corresponding SVG file directly in Emacs image-mode
(bottom). As you can see, the SVG version has much crisper text.
Is there a way to use SVG figures as a format when working with Matplotlib plots in comint-mime
?
My configuration:
(use-package comint-mime
:ensure t
:hook (inferior-python-mode . comint-mime-setup))
(use-package python
:custom
(python-indent-guess-indent-offset t)
(python-indent-guess-indent-offset-verbose nil)
(python-shell-interpreter "ipython3")
(python-shell-interpreter-args "--simple-prompt --classic"))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.