A text control is just a block of text (really, HTML) that gets inserted among other controls. This can be useful in interactions that are not in context, for example by providing a brief description of what the interaction does. Unfortunately, the following generates a syntax error:
def f("This function does nothing."):
pass
Doing nothing in Python is perfectly legal, but having a non-variable in the definition isn't. My current implementation isn't pretty:
@interact
def _(t1=text_control("This function does nothing.")):
pass
In working on the control I have come to realize that the structure of interact is indeed limited. Currently, interact takes a function, whose variables are set with defaults as control objects. The design has several major limitations:
- All controls must be tied to a variable, which is not what we always want. For example, there is no reason to tie a block of text to a variable.
- Functions generated for interact cannot be used outside of interact. Similarly, functions generated outside of interact do not make good interactions. One has to write a wrapper function with the right controls.
Mathematica's Manipulate command uses a more logical design: pick any function and pass along a list of controls. For example:
Current
@interact
def f(t1=text_control("Plots a function."),
l=slider(-20, 0, default=-2, label='Lower bound:'),
u=slider(0,20, default=2, label='Upper bound:')):
plot(lambda x:sin(x)/x, l, u).show()
Versus something like:
def f(l,u):
plot(lambda x:sin(x)/x, l, u).show()
interact(f, "Plots a function",
('l', slider(-20, 0, default=-2, label='Lower bound:')),
('u', slider(0,20, default=2, label='Upper bound:')))
Benefits:
- We can put anything in the list: text, numbers (default values with no controls) without associating a variable.
- We can easily make high quality (that is, with appropriate controls, text, etc.) interactions for builtin SAGE functions or any other functions not designed for interact.
- We can nest interactions. Right now, it is possible to create a function, and then call interact on it, but this method is known to break because interactions are limited to one and nothing else per cell.
- I think it is easier to understand for someone who knows Python well but is not familiar with interact.
Well, those were my thoughts on interact right now, but perhaps I am looking at interact from the wrong angle today after messing with text controls. I will continue to perfect the text control, with another control in mind and more long-term plans that involve something completely different. To the reader I leave this mystery and hint only that the idea for the new control came to me yesterday while I was writing the example interaction.