Opened 7 years ago

Last modified 5 years ago

#18302 new defect

Deprecate / warn against show()

Reported by: vbraun Owned by:
Priority: major Milestone: sage-6.7
Component: misc Keywords:
Cc: ncohen Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

The show() function is an alias for pretty_print(). The latter is far less confusing, its just like print() except that it makes use of rich output (graphics etc). But just show(x) is hardly self-explanatory, and in fact it is often misused in our own tutorials, e.g.

sage: P = parametric_plot(...)
sage: show(P)    # no need to write show() here

If you know just a little bit of Python then you would't type in

sage: x = 123
sage: print(x)

but many users are not aware of the show <-> print analogy.

Change History (17)

comment:1 Changed 7 years ago by novoselt

I would hate to change all my code that uses show, yet I think this ticket is a great idea.

comment:2 Changed 7 years ago by mantepse

I don't understand this. I thought:

sage: g = graphs.RandomGNP(20,.5)
sage: g
RandomGNP(20,0.500000000000000): Graph on 20 vertices
sage: print g
RandomGNP(20,0.500000000000000)
sage: latex(g)
...tikz code for use in paper
sage: show(g)
...open graph in display

is what I should expect (and it's also what I expect). I use these features a lot, so please let me know what I should do instead!

comment:3 Changed 7 years ago by vbraun

If you want to see a plot of the graph use one of the following two

sage: plot(g)
sage: g.plot()

Or use pretty_print(g) instead of show(g), though whether or not that results in a picture will depend on the user interface capabilities.

comment:4 follow-up: Changed 7 years ago by kcrisman

I know this has been discussed elsewhere, but since there is an actual ticket for this... What would the recommended syntax be going forward for interacts, where simply printing the plot doesn't work (as far as I recall, neither in sagenb nor in sagecell)? That is

@interact
def _():
    P = plot(x)
    P

wouldn't show the plot, you need to use show(P) (or did need to). This is a pretty important use case, though of course it would be nice not to have to do show(P), we should still support this working for the immediate future. (And don't say to do P.show(), because that's even more annoying and less likely to occur to someone new to OOP.)

comment:5 follow-ups: Changed 7 years ago by vbraun

Since the current interacts don't work at all with the IPython notebook we could just keep the old behavior in @interact on SageNB / SageCell. Its somewhat unsatisfactory as it doesn't give you a way to move forward. There could be a switch @interact(old_show=False) though. In any case we need some decision about what to do with interacts in the IPython notebook, and we should probably use IPython's own machinery for interacts.

Another solution would be as I suggested in #18292, have every top-level statement in an @interact-function show its rich output. Then you don't need to call show / pretty_print in the first place. But if you did then that would be ok, too, as it returns None and then the result of the statement isn't displayed.

comment:6 in reply to: ↑ 5 Changed 7 years ago by kcrisman

In any case we need some decision about what to do with interacts in the IPython notebook, and we should probably use IPython's own machinery for interacts.

Sure, of course. I didn't even know ipynb supported interacts of this kind, though Jason G. has been doing a lot with the next-level widgets, I guess.

It would also be nice to eventually have one unified interact interface in SMC, Sage cell, sagenb, ipynb, but maybe that is wishing for too much coordination (and I understand that sagenb can't handle some stuff without an upgrade of some kind or maybe using Ipython (better?) or something). But that is way beyond this ticket.

comment:7 in reply to: ↑ 5 Changed 7 years ago by vbraun

Replying to vbraun:

Another solution would be as I suggested in #18292, have every top-level statement in an @interact-function show its rich output.

On second thought, that is too much magic to be understandable. Just print a warning that you should use pretty_print instead of show.

comment:8 Changed 5 years ago by jdemeyer

Instead of pretty_print() or show(), there is an obvious third possibility: display() which is what IPython uses. This works in the current Sage-Jupyter notebook:

P = plot(sin(x))
from IPython.display import display
display(P)

I'm not saying that Sage should use exactly the display function from IPython, but maybe have a Sage-specific display function which does what show currently does?

comment:9 follow-up: Changed 5 years ago by kcrisman

Now that Jupyter does support interacts, my comment:4 applies again. I really don't know why everyone thinks show is so bad, particularly because to non-programmers "show" is a word while "pretty_print" has an underscore and is confusing. Printing a plot should tell me stuff about a plot, and printing it prettily should print that stuff prettily.

On the other hand, display seems reasonable, though perhaps superfluous to just replace show with display?

comment:10 in reply to: ↑ 4 ; follow-up: Changed 5 years ago by jdemeyer

Replying to kcrisman:

That is

@interact
def _():
    P = plot(x)
    P

wouldn't show the plot

Of course this wouldn't show the plot: the statement P doesn't do anything. It's exactly the same as a regular non-interactive function:

def f():
    P = plot(x)
    P

f()

This wouldn't show the plot either.

If you change the last line to return P then it does show the plot in Jupyter.

comment:11 in reply to: ↑ 10 ; follow-up: Changed 5 years ago by kcrisman

Of course this wouldn't show the plot: the statement P doesn't do anything.

But the point is that outside a function it does do something. So we can't just say show() is unnecessary; in this case it is programmatically necessary to have something, and show/display is the normal English word you might use with a picture.

comment:12 in reply to: ↑ 11 ; follow-up: Changed 5 years ago by jdemeyer

Replying to kcrisman:

Of course this wouldn't show the plot: the statement P doesn't do anything.

But the point is that outside a function it does do something.

Right: executing code in a function is different from executing the same code directly in a cell. This is standard Python stuff, I don't understand how that is related to the discussion of show().

comment:13 in reply to: ↑ 12 Changed 5 years ago by kcrisman

Of course this wouldn't show the plot: the statement P doesn't do anything.

But the point is that outside a function it does do something.

Right: executing code in a function is different from executing the same code directly in a cell. This is standard Python stuff, I don't understand how that is related to the discussion of show().

It's related because not every Sage user can be expected to be an expert (at least not immediately) in "standard Python stuff". It is not obvious that behavior changes inside functions when you are just cutting/pasting @interacts. So completely deprecating show might make teaching people the "right" thing to do even harder - at least if it were in favor of pretty_print. But I've exhausted this discussion for now.

comment:14 in reply to: ↑ 9 Changed 5 years ago by paulmasson

Replying to kcrisman:

I really don't know why everyone thinks show is so bad

I agree with Karl-Dieter on this. I think show is self-explanatory (particularly to people from Missouri), and it's what Mathematica uses. If it does no harm to include it when expecting output, then it helps to make code consistent for users who are either new or employ interacts more than the command line. When I assembled some documentation of special functions for rendering with SageMathCell, I used show in places where it didn't need to be just to keep the code consistent.

And FWIW, I don't use pretty_print because it just sounds silly to me. What's wrong with show?

comment:15 Changed 5 years ago by was

I think this ticket should be closed.

comment:16 Changed 5 years ago by jdemeyer

Personally, I also prefer the name show to pretty_print. The latter reminds me to much of "printing" and I think that displaying a plot for example isn't really "printing" anything. To be fair, displaying an equation as LaTeX is pretty printing. But show/pretty_print do more than that.

Still, I remind my proposal to use the name display (instead of or in addition to pretty_print/show) simply because that's what IPython upstream uses.

Last edited 5 years ago by jdemeyer (previous) (diff)

comment:17 Changed 5 years ago by vbraun

I think switching to display is also a great idea (btw GAP also uses Display). We would be consistent with ourselves AND jupyter...

That Mathematica uses Show[] is another point against as that language is known to get everything wrong ;-)

Though the name display is currently not available as line magics can also be called without percent (not sure why), so the following are equivalent

sage: display text plain
sage: display
Display preferences:
* graphics is not specified
* supplemental_plot = never
* text = plain
sage: %display unicode_art
sage: %display
Display preferences:
* graphics is not specified
* supplemental_plot = never
* text = unicode_art
Note: See TracTickets for help on using tickets.