# 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
|
|
1988 | 1988 | sage: sagenb.notebook.interact.InteractCanvas([B], 3).render_controls() |
1989 | 1989 | '<table>...' |
1990 | 1990 | """ |
| 1991 | layout = self.__options.get('layout',None) |
1991 | 1992 | 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 </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 </font></td><td>%s</td>\n'%(c.label(), c.render()) |
| 2005 | |
| 2006 | tbl_body += '</tr>' |
| 2007 | |
1998 | 2008 | return '<table>%s</table>'%tbl_body |
1999 | 2009 | |
2000 | 2010 | def wrap_in_outside_frame(self, inside): |
… |
… |
|
2099 | 2109 | s = """interact(%r, '_interact_.recompute(\\'%s\\')')""" % (cell_id, cell_id) |
2100 | 2110 | JavascriptCodeButton.__init__(self, "Update", s) |
2101 | 2111 | |
2102 | | def interact(f): |
| 2112 | from sage.misc.misc import decorator_defaults |
| 2113 | |
| 2114 | @decorator_defaults |
| 2115 | def interact(f, layout=None): |
2103 | 2116 | r""" |
2104 | 2117 | Use interact as a decorator to create interactive Sage notebook |
2105 | 2118 | cells with sliders, text boxes, radio buttons, check boxes, and |
… |
… |
|
2112 | 2125 | |
2113 | 2126 | - ``f`` - a Python function |
2114 | 2127 | |
| 2128 | - ``layout`` (optional) - a list of rows of controls, each control |
| 2129 | denoted by a string of its variable name. |
| 2130 | |
2115 | 2131 | EXAMPLES: |
2116 | 2132 | |
2117 | 2133 | In each example below we use a single underscore for the function |
… |
… |
|
2129 | 2145 | ... |
2130 | 2146 | <html>... |
2131 | 2147 | |
| 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 | |
2132 | 2155 | Draw a plot interacting with the "continuous" variable ``a``. By |
2133 | 2156 | default continuous variables have exactly 50 possibilities. |
2134 | 2157 | |
… |
… |
|
2492 | 2515 | i = args.index('auto_update') |
2493 | 2516 | controls[i] = UpdateButton(SAGE_CELL_ID) |
2494 | 2517 | |
2495 | | C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update) |
| 2518 | C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update, layout=layout) |
2496 | 2519 | html(C.render()) |
2497 | 2520 | |
2498 | 2521 | def _(): |