Ticket #1322: manipulate.patch

File manipulate.patch, 10.8 KB (added by Jason Grout, 15 years ago)

A different very rough cut of functionality.

  • sage/server/notebook/all.py

    # HG changeset patch
    # User Jason Grout <grout@rayunion.org>
    # Date 1202934757 21600
    # Node ID 1fef7442d16170b5024027f0bfabfd6fe94b58ac
    # Parent  ebe94321a279acbcf6613271b82748a2462d64f7
    [mq]: manipulate.patch
    
    diff -r ebe94321a279 -r 1fef7442d161 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 widgets.all import *
  • sage/server/notebook/js.py

    diff -r ebe94321a279 -r 1fef7442d161 sage/server/notebook/js.py
    a b function continue_update_check() { 
    20262026    }
    20272027}
    20282028
     2029function manipulate_cell(id, cmd, variable, value) {
     2030    var cell_input = get_cell(id)
     2031    cell_input.value = variable+"="+value+ "\n"+decode64(cmd);
     2032    evaluate_cell(id, 0);
     2033}
     2034
     2035
    20292036///////////////////////////////////////////////////////////////////
    20302037// Slideshow Functions
    20312038///////////////////////////////////////////////////////////////////
    function is_submit(e) { 
    25292536  return key_generic_submit(e);
    25302537}
    25312538
     2539//////////////////////////////////////////////////////////////////
     2540// The following header applies to the encode64 and decode64 functions
     2541//
     2542// This code was written by Tyler Akins and has been placed in the
     2543// public domain.  It would be nice if you left this header intact.
     2544// Base64 code from Tyler Akins -- http://rumkin.com
     2545
     2546var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
     2547
     2548function encode64(input) {
     2549   var output = "";
     2550   var chr1, chr2, chr3;
     2551   var enc1, enc2, enc3, enc4;
     2552   var i = 0;
     2553
     2554   do {
     2555      chr1 = input.charCodeAt(i++);
     2556      chr2 = input.charCodeAt(i++);
     2557      chr3 = input.charCodeAt(i++);
     2558
     2559      enc1 = chr1 >> 2;
     2560      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
     2561      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
     2562      enc4 = chr3 & 63;
     2563
     2564      if (isNaN(chr2)) {
     2565         enc3 = enc4 = 64;
     2566      } else if (isNaN(chr3)) {
     2567         enc4 = 64;
     2568      }
     2569
     2570      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
     2571         keyStr.charAt(enc3) + keyStr.charAt(enc4);
     2572   } while (i < input.length);
     2573   
     2574   return output;
     2575}
     2576
     2577function decode64(input) {
     2578   var output = "";
     2579   var chr1, chr2, chr3;
     2580   var enc1, enc2, enc3, enc4;
     2581   var i = 0;
     2582
     2583   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
     2584   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
     2585
     2586   do {
     2587      enc1 = keyStr.indexOf(input.charAt(i++));
     2588      enc2 = keyStr.indexOf(input.charAt(i++));
     2589      enc3 = keyStr.indexOf(input.charAt(i++));
     2590      enc4 = keyStr.indexOf(input.charAt(i++));
     2591
     2592      chr1 = (enc1 << 2) | (enc2 >> 4);
     2593      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
     2594      chr3 = ((enc3 & 3) << 6) | enc4;
     2595
     2596      output = output + String.fromCharCode(chr1);
     2597
     2598      if (enc3 != 64) {
     2599         output = output + String.fromCharCode(chr2);
     2600      }
     2601      if (enc4 != 64) {
     2602         output = output + String.fromCharCode(chr3);
     2603      }
     2604   } while (i < input.length);
     2605
     2606   return output;
     2607}
     2608
     2609
     2610
    25322611%s
    25332612"""%keyhandler.all_tests()
    25342613    s += keyboards.get_keyboard('')
  • new file sage/server/notebook/widgets/__init__.py

    diff -r ebe94321a279 -r 1fef7442d161 sage/server/notebook/widgets/__init__.py
    - +  
     1
  • new file sage/server/notebook/widgets/all.py

    diff -r ebe94321a279 -r 1fef7442d161 sage/server/notebook/widgets/all.py
    - +  
     1"""nodoctest
     2"""
     3#############################################################################
     4#       Copyright (C) 2008 Jason Grout <jason-sage@creativetrax.com>
     5#  Distributed under the terms of the GNU General Public License (GPL)
     6#  The full text of the GPL is available at:
     7#                  http://www.gnu.org/licenses/
     8#############################################################################
     9
     10
     11from widget import manipulate, Menu, ButtonGroup, TextBox, CheckBox, CheckBoxGroup
  • new file sage/server/notebook/widgets/widget.py

    diff -r ebe94321a279 -r 1fef7442d161 sage/server/notebook/widgets/widget.py
    - +  
     1######################################################################
     2#       Copyright (C) 2008 Jason Grout <jason-sage@creativetrax.com>
     3#  Distributed under the terms of the GNU General Public License (GPL)
     4#  The full text of the GPL is available at:
     5#                  http://www.gnu.org/licenses/
     6######################################################################
     7
     8from   sage.structure.sage_object import SageObject
     9from base64 import standard_b64encode, standard_b64decode
     10
     11
     12
     13class NotebookWidget(SageObject):
     14    """
     15    Base class for notebook widgets.
     16    """
     17    def __init__(self, options=[]):
     18        """
     19        Initialize a notebook widget.  An optional argument of a list
     20        can be passed for
     21        """
     22        self.options=options
     23
     24    def initialize(self, cmd, variable, cell_id):
     25        self.cmd=standard_b64encode(cmd)
     26        self.variable=variable
     27        self.cell_id=cell_id
     28
     29    def default(self):
     30        return self.options[0]
     31
     32
     33    def global_access(self, member=None):
     34        if member is None:
     35            return r"""sage.server.notebook.widgets.widget.__SAGE_MANIPULATE_CACHE__[%d][\"variables\"][\"%s\"]"""%(self.cell_id, self.variable)
     36        else:
     37            return r"""sage.server.notebook.widgets.widget.__SAGE_MANIPULATE_CACHE__[%d][\"variables\"][\"%s\"].%s"""%(self.cell_id, self.variable, member)
     38           
     39
     40class ButtonGroup(NotebookWidget):
     41    def to_html(self):
     42        ret=""
     43        for i in range(len(self.options)):
     44            ret += """<input type='button' value='%r' onclick='manipulate_cell(%d,"%s", "%s", "%s");'></input>"""%(self.options[i], self.cell_id, self.cmd, self.variable, self.global_access("options[%d]"%i))
     45        return ret+"\n"
     46
     47
     48class Menu(NotebookWidget):
     49    def to_html(self):
     50        ret = """<select name='' onchange='manipulate_cell(%d,"%s", "%s", "%s["+this.selectedIndex+"]");'>"""%(self.cell_id, self.cmd, self.variable, self.global_access("options"))
     51        for option_id in range(len(self.options)):
     52            ret += """<option value="%s">%s</option>\n"""%(option_id, self.options[option_id])
     53        ret += "</select>\n"
     54        return ret
     55
     56
     57class TextBox(NotebookWidget):
     58
     59    def __init__(self, default=""):
     60        self._default = default
     61
     62    def default(self):
     63        return self._default
     64   
     65    def to_html(self):
     66        return """<input type='text' value='%r' onchange='manipulate_cell(%d,"%s", "%s", this.value);'></input>"""%(self.default(), self.cell_id, self.cmd, self.variable)
     67       
     68class CheckBox(NotebookWidget):
     69
     70    def __init__(self, checked=True, unchecked=False):
     71        self._checked=checked
     72        self._unchecked=unchecked
     73
     74
     75    def default(self):
     76        return self._checked
     77
     78    def to_html(self):
     79        return """<input type='checkbox' checked='checked' onclick='if(this.checked) {manipulate_cell(%d,"%s", "%s", "%s");} else {manipulate_cell(%d, "%s", "%s", "%s");}'></input>"""%( self.cell_id, self.cmd, self.variable, self.global_access("_checked"), self.cell_id, self.cmd, self.variable, self.global_access("_unchecked"))
     80
     81
     82# CheckBoxGroup is not currently functional
     83class CheckBoxGroup(NotebookWidget):
     84    """
     85    Input is a list of tuples.  The first entry in each tuple
     86    corresponds to a checked box, the second to an unchecked box.
     87    """
     88   
     89    def __init__(self, checklist):
     90        self._checklist = checklist
     91
     92    def default(self):
     93        return [item[0] for item in self._checklist]
     94
     95    def to_html_cached(self):
     96        ret = ""
     97        for item in self._checklist:
     98            ret += """<input type='checkbox' checked='checked' name="%r" onclick='if(this.checked) {manipulate_cell(%d,"%s", "%s", "%r");} else {manipulate_cell(%d, "%s", "%s", "%r");}'></input>"""%( self.variable, self.cell_id, self.cmd, self.variable, item[0], self.cell_id, self.cmd, self.variable, item[1])
     99        return ret+"\n"
     100       
     101
     102
     103def manipulate(cmd, variables, cell_id, real=True):
     104    controls=''
     105    initialization=''
     106    __SAGE_MANIPULATE_CACHE__[cell_id]={}
     107    __SAGE_MANIPULATE_CACHE__[cell_id]['cmd']=cmd
     108    __SAGE_MANIPULATE_CACHE__[cell_id]['variables']={}
     109
     110   
     111    for variable, value in variables.items():
     112        if type(value) is list:
     113            value=Menu(value)
     114        elif type(value) is tuple:
     115            value=Menu(value)
     116
     117        if isinstance(value, NotebookWidget):
     118            value.initialize(cmd, variable, cell_id)
     119            __SAGE_MANIPULATE_CACHE__[cell_id]['variables'][variable]=value
     120            controls += value.to_html()
     121            initialization += "%s=%r\\n"%(variable, value.default())
     122
     123    if real:
     124        print "<html>"
     125    else:
     126        print "<htm>"
     127    print "<script>"
     128    print "var cell_input = get_cell(%d);"%(cell_id)
     129    print "cell_input.style.display = 'none';"
     130    print """cell_input.value = "%s\\n"+decode64("%s");"""%(initialization, standard_b64encode(cmd))
     131    print "evaluate_cell(%d, 0);</script>"%(cell_id)
     132    print controls
     133    print "</html>"
     134
     135
     136__SAGE_MANIPULATE_CACHE__={}
  • sage/server/notebook/worksheet.py

    diff -r ebe94321a279 -r 1fef7442d161 sage/server/notebook/worksheet.py
    a b class Worksheet: 
    12231223
    12241224        # TODOss
    12251225        os.system('chmod -R a+rw "%s"'%absD)
     1226        input += '__SAGE_NOTEBOOK_CELL_ID__=%s\n'%(C.id())
    12261227       
    12271228        if C.time():
    12281229            input += '__SAGE_t__=cputime()\n__SAGE_w__=walltime()\n'
  • setup.py

    diff -r ebe94321a279 -r 1fef7442d161 setup.py
    a b code = setup(name = 'sage', 
    12351235                     'sage.server.server1',
    12361236                     'sage.server.notebook',
    12371237                     'sage.server.notebook.compress',
     1238                     'sage.server.notebook.widgets',
    12381239                     'sage.server.wiki',
    12391240                     'sage.server.trac',
    12401241                     
  • setupnb.py

    diff -r ebe94321a279 -r 1fef7442d161 setupnb.py
    a b setup(name = 'sage', 
    894894                     'sage.server.server1',
    895895                     'sage.server.notebook',
    896896                     'sage.server.notebook.compress',
     897                     'sage.server.notebook.widgets',
    897898                     'sage.server.wiki',
    898899                     'sage.server.trac',
    899900