sagemath / sage

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

Displaying a list of graphic objects prints what appears to be an empty list #1912

Closed jasongrout closed 10 years ago

jasongrout commented 16 years ago

> As a final comment, I'll note that the following behavior with objects
> which automatically display is interesting:
> 
> sage: C=circle((0,0),1);P=plot(sin,0,1)
> sage: [C,P]
> [, ]
> 
> and then a display of circle above a plot of sin (in the notebook) or
> two separate pictures (in the command line).  I have no idea what, if
> any, connection should be made with this work, though.

I think it is just printing out the list for you to see and the "print" function for a graphics object displays the object, so you see each object "printed" out.

It would be nice if the text display indicated this, instead of "[, ]".  Maybe something like "[<Graphic object>, <Graphic object>]", since the objects actually are there.  It misleadingly looks like you have an empty list.

CC: @kcrisman

Component: graphics

Reviewer: Frédéric Chapoton

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

williamstein commented 16 years ago
comment:1

It would be nice if the text display indicated this, instead of "[, ]". Maybe something like "[, ]", since the objects actually are there. It misleadingly looks like you have an empty list.

I agree. However, I have absolutely no clue how or even if this is possible to implement in Python. I almost think it isn't. More precisely, we could easily make it so we have

sage: plot(sin, 0,1)

[an actual image] but that would be really ugly. William
williamstein commented 16 years ago
comment:2

Actually, I vaguely recall there is a "displayhook" in Python that is called when one types

 sage: <arbitrary object>

Maybe we could overload that so if is a list or tuple then instead of calling _repr_ on each graphics object, we call str.

William

It's weird. Every time I think something is impossible I immediately seem to think of a solution...

ba94b9bb-195b-4422-a5e2-176920eaa163 commented 16 years ago
comment:3

Another idea: make it return whenever it creates an object; but in IPython/the notebook, check to see if it's about to print "", and if so print nothing instead.

jasongrout commented 16 years ago
comment:4

It's weird. Every time I think something is impossible I immediately seem to think of a solution...

We ought to put you in charge of more hard problems! So...do you think it's impossible to prove the Riemann hypothesis? :)

jasongrout commented 16 years ago
comment:5

I think either of these solutions works okay, but I prefer the first since it clearly tells the user that they just constructed a list of graphic objects.

sage: [graphic1, graphic2]
# Displays each graphic, as well as:
[<graphic object>, <graphic object>]
sage: [graphic1, graphic2]
# Displays each graphic, but doesn't print anything

Just for comparison, Mathematica 6 actually prints out the graphics, surrounded by the delimiters (so the graphics are really inside the list and appear that way) when using the notebook and prints out just a string representation {-Graphics-} when used from the command line.

williamstein commented 16 years ago
comment:6

This idea of Jason Grout is the sort of thing we can do to solve this sort of problem:

def pretty_print (object):
    if object is None:
        return
    if isinstance(object, (sage.plot.plot.Graphics, sage.plot.plot3d.base.Graphics3d)):
        print repr(object)
        return
    import __builtin__
    __builtin__._=object
    try:
        print html("$$"+latex(object)+"$$")
    except:
        import sys
        sys.__displayhook__(object)

def notebook_pretty(enable=True):
    import sys
    if enable:
        sys.displayhook = pretty_print
    else:
        sys.displayhook = sys.__displayhook__

# To enable the pretty-printing
notebook_pretty(True)
jasongrout commented 16 years ago
comment:7

See #1922 for the above pretty_print functions.

mezzarobba commented 10 years ago
comment:11

Appears to be fixed, most probably as part of #14469. And there is already a doctest covering this behaviour.

fchapoton commented 10 years ago

Reviewer: Frédéric Chapoton

fchapoton commented 10 years ago
comment:13

Ok, I think this can be closed.