emacs-jupyter / jupyter

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

FeatureRequest: Support "`\r`"(carriage return) for progress bar #412

Open hwiorn opened 2 years ago

hwiorn commented 2 years ago

Emacs-jupyter ignores \r character in results blocks. But this makes the progress bar which uses \r for clearing a previous text line to useless.

#+begin_src jupyter-python :session tqdm :async yes :results output
from tqdm.std import tqdm
from time import sleep
for i in tqdm(range(1,10)):
    sleep(0.1)
#+end_src

#+RESULTS:
#+begin_example

  0%|          | 0/9 [00:00<?, ?it/s]
 11%|█         | 1/9 [00:00<00:00,  9.97it/s]
 22%|██▏       | 2/9 [00:00<00:00,  9.94it/s]
 33%|███▎      | 3/9 [00:00<00:00,  9.87it/s]
 44%|████▍     | 4/9 [00:00<00:00,  9.88it/s]
 56%|█████▌    | 5/9 [00:00<00:00,  9.90it/s]
 67%|██████▋   | 6/9 [00:00<00:00,  9.89it/s]
 78%|███████▊  | 7/9 [00:00<00:00,  9.90it/s]
 89%|████████▉ | 8/9 [00:00<00:00,  9.90it/s]
100%|██████████| 9/9 [00:00<00:00,  9.89it/s]
100%|██████████| 9/9 [00:00<00:00,  9.89it/s]
#+end_example

This can be really an issue when iteration has a long delay, like dataset downloading or deep learning.

#+begin_src jupyter-python :session tqdm :async yes :results output
from tqdm.std import tqdm
from time import sleep
for i in tqdm(range(1,1000000)): # large iteration
    sleep(0.5)
#+end_src

#+RESULTS:
#+begin_example

  0%|          | 0/999999 [00:00<?, ?it/s]
  0%|          | 1/999999 [00:00<139:07:02,  2.00it/s]
  0%|          | 2/999999 [00:01<139:09:24,  2.00it/s]
  0%|          | 3/999999 [00:01<139:10:22,  2.00it/s]
  0%|          | 4/999999 [00:02<139:12:06,  2.00it/s]
  0%|          | 5/999999 [00:02<139:15:28,  1.99it/s]
  0%|          | 6/999999 [00:03<139:15:23,  1.99it/s]
  0%|          | 7/999999 [00:03<139:15:23,  1.99it/s]
  0%|          | 8/999999 [00:04<139:15:46,  1.99it/s]
  0%|          | 9/999999 [00:04<139:15:29,  1.99it/s]
  0%|          | 10/999999 [00:05<139:16:21,  1.99it/s]
  0%|          | 11/999999 [00:05<139:16:13,  1.99it/s]
  0%|          | 12/999999 [00:06<139:15:38,  1.99it/s]
...

I tried to remove lines before \r like this. But it doesn't work for jupyter-* because emacs-jupyter doesn't prints any carriage return characters.

    (defun org-babel-replace-cr-text ()
      (replace-regexp-in-region "^.+\r" "" (point-min) (point-max)))
    (add-hook 'org-babel-after-execute-hook #'org-babel-replace-cr-text)\
#+begin_src python :results output
from time import sleep
for i in range(10):
    print(f'Iter {i}', end='\r')
    sleep(0.1)
print(f'Iter 10')
#+end_src

#+RESULTS:
Iter 10
lsiksous commented 2 years ago

You can use tqdm.tk instead with :results none

hwiorn commented 2 years ago

@lsiksous Thank you. Yes, I know it. It could be useful on local. But I don't think it works on remote and terminal usage.