# HG changeset patch # User William Stein # 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/sage/server/notebook/all.py Thu Feb 28 00:19:10 2008 -0800 +++ b/sage/server/notebook/all.py Sat Mar 01 04:25:34 2008 -0600 @@ -13,3 +13,5 @@ from notebook_object import notebook, in from notebook_object import notebook, inotebook from sagetex import sagetex + +from manipulate import manipulate diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/cell.py --- a/sage/server/notebook/cell.py Thu Feb 28 00:19:10 2008 -0800 +++ b/sage/server/notebook/cell.py Sat Mar 01 04:25:34 2008 -0600 @@ -290,7 +290,21 @@ class Cell(Cell_generic): def computing(self): return self in self.__worksheet.queue() + def is_manipulating(self): + return hasattr(self, 'manipulate') + + def stop_manipulating(self): + if self.is_manipulating(): + del self.manipulate + def set_input_text(self, input): + if input.startswith('%manipulate'): + self.manipulate = input[len('%manipulate')+1:] + self.__version = 1+self.version() + return + elif self.is_manipulating(): + del self.manipulate + self.__version = 1+self.version() self.__in = input if hasattr(self, '_html_cache'): @@ -315,6 +329,10 @@ class Cell(Cell_generic): self.__in = new_text def set_output_text(self, output, html, sage=None): + if self.is_manipulating(): + self._manipulate_output = (output,html) + return + if hasattr(self, '_html_cache'): del self._html_cache output = output.replace('\r','') @@ -370,7 +388,29 @@ class Cell(Cell_generic): x = x.replace(s,begin + s[7:-1] + end) return x - def output_text(self, ncols=0, html=True, raw=False): + def output_text(self, ncols=0, html=True, raw=False, allow_manipulate=True): + if allow_manipulate and hasattr(self, '_manipulate_output'): + z = self.output_text(ncols,html,raw,allow_manipulate=False) + try: + # Fill in the input valuees + if hasattr(self, 'manipulate'): + inp = self.manipulate + i = inp.lstrip().find('\n'); inp = inp[:i] + i = inp.rfind('.'); inp = inp[i+1:] + var, value = inp[:i].split('=') + z = z.replace(''%var, value) + + # Fill in the output template + output,html = self._manipulate_output + z = z.replace('', output.lstrip()) + z = z.replace('', html) + + + return z + except (ValueError, AttributeError), msg: + print msg + pass + s = self.__out if raw: diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/js.py --- a/sage/server/notebook/js.py Thu Feb 28 00:19:10 2008 -0800 +++ b/sage/server/notebook/js.py Sat Mar 01 04:25:34 2008 -0600 @@ -2496,6 +2496,18 @@ function show_help_window(worksheet) { } +/////////////////////////////////////////////////////////////////// +// Dynamic / Manipulate +/////////////////////////////////////////////////////////////////// + +function dynamic(id, input) { + active_cell_list = active_cell_list.concat([id]); + async_request(worksheet_command('eval'), evaluate_cell_callback, + 'newcell=0' + '&id=' + id + '&input='+escape0('%manipulate\n' + input)); +} + + + /********************* js math ***************************/ diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/manipulate.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sage/server/notebook/manipulate.py Sat Mar 01 04:25:34 2008 -0600 @@ -0,0 +1,23 @@ +SAGE_CELL_ID=0 + +def _text_box(f, var): + print """ + +
+ + +
+
+ + """%(var, SAGE_CELL_ID, var, f.__name__) + +def manipulate(f): + """ + Decorate a function f to make a manipulatable version of f. + """ + import inspect + vars = inspect.getargspec(f)[0] + print _text_box(f, vars[0]) + def g(): + return f(eval(vars[0])) + return g diff -r 5db8b4f0c24b -r be4a79848ce3 sage/server/notebook/worksheet.py --- a/sage/server/notebook/worksheet.py Thu Feb 28 00:19:10 2008 -0800 +++ b/sage/server/notebook/worksheet.py Sat Mar 01 04:25:34 2008 -0600 @@ -1203,7 +1203,10 @@ class Worksheet: D = C.directory() if not C.introspect(): - I = C.input_text().strip() + if hasattr(C, 'manipulate'): + I = C.manipulate + else: + I = C.input_text().strip() if I in ['restart', 'quit', 'exit']: self.restart_sage() S = self.system() @@ -1240,6 +1243,10 @@ class Worksheet: # TODOss os.system('chmod -R a+rw "%s"'%absD) + + # This is useful mainly for manipulate -- it allows + # a cell to know it's ID. + input += 'sage.server.notebook.manipulate.SAGE_CELL_ID=%s\n'%(C.id()) if C.time(): input += '__SAGE_t__=cputime()\n__SAGE_w__=walltime()\n'