Ticket #7379: trac-7379-interact-table-layout.patch

File trac-7379-interact-table-layout.patch, 3.0 KB (added by jason, 11 years ago)

FOR SAGENB REPOSITORY

  • sagenb/notebook/interact.py

    # HG changeset patch
    # User Jason Grout <jason-sage@creativetrax.com>
    # 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 b  
    19881988            sage: sagenb.notebook.interact.InteractCanvas([B], 3).render_controls()
    19891989            '<table>...'
    19901990        """
     1991        layout = self.__options.get('layout',None)
    19911992        tbl_body = ''
    1992         for c in self.__controls:
    1993             if c.label() == '':
    1994                 tbl_body += '<tr><td colspan=2>%s</td></tr>\n'%c.render()
    1995             else:
    1996                 tbl_body += '<tr><td align=right><font color="black">%s&nbsp;</font></td><td>%s</td></tr>\n'%(
    1997                 c.label(), c.render())
     1993        if layout is None:
     1994            layout = [[c.var()] for c in self.__controls]
     1995           
     1996        controls = dict([c.var(), c] for c in self.__controls)
     1997        for row in layout:
     1998            tbl_body += '<tr>'
     1999            for c_name in row:
     2000                c = controls[c_name]
     2001                if c.label() == '':
     2002                    tbl_body += '<td colspan=2>%s</td>\n'%c.render()
     2003                else:
     2004                    tbl_body += '<td align=right><font color="black">%s&nbsp;</font></td><td>%s</td>\n'%(c.label(), c.render())
     2005                   
     2006            tbl_body += '</tr>'
     2007       
    19982008        return '<table>%s</table>'%tbl_body
    19992009
    20002010    def wrap_in_outside_frame(self, inside):
     
    20992109        s = """interact(%r, '_interact_.recompute(\\'%s\\')')""" % (cell_id, cell_id)
    21002110        JavascriptCodeButton.__init__(self, "Update", s)
    21012111       
    2102 def interact(f):
     2112from sage.misc.misc import decorator_defaults
     2113
     2114@decorator_defaults
     2115def interact(f, layout=None):
    21032116    r"""
    21042117    Use interact as a decorator to create interactive Sage notebook
    21052118    cells with sliders, text boxes, radio buttons, check boxes, and
     
    21122125
    21132126    - ``f`` - a Python function
    21142127
     2128    - ``layout`` (optional) - a list of rows of controls, each control
     2129      denoted by a string of its variable name.
     2130
    21152131    EXAMPLES:
    21162132
    21172133    In each example below we use a single underscore for the function
     
    21292145        ...
    21302146        <html>...
    21312147
     2148    ::
     2149
     2150        sage: @interact(layout=[['a','b'],['d']])
     2151        ... def _(a=x^2, b=(0..20), c=100, d=x+1): print a+b+c+d
     2152        ...
     2153        <html>...
     2154
    21322155    Draw a plot interacting with the "continuous" variable ``a``.  By
    21332156    default continuous variables have exactly 50 possibilities.
    21342157
     
    24922515        i = args.index('auto_update')
    24932516        controls[i] = UpdateButton(SAGE_CELL_ID)
    24942517
    2495     C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update)
     2518    C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update, layout=layout)
    24962519    html(C.render())
    24972520
    24982521    def _():