skip to Main Content

I want to print on the same line in a VS Code notebook but it does not work.

b also does not work well, it requires me to use two b as a group to backspace.

I installed the Jupyter Notebook Extension. And I also tried in a Jupyter notebook, it’s the same as in the VS Code notebook with the extension.

This is the code to reproduce the program:

import time


def test_output():
        print('bb bb: OK', end='', flush=True)
        print(' -> Read: OK', end='', flush=True)
        print(' -> Json: OK', end='', flush=True)
        print(' -> Analysing...', end='', flush=True)


for i in range(15):
    print(f'r{" " * 20}r', end="", flush=True)
    time.sleep(1.5)
    print(f"rtest({i:02d}/15): Opening", end="", flush=True)
    time.sleep(1.5)
    test_output()

and this is the output:

test(14/15): OpeningK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing...

This is the code and output of b not working:

import time


def test_output():
        print('bbb: OK', end='', flush=True)
        print(' -> Read: OK', end='', flush=True)
        print(' -> Json: OK', end='', flush=True)
        print(' -> Analysing...', end='', flush=True)


for i in range(15):
    print(f'r{" " * 20}r', end="", flush=True)
    time.sleep(1.5)
    print(f"rtest({i:02d}/15): Opening", end="", flush=True)
    time.sleep(1.5)
    test_output()
test(14/15): Opening OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing...

These problemr only occur in a notebook, whether in VS Code or Jupyter notebook. But if I run in the terminal, it will work well.

2

Answers


  1. As mentionned here, the notebook only emulates a terminal; since b is based on the cursor position and notebook do not emulate the cursor position, b is not supported.

    Login or Signup to reply.
  2. What is supposed to happen exactly in your demo if it worked like you wanted? You don’t specify.

    Jupyter-based Option

    Since b doesn’t work in Jupyter as others have said:
    This gets close, I think (it’s based on here):

    import time
    
    
    def test_output():
            print('rr rr: OK', end='r', flush=True)
            print(' -> Read: OK', end='r', flush=True)
            print(' -> Json: OK', end='r', flush=True)
            print(' -> Analysing...', end='r', flush=True)
    
    
    for i in range(15):
        print(f'r{" " * 20}r', end="", flush=True)
        time.sleep(1.5)
        print(f"rtest({i:02d}/15): Opening", end="r", flush=True)
        time.sleep(1.5)
        test_output()
    

    I probably needs some adjusting if you say what should happen?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search