Hi everyone,

We're working on removing some warts in the interact implementation. Unfortunately, this involves some changes to how input_box works, as detailed below. If you care, please comment on these changes. If the consensus is that these changes are good, we'll merge these changes into the Sage cell server [1], and eventually they will make it into Sage.

[1] https://github.com/sagemath/sagecell/pull/274

Thanks,

Jason



Make all input_box defaults go through the adapter
==================================================

Current Behavior: If a Sage object is specified as a default value, it is explicitly used as the default value. The adapter is *not* run on the default value.

Proposal: In most cases (except type=str input boxes), repr the default object, and use a default adapter that involves a call to sage_eval. Apply the adapter to the repr of the default. This means that the default value is treated in exactly the same way as user input.

Examples
--------

Default objects
^^^^^^^^^^^^^^^

@interact
def _(n=Integers(3)(2)):
    print n^2

Current: prints 1 initially and shows 2 in the box. If the user changes the 2 in the box to a 3, prints 9, since the user input is just run through sage_eval, and becomes an ordinary integer

Proposed: the default value is sent through repr to get a string, then the string is sent through the default adapter (which uses sage_eval to get a normal integer). So prints 4 initially and shows 2 in the box. If the user changes the 2 in the box to a 3, prints 9. So the user experience is consistent with the initial experience, at the cost of some possible developer confusion since the default value isn't really Integers(3)(2), but rather sage_eval(repr(Integers(3)(2))).

String default values
^^^^^^^^^^^^^^^^^^^^^

@interact
def _(n=input_box("ABCDE")):
    print n

Current: prints ABCDE initially and shows ABCDE in the input box. If the user changes the input box to ABCDEF, an error is thrown since sage_eval is run on the user input, and there is no variable or function named ABCDEF.

Proposed: The default value is sent through repr, then through the default adapter, so prints ABCDE initially and shows 'ABCDE' in the input box (note the quotes). If the user modifies the input box, hopefully they see that quotes are needed to make it a valid string, so they modify it to be 'ABCDEF', and then the interact prints ABCDEF

Note that using type=str will *not* use sage_eval to evaluate inputs (like currently), but will just make the input a string. So

@interact
def _(n=input_box("ABCDE", type=str)):
    print n

or its shortcut:

@interact
def _(n="ABCDE"):
    print n

work the same as before---in both cases, the input is always treated as a string, so n will always be a string, and *not* run through sage_eval. The input box in the interact will *not* display quotes.

Adapter applied to all inputs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@interact
def _(n=input_box("2",type=Integers(3))):
    print n^2

Current: shows 2 in the input box, but prints 22, since the default "2" is not run through the adapter, but is kept as a string. Changing the input box to 3 prints 0.

Proposed: the default value is really Integers(3)(sage_eval(repr("2"))) (which is Integers(3)('2')), so prints 1, but the input box shows '2' (the repr of "2"). Changing the input box to either '3' or 3 prints 0.

As another example:

@interact
def _(n=input_box(2,type=Integers(3))):
    print n^2

Current: shows 2 in the input box, but prints 4, since the default value is not run through the Integers(3) adapter. Changing the 2 to a 3 prints 0.

Proposed: since the default value really is Integers(3)(sage_eval(repr(2))), the input box shows 2 and prints 1 (which makes a lot more sense, I think). If the user changes the 2 to a 3, prints 0.




--
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to