astoff / comint-mime

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

mode not working in inferior python #8

Closed jnboehm closed 2 years ago

jnboehm commented 2 years ago

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.

astoff commented 2 years ago

Can you check if the latest commit fixes it for you?

jnboehm commented 2 years ago

It works not with matplotlib, thanks a lot!

But the example with sympy still only returns the math code unrendered. (Although I have to admit that this is not a high priority for me as I don't really use that package myself.)

astoff commented 2 years ago

Ah, yes — see the lines below concerning LaTeX rendering:

https://github.com/astoff/comint-mime/blob/1431605940724318c403a6048049c6a32cc16b0e/comint-mime.el#L64-L65

Probably nothing really to worry there, but it was a suggestion from ELPA people.

jnboehm commented 2 years ago

I see, thanks for the explanation! Maybe it could be mentioned in the readme, but other than that I have no further comments. Thank you!