# HG changeset patch # User Jason Grout # Date 1311809242 25200 # Node ID f67d7039d959b56d5b611b5979375b95dbe69398 # Parent 6e41d4960bc5f472eda2ad125f7234ce1e0d7396 [mq]: show.patch diff --git a/sage/misc/html.py b/sage/misc/html.py --- a/sage/misc/html.py +++ b/sage/misc/html.py @@ -173,13 +173,17 @@ s = s[j+7:] from sage.misc.misc import EMBEDDED_MODE - import sys if EMBEDDED_MODE: if EMBEDDED_MODE['frontend']=='notebook': print "%s"%t + return '' elif EMBEDDED_MODE['frontend']=='sagecell': + import sys sys._sage_messages.message_queue.display({'text/html':"%s"%t}) - return '' + return '' + else: + return '' + def table(self, x, header = False): r""" @@ -277,13 +281,20 @@ """ - print self.table_str(x, header=header) + from sage.misc.misc import EMBEDDED_MODE + output_string = self.table_str(x, header=header) + if EMBEDDED_MODE: + if EMBEDDED_MODE['frontend']=='notebook': + print ""+output_string+"" + elif EMBEDDED_MODE['frontend']=='sagecell': + import sys + sys._sage_messages.message_queue.display({'text/html':output_string}) def table_str(self, x, header): import types from sage.misc.all import latex from itertools import cycle - ret = "" + ret=[] # list of strings if isinstance(x, types.GeneratorType): x = list(x) if isinstance(x, (list, tuple)): @@ -291,25 +302,25 @@ if rows > 0: # if the table has less then 100 rows, don't truncate the output in the notebook if rows <= 100: - ret += "\n
\n\n" + ret.append("
\n
\n") else: - ret += "\n
\n
\n" + ret.append("
\n
\n") if header is True: header=x[0] x = list(x[1:]) if header is not False: - ret += "" - ret += self._table_columns(header, True) - ret += "" + ret.append("") + ret.append(self._table_columns(header, True)) + ret.append("") for row_class, row in zip(cycle(["row-a", "row-b"]), x): - ret += "" % row_class - ret += self._table_columns(row, False) - ret += "" - ret += "\n
\n
\n" - return ret + ret.append("" % row_class) + ret.append(self._table_columns(row, False)) + ret.append("") + ret.append("\n\n") + return "".join(ret) def _table_columns(self, row, header=False): r""" diff --git a/sage/misc/latex.py b/sage/misc/latex.py --- a/sage/misc/latex.py +++ b/sage/misc/latex.py @@ -2072,7 +2072,8 @@ if pdflatex or (viewer == "pdf" and engine == "latex"): engine = "pdflatex" # notebook - if EMBEDDED_MODE and viewer is None: + import sage.misc.misc as misc + if misc.EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: @@ -2080,14 +2081,28 @@ if not jsMath_okay: break if jsMath_okay: # put comma at end of line in print below? - print JSMath().eval(objects, mode=mode, combine_all=combine_all) + jsmath_expr = str(JSMath().eval(objects, mode=mode, combine_all=combine_all)) + if misc.EMBEDDED_MODE['frontend']=='sagecell': + mathexpr=jsmath_expr.replace('','').replace('','') + import sys + sys._sage_messages.message_queue.display({'text/html':mathexpr}) + else: + print jsmath_expr else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, engine=engine) - print ''%png_link # put comma at end of line? + if misc.EMBEDDED_MODE['frontend']=='sagecell': + import sys + import json #TODO: be smart about which json + sys._sage_upload_file_pipe.send_bytes(json.dumps([png_file])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + msg={'text/filename': png_file} + sys._sage_messages.message_queue.display(msg) + else: + print ''%png_link # put comma at end of line? return # command line or notebook with viewer tmp = tmp_dir('sage_viewer') diff --git a/sage/plot/animate.py b/sage/plot/animate.py --- a/sage/plot/animate.py +++ b/sage/plot/animate.py @@ -388,6 +388,15 @@ from subprocess import check_call, CalledProcessError try: check_call(cmd, shell=True) + import sage.misc.misc as misc + if misc.EMBEDDED_MODE and misc.EMBEDDED_MODE['frontend']=='sagecell': + import json #TODO: be smart about which json + import sys + sys._sage_upload_file_pipe.send_bytes(json.dumps([savefile])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + msg={'text/filename': os.path.basename(savefile)} + sys._sage_messages.message_queue.display(msg) + if show_path: print "Animation saved to file %s." % savefile except (CalledProcessError, OSError): diff --git a/sage/plot/graphics.py b/sage/plot/graphics.py --- a/sage/plot/graphics.py +++ b/sage/plot/graphics.py @@ -1705,17 +1705,27 @@ # This option should not be passed on to save(). linkmode = kwds.pop('linkmode', False) - if sage.plot.plot.DOCTEST_MODE: kwds.pop('filename', None) self.save(DOCTEST_MODE_FILE, **kwds) - elif sage.plot.plot.EMBEDDED_MODE: + elif sage.misc.misc.EMBEDDED_MODE: kwds.setdefault('filename', sage.misc.misc.graphics_filename()) + filename=kwds['filename'] self.save(**kwds) - if linkmode == True: - return "" % kwds['filename'] + import sys + if sage.misc.misc.EMBEDDED_MODE['frontend']=='sagecell': + import json #TODO: be smart about which json + sys._sage_upload_file_pipe.send_bytes(json.dumps([filename])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + + if linkmode: + return ""%filename else: - html("" % kwds['filename']) + if sage.misc.misc.EMBEDDED_MODE['frontend']=='sagecell': + msg={'text/filename': filename} + sys._sage_messages.message_queue.display(msg) + else: + html(""%filename) else: kwds.setdefault('filename', sage.misc.misc.tmp_filename() + '.png') self.save(**kwds) @@ -2534,42 +2544,47 @@ options.update(kwds) dpi = options.pop('dpi') transparent = options.pop('transparent') + format = options.pop('format', None) fig_tight = options.pop('fig_tight') if filename is None: filename = options.pop('filename') if filename is None: filename = sage.misc.misc.graphics_filename() - ext = os.path.splitext(filename)[1].lower() - - if ext not in ALLOWED_EXTENSIONS: - raise ValueError("allowed file extensions for images are '" - + "', '".join(ALLOWED_EXTENSIONS) + "'!") - elif ext in ['', '.sobj']: - SageObject.save(self, filename) + + if isinstance(filename, basestring): + ext = os.path.splitext(filename)[1].lower() + if format is None: + # guess the format from the extension, so make sure that the extension + # is allowed + if ext not in ALLOWED_EXTENSIONS: + raise ValueError("allowed file extensions for images are '" + + "', '".join(ALLOWED_EXTENSIONS) + "'!") + elif ext in ['', '.sobj']: + SageObject.save(self, filename) + + figure = self.matplotlib(**options) + # You can output in PNG, PS, EPS, PDF, or SVG format, depending on the file extension. + # matplotlib looks at the file extension to see what the renderer should be. + # The default is FigureCanvasAgg for PNG's because this is by far the most + # common type of files rendered, like in the notebook, for example. + # if the file extension is not '.png', then matplotlib will handle it. + from matplotlib.backends.backend_agg import FigureCanvasAgg + figure.set_canvas(FigureCanvasAgg(figure)) + # this messes up the aspect ratio! + #figure.canvas.mpl_connect('draw_event', pad_for_tick_labels) + + # tight_layout adjusts the *subplot* parameters so ticks aren't cut off, etc. + figure.tight_layout() + + if fig_tight is True: + figure.savefig(filename, dpi=dpi, bbox_inches='tight', + bbox_extra_artists=self._bbox_extra_artists, + transparent=transparent, format=format) + else: - figure = self.matplotlib(**options) - # You can output in PNG, PS, EPS, PDF, or SVG format, depending - # on the file extension. - # matplotlib looks at the file extension to see what the renderer should be. - # The default is FigureCanvasAgg for PNG's because this is by far the most - # common type of files rendered, like in the notebook, for example. - # if the file extension is not '.png', then matplotlib will handle it. - from matplotlib.backends.backend_agg import FigureCanvasAgg - figure.set_canvas(FigureCanvasAgg(figure)) - # this messes up the aspect ratio! - #figure.canvas.mpl_connect('draw_event', pad_for_tick_labels) - - # tight_layout adjusts the *subplot* parameters so ticks aren't cut off, etc. - figure.tight_layout() - - if fig_tight is True: - figure.savefig(filename, dpi=dpi, bbox_inches='tight', - bbox_extra_artists=self._bbox_extra_artists, - transparent=transparent) - else: - figure.savefig(filename, dpi=dpi, - transparent=transparent) + figure.savefig(filename, dpi=dpi, + transparent=transparent, format=format) class GraphicsArray(SageObject): @@ -2929,11 +2944,18 @@ self.save(DOCTEST_MODE_FILE, dpi=dpi, figsize=self._figsize, axes = axes, **args) return - if sage.plot.plot.EMBEDDED_MODE: + if filename is None: + filename = sage.misc.misc.graphics_filename() + if sage.misc.misc.EMBEDDED_MODE: self.save(filename, dpi=dpi, figsize=self._figsize, axes = axes, **args) + if sage.misc.misc.EMBEDDED_MODE['frontend']=='sagecell': + import json #TODO: be smart about which json + import sys + sys._sage_upload_file_pipe.send_bytes(json.dumps([filename])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + msg={'text/filename': filename} + sys._sage_messages.message_queue.display(msg) return - if filename is None: - filename = sage.misc.misc.tmp_filename() + '.png' self._render(filename, dpi=dpi, figsize=self._figsize, axes = axes, **args) os.system('%s %s 2>/dev/null 1>/dev/null &'%( sage.misc.viewer.browser(), filename)) diff --git a/sage/plot/plot.py b/sage/plot/plot.py --- a/sage/plot/plot.py +++ b/sage/plot/plot.py @@ -343,7 +343,6 @@ ## going to be used. #DEFAULT_FIGSIZE=(6, 3.70820393249937) -from sage.misc.misc import EMBEDDED_MODE DOCTEST_MODE = False import sage.misc.misc from sage.misc.misc import srange diff --git a/sage/plot/plot3d/base.pyx b/sage/plot/plot3d/base.pyx --- a/sage/plot/plot3d/base.pyx +++ b/sage/plot/plot3d/base.pyx @@ -1077,6 +1077,7 @@ from sage.plot.plot import DOCTEST_MODE from sage.misc.misc import EMBEDDED_MODE + import sys ext = None # Tachyon resolution options @@ -1090,9 +1091,18 @@ opts = '-res %s %s'%(figsize[0]*100, figsize[1]*100) if DOCTEST_MODE or viewer=='tachyon' or (viewer=='java3d' and EMBEDDED_MODE): + ext = "png" + filename_full=filename+'.'+ext T = self._prepare_for_tachyon(frame, axes, frame_aspect_ratio, aspect_ratio, zoom) - tachyon_rt(T.tachyon(), filename+".png", verbosity, True, opts) - ext = "png" + tachyon_rt(T.tachyon(), filename_full, verbosity, True, opts) + if EMBEDDED_MODE and EMBEDDED_MODE['frontend']=='sagecell': + msg={} + import json #TODO: be smart about which json + sys._sage_upload_file_pipe.send_bytes(json.dumps([filename_full])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + msg['text/filename']=filename_full + sys._sage_messages.message_queue.display(msg) + import sage.misc.viewer viewer_app = sage.misc.viewer.browser() @@ -1128,13 +1138,23 @@ T.export_jmol(archive_name, force_reload=EMBEDDED_MODE, zoom=zoom*100, **kwds) viewer_app = os.path.join(sage.misc.misc.SAGE_LOCAL, "bin/jmol") - # We need a script to load the file + if EMBEDDED_MODE and EMBEDDED_MODE['frontend']=='sagecell': + msg={} + import json #TODO: be smart about which json + sys._sage_upload_file_pipe.send_bytes(json.dumps([archive_name])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + msg['application/x-jmol']=archive_name + sys._sage_messages.message_queue.display(msg) + + else: + # We need a script to load the file + if EMBEDDED_MODE and EMBEDDED_MODE['frontend']=='notebook': + import sagenb + path = "cells/%s/%s" %(sagenb.notebook.interact.SAGE_CELL_ID, archive_name) + else: + path = archive_name + f = open(filename + '.'+ext, 'w') - if EMBEDDED_MODE: - import sagenb - path = "cells/%s/%s" %(sagenb.notebook.interact.SAGE_CELL_ID, archive_name) - else: - path = archive_name f.write('set defaultdirectory "%s"\n' %path) f.write('script SCRIPT\n') f.close() diff --git a/sage/plot/plot3d/tachyon.py b/sage/plot/plot3d/tachyon.py --- a/sage/plot/plot3d/tachyon.py +++ b/sage/plot/plot3d/tachyon.py @@ -318,6 +318,13 @@ if sage.misc.misc.EMBEDDED_MODE: filename = sage.misc.misc.graphics_filename() self.save(filename, verbose=verbose, extra_opts=extra_opts) + if sage.misc.misc.EMBEDDED_MODE['frontend']=='sagecell': + import json #TODO: be smart about which json + import sys + sys._sage_upload_file_pipe.send_bytes(json.dumps([filename])) + sys._sage_upload_file_pipe.recv_bytes() # confirmation upload happened + msg={'text/filename': filename} + sys._sage_messages.message_queue.display(msg) return filename = sage.misc.misc.tmp_filename() + '.png' self.save(filename, verbose=verbose, extra_opts=extra_opts)