sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.37k stars 466 forks source link

Deprecate / warn against show() #18302

Open vbraun opened 9 years ago

vbraun commented 9 years ago

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.

CC: @nathanncohen

Component: misc

Issue created by migration from https://trac.sagemath.org/ticket/18302

novoselt commented 9 years ago
comment:1

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

mantepse commented 9 years ago
comment:2

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!

vbraun commented 9 years ago
comment:3

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.

kcrisman commented 9 years ago
comment:4

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.)

vbraun commented 9 years ago
comment:5

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.

kcrisman commented 9 years ago
comment:6

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.

vbraun commented 9 years ago
comment:7

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.

jdemeyer commented 7 years ago
comment:8

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?

kcrisman commented 7 years ago
comment:9

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?

jdemeyer commented 7 years ago
comment:10

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.

kcrisman commented 7 years ago
comment:11

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.

jdemeyer commented 7 years ago
comment:12

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().

kcrisman commented 7 years ago
comment:13

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.

53959995-fd20-47f5-85e6-5e769b863d1f commented 7 years ago
comment:14

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?

williamstein commented 7 years ago
comment:15

I think this ticket should be closed.

jdemeyer commented 7 years ago
comment:16

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.

vbraun commented 7 years ago
comment:17

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