# HG changeset patch # User Jason Grout # Date 1273736798 18000 # Node ID ec74250342eadfa4d730a74513821e16640d738e # Parent fb36e936dc34c4cbf26b93b881bcbf956c836b16 #7379: layout interact controls in a table diff -r fb36e936dc34 -r ec74250342ea sagenb/notebook/interact.py --- a/sagenb/notebook/interact.py Fri Apr 30 23:40:22 2010 -0700 +++ b/sagenb/notebook/interact.py Thu May 13 02:46:38 2010 -0500 @@ -1988,13 +1988,23 @@ sage: sagenb.notebook.interact.InteractCanvas([B], 3).render_controls() '...' """ + layout = self.__options.get('layout',None) tbl_body = '' - for c in self.__controls: - if c.label() == '': - tbl_body += '\n'%c.render() - else: - tbl_body += '\n'%( - c.label(), c.render()) + if layout is None: + layout = [[c.var()] for c in self.__controls] + + controls = dict([c.var(), c] for c in self.__controls) + for row in layout: + tbl_body += '' + for c_name in row: + c = controls[c_name] + if c.label() == '': + tbl_body += '\n'%c.render() + else: + tbl_body += '\n'%(c.label(), c.render()) + + tbl_body += '' + return '
%s
%s %s
%s%s %s
%s
'%tbl_body def wrap_in_outside_frame(self, inside): @@ -2099,7 +2109,10 @@ s = """interact(%r, '_interact_.recompute(\\'%s\\')')""" % (cell_id, cell_id) JavascriptCodeButton.__init__(self, "Update", s) -def interact(f): +from sage.misc.misc import decorator_defaults + +@decorator_defaults +def interact(f, layout=None): r""" Use interact as a decorator to create interactive Sage notebook cells with sliders, text boxes, radio buttons, check boxes, and @@ -2112,6 +2125,9 @@ - ``f`` - a Python function + - ``layout`` (optional) - a list of rows of controls, each control + denoted by a string of its variable name. + EXAMPLES: In each example below we use a single underscore for the function @@ -2129,6 +2145,13 @@ ... ... + :: + + sage: @interact(layout=[['a','b'],['d']]) + ... def _(a=x^2, b=(0..20), c=100, d=x+1): print a+b+c+d + ... + ... + Draw a plot interacting with the "continuous" variable ``a``. By default continuous variables have exactly 50 possibilities. @@ -2492,7 +2515,7 @@ i = args.index('auto_update') controls[i] = UpdateButton(SAGE_CELL_ID) - C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update) + C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update, layout=layout) html(C.render()) def _():