Ask Your Question

show() does not show in the right place

asked 2016-05-31 10:36:00 +0200

stefanutti gravatar image

updated 2016-05-31 17:49:25 +0200

Why the show() funtion does not show in the right position?

It should show between the BEFORE and AFTER prints, but actually if you run the code, it will appear right before the first print (see the code): print ("BEFORE SHOW ", i)

print ("Create a graph")
G = Graph(sparse=True)

for i in range(5):
    print ("BEFORE SHOW ", i)
    print ("AFTER SHOW ", i)

    print ("BEFORE PLOT ", i)
    print ("AFTER PLOT ", i)
edit retag flag offensive close merge delete


What do you mean by "not in the right position"? (Your question is very unclear to me!)

B r u n o gravatar imageB r u n o ( 2016-05-31 11:17:01 +0200 )edit

It should show between the BEFORE and AFTER prints, but actually if you run the code, it will appear right before the first print: print ("BEFORE SHOW ", i)

stefanutti gravatar imagestefanutti ( 2016-05-31 11:27:19 +0200 )edit

show() isn't necessarily designed to "show" in a particular spot. It is designed to give a graphical representation of whatever you have (not necessarily a plot), but no guarantees to placement, because it was designed for command-line usage originally. Which GUI are you using here - the Sage cell, sagenb, or Sage Math Cloud? It would likely depend on this.

kcrisman gravatar imagekcrisman ( 2016-05-31 17:51:19 +0200 )edit

I'm using the cloud sagemath worksheet. sagews

stefanutti gravatar imagestefanutti ( 2016-05-31 18:29:53 +0200 )edit

I think the original post is questioning the order in which the outputs are displayed in the output of the SMC cell. If my input is something like:

print 1

print 2

I would expect the 1 to be displayed before the 2; in this case, you expect ("BEFORE SHOW ",0) to be the first line of output, followed by the image of the graph, yes? But that's not what it is doing. The topmost output is the graph, then ("BEFORE SHOW ",0), etc.

Is that the question?

JEFLSU gravatar imageJEFLSU ( 2016-05-31 20:25:31 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-31 20:52:47 +0200

William Stein gravatar image

The problem is Python output buffering. Put sys.stdout.flush() after each print statement to ensure that the output is flushed to the output stream immediately, so you can see it then. The show command always flushes immediately, which is why it appears before.

edit flag offensive delete link more


It did work! Thanks you

stefanutti gravatar imagestefanutti ( 2016-06-01 00:19:19 +0200 )edit

I thought of an easy way to change SMC to have the behavior you expect and made a ticket:

William Stein gravatar imageWilliam Stein ( 2016-06-02 01:01:28 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-05-31 10:36:00 +0200

Seen: 911 times

Last updated: May 31 '16