Ask Your Question
1

show() does not show in the right place

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

stefanutti gravatar image

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

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)
edit retag flag offensive close merge delete

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 ( 2016-05-31 11:17:01 +0100 )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 +0100 )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 +0100 )edit

I'm using the cloud sagemath worksheet. sagews

stefanutti gravatar imagestefanutti ( 2016-05-31 18:29:53 +0100 )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 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

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

Comments

It did work! Thanks you

stefanutti gravatar imagestefanutti ( 2016-06-01 00:19:19 +0100 )edit
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 ( 2016-06-02 01:01:28 +0100 )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

Stats

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

Seen: 969 times

Last updated: May 31 '16