malioboro
malioboro

Reputation: 3281

sys.stdout.flush not work in jupyter notebook

So I only want to re-run code from this repo: https://github.com/dennybritz/reinforcement-learning/blob/master/MC/MC%20Prediction%20Solution.ipynb

My focus is on the print's part:

 for i_episode in range(1, num_episodes + 1):
        # Print out which episode we're on, useful for debugging.
        if i_episode % 1000 == 0:
            print "\rEpisode {}/{}.".format(i_episode, num_episodes)
            sys.stdout.flush()

He is using sys.stdout.flush() to create a simple "progress" output. You can see the output from his repo it only show the last Episode iteration 10000/10000 because using sys.stdout.flush()

But when I try to run it in my jupyter notebook (I run using cmd command jupyter notebook) I think sys.stdout.flush() not works, it show every printed iterations, not overwrite the previous one:

Episode 1000/10000.
Episode 2000/10000.
Episode 3000/10000.
Episode 4000/10000.
Episode 5000/10000.
Episode 6000/10000.
Episode 7000/10000.
Episode 8000/10000.
Episode 9000/10000.
Episode 10000/10000.

Am I missing something when run jupyter to make it works?

Upvotes: 3

Views: 4412

Answers (2)

Juan V.
Juan V.

Reputation: 1

Python program to print list without new line character

Declare a List
import time
listfruits = ['Apple', 'Banana', 'Orange', 'Mango']
Print list using for Loop
for fruit in listfruits:
    print('\r'+fruit, end=' ')
    time.sleep(3)

Upvotes: 0

malioboro
malioboro

Reputation: 3281

Charless Duffy comment makes everything clear. The problem of overwriting is not on flush function buat in print function.

I found the solution is just edit the print format from:

print("\rEpisode {}/{}.".format(i_episode, num_episodes))

to

print("\rEpisode {}/{}.".format(i_episode, num_episodes), end="")

and the overwrite works now

Upvotes: 1

Related Questions