emacs-jupyter / jupyter

An interface to communicate with Jupyter kernels.
GNU General Public License v3.0
934 stars 92 forks source link

[BUG] Images are slow #455

Open NightMachinery opened 1 year ago

NightMachinery commented 1 year ago
#+begin_src jupyter-python :kernel py_base :session /jpy:127.0.0.1#6035:orgk1/ :async yes :exports both
import numpy as np
import matplotlib.pyplot as plt

# Generate random color images
num_images = 4
image_shape = (1000, 1000, 3)  # Image size with three color channels (RGB)

for _ in range(num_images):
    # Generate random image data
    image_data = np.random.rand(*image_shape)

    # Create a figure and axis
    fig, ax = plt.subplots()

    # Display the image
    ax.imshow(image_data)

    # Remove axis ticks and labels
    ax.axis('off')

    # Show the plot
    plt.show()
#+end_src

#+RESULTS:
:RESULTS:
#+attr_org: :width 663
[[file:./.ob-jupyter/e9613479dec1ba311eaf35a0732954705ba09805.png]]
#+attr_org: :width 663
[[file:./.ob-jupyter/89a20965ee741da61412f8983afc32c2b1089966.png]]
#+attr_org: :width 663
[[file:./.ob-jupyter/df3b4e746218456cc9f7fef49b6048e662d433bb.png]]
#+attr_org: :width 663
[[file:./.ob-jupyter/2972bb76e9bdf4ef2543216ab2dbf04b3df64c27.png]]
:END:

This block takes around ~8s to complete. (I manually measured it using a CLI stopwatch.)

The same block completes in ~1s in VSCode, and it will indeed show ~1s if you measure the time using a tool like pytictoc.

So the problem is that somehow emacs-jupyter makes getting the already produced output to the cell results slow.

This makes using emacs-jupyter with code that produces a lot of plots very difficult.

NightMachinery commented 1 year ago
  1. Using :async no is much better for testing this problem, as it will print the actual time taken.
  2. Without :session /jpy:127.0.0.1#6035:orgk1/, the problem is not present:
    #+begin_src jupyter-python :kernel py_base :session emacs_py_1 :async no :exports both

takes 1.2s.

I was port-forwarding /jpy:10.2.32.22#6035:orgk1/ to the localhost from a remote server, so I also tested using /jpy:10.2.32.22#6035:orgk1/ directly. This was still slow. Note that VSCode connects to this same server and has no issues.