Ticket #1322: manipulate-take3.patch

File manipulate-take3.patch, 5.7 KB (added by was, 13 years ago)

this is independent of the patches above -- it's standalone -- see comments below for how to use.

  • sage/server/notebook/all.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1204367134 21600
    # Node ID be4a79848ce310affa812de14c452e2021e342e7
    # Parent  5db8b4f0c24b27fee4b5287154af7a7b374d4d34
    trac #1322 -- a completely different approach to manipulate (fresh patch)
    
    diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/all.py
    a b from notebook_object import notebook, in 
    1313from notebook_object import notebook, inotebook
    1414
    1515from sagetex import sagetex
     16
     17from manipulate import manipulate
  • sage/server/notebook/cell.py

    diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/cell.py
    a b class Cell(Cell_generic): 
    290290    def computing(self):
    291291        return self in self.__worksheet.queue()
    292292
     293    def is_manipulating(self):
     294        return hasattr(self, 'manipulate')
     295
     296    def stop_manipulating(self):
     297        if self.is_manipulating():
     298            del self.manipulate
     299
    293300    def set_input_text(self, input):
     301        if input.startswith('%manipulate'):
     302            self.manipulate = input[len('%manipulate')+1:]
     303            self.__version = 1+self.version()
     304            return
     305        elif self.is_manipulating():
     306            del self.manipulate
     307           
    294308        self.__version = 1+self.version()
    295309        self.__in = input
    296310        if hasattr(self, '_html_cache'):
    class Cell(Cell_generic): 
    315329        self.__in = new_text
    316330
    317331    def set_output_text(self, output, html, sage=None):
     332        if self.is_manipulating():
     333            self._manipulate_output = (output,html)
     334            return
     335           
    318336        if hasattr(self, '_html_cache'):
    319337            del self._html_cache
    320338        output = output.replace('\r','')
    class Cell(Cell_generic): 
    370388            x = x.replace(s,begin + s[7:-1] + end)
    371389        return x
    372390
    373     def output_text(self, ncols=0, html=True, raw=False):
     391    def output_text(self, ncols=0, html=True, raw=False, allow_manipulate=True):
     392        if allow_manipulate and hasattr(self, '_manipulate_output'):
     393            z = self.output_text(ncols,html,raw,allow_manipulate=False)
     394            try:
     395                # Fill in the input valuees
     396                if hasattr(self, 'manipulate'):
     397                    inp = self.manipulate
     398                    i = inp.lstrip().find('\n'); inp = inp[:i]
     399                    i = inp.rfind('.'); inp = inp[i+1:]
     400                    var, value = inp[:i].split('=')
     401                    z = z.replace('<?%s>'%var, value)
     402               
     403                # Fill in the output template
     404                output,html = self._manipulate_output
     405                z = z.replace('<?TEXT>', output.lstrip())
     406                z = z.replace('<?HTML>', html)
     407
     408               
     409                return z
     410            except (ValueError, AttributeError), msg:
     411                print msg
     412                pass
     413           
    374414        s = self.__out
    375415       
    376416        if raw:
  • sage/server/notebook/js.py

    diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/js.py
    a b function show_help_window(worksheet) { 
    24962496}
    24972497
    24982498
     2499///////////////////////////////////////////////////////////////////
     2500// Dynamic / Manipulate
     2501///////////////////////////////////////////////////////////////////
     2502
     2503function dynamic(id, input) {
     2504    active_cell_list = active_cell_list.concat([id]);
     2505    async_request(worksheet_command('eval'), evaluate_cell_callback,
     2506            'newcell=0' + '&id=' + id + '&input='+escape0('%manipulate\n' + input));
     2507}
     2508
     2509
     2510
    24992511/********************* js math ***************************/
    25002512
    25012513
  • new file sage/server/notebook/manipulate.py

    diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/manipulate.py
    - +  
     1SAGE_CELL_ID=0
     2
     3def _text_box(f, var):
     4    print """<html>
     5    <input type='text' value='<?%s>' onchange='dynamic(%s, "sage.server.notebook.manipulate.%s="+this.value+"\\n%s()");'></input>
     6    <table bgcolor=black cellpadding=3><tr><td bgcolor=white>
     7<?TEXT>
     8    <table border=0 width=800px>
     9    <tr><td align=center>  <?HTML>  </td></tr></table>
     10    </td></tr></table>
     11    </html>
     12    """%(var, SAGE_CELL_ID, var, f.__name__)
     13
     14def manipulate(f):
     15    """
     16    Decorate a function f to make a manipulatable version of f.
     17    """
     18    import inspect
     19    vars = inspect.getargspec(f)[0]
     20    print _text_box(f, vars[0])
     21    def g():
     22        return f(eval(vars[0]))
     23    return g   
  • sage/server/notebook/worksheet.py

    diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/worksheet.py
    a b class Worksheet: 
    12031203           
    12041204        D = C.directory()
    12051205        if not C.introspect():
    1206             I = C.input_text().strip()
     1206            if hasattr(C, 'manipulate'):
     1207                I = C.manipulate
     1208            else:
     1209                I = C.input_text().strip()
    12071210            if I in ['restart', 'quit', 'exit']:
    12081211                self.restart_sage()
    12091212                S = self.system()
    class Worksheet: 
    12401243
    12411244        # TODOss
    12421245        os.system('chmod -R a+rw "%s"'%absD)
     1246
     1247        # This is useful mainly for manipulate -- it allows
     1248        # a cell to know it's ID.
     1249        input += 'sage.server.notebook.manipulate.SAGE_CELL_ID=%s\n'%(C.id())
    12431250       
    12441251        if C.time():
    12451252            input += '__SAGE_t__=cputime()\n__SAGE_w__=walltime()\n'