Ask Your Question
1

show() does not show in the right place

asked 8 years ago

stefanutti gravatar image

updated 8 years ago

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)
G.allow_multiple_edges(True)
G.add_edge(1,2,"blue")
G.add_edge(2,3,"green")
G.add_edge(3,1,"red")
G.add_edge(1,4,"green")
G.add_edge(2,4,"red")
G.add_edge(3,4,"blue")

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

    print ("BEFORE PLOT ", i)
    G.plot()
    print ("AFTER PLOT ", i)
Preview: (hide)

Comments

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 ( 8 years ago )

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 ( 8 years ago )

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 ( 8 years ago )

I'm using the cloud sagemath worksheet. sagews

stefanutti gravatar imagestefanutti ( 8 years ago )

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 ( 8 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 8 years ago

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.

Preview: (hide)
link

Comments

It did work! Thanks you

stefanutti gravatar imagestefanutti ( 8 years ago )
1

I thought of an easy way to change SMC to have the behavior you expect and made a ticket: https://github.com/sagemathinc/smc/is...

William Stein gravatar imageWilliam Stein ( 8 years ago )

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

Stats

Asked: 8 years ago

Seen: 1,032 times

Last updated: May 31 '16