def display_immediately(self, plain_text, rich_output): """ Show output immediately. This method is similar to the rich output :meth:`displayhook`, except that it can be invoked at any time. INPUT: Same as :meth:`displayhook`. OUTPUT: This method does not return anything. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_cell import BackendCell sage: backend = BackendCell() sage: _ = backend.display_immediately(plain_text, plain_text) Example plain text output """ if isinstance(rich_output, OutputPlainText): return {u'text/plain': rich_output.text.get()}, {} if isinstance(rich_output, OutputAsciiArt): return {u'text/plain': rich_output.ascii_art.get()}, {} if isinstance(rich_output, OutputLatex): self.display_html(rich_output.mathjax()) elif isinstance(rich_output, OutputHtml): self.display_html(rich_output.html.get()) elif isinstance(rich_output, OutputImageGif): self.display_file(rich_output.gif.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageJpg): self.display_file(rich_output.jpg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePdf): self.display_file(rich_output.pdf.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePng): self.display_file(rich_output.png.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageSvg): self.display_file(rich_output.svg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputSceneCanvas3d): self.display_file(rich_output.canvas3d.filename(), 'application/x-canvas3d') elif isinstance(rich_output, OutputSceneJmol): path = tempfile.mkdtemp(suffix=".jmol", dir=".") os.chmod(path, stat.S_IRWXU + stat.S_IXGRP + stat.S_IXOTH) rich_output.scene_zip.save_as(os.path.join(path, 'scene.zip')) rich_output.preview_png.save_as(os.path.join(path, 'preview.png')) display_message({'text/plain': 'application/x-jmol file', 'application/x-jmol': path}) else: raise TypeError('rich_output type not supported, got {0}'.format(rich_output)) return {u'text/plain': None}, {}
def display_file(self, path, mimetype=None): path = os.path.relpath(path) if path.startswith("../"): shutil.copy(path, ".") path = os.path.basename(path) os.chmod(path, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH) if mimetype is None: mimetype = 'application/x-file' msg = {'text/plain': '%s file' % mimetype, mimetype: path} display_message(msg) sys._sage_.sent_files[path] = os.path.getmtime(path)
def display_html(self, s): display_message({'text/plain': 'html', 'text/html': s})
def display_immediately(self, plain_text, rich_output): """ Show output immediately. This method is similar to the rich output :meth:`displayhook`, except that it can be invoked at any time. INPUT: Same as :meth:`displayhook`. OUTPUT: This method does not return anything. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_cell import BackendCell sage: backend = BackendCell() sage: _ = backend.display_immediately(plain_text, plain_text) Example plain text output """ if isinstance(rich_output, OutputPlainText): return {u'text/plain': rich_output.text.get()}, {} if isinstance(rich_output, OutputAsciiArt): return {u'text/plain': rich_output.ascii_art.get()}, {} if isinstance(rich_output, OutputLatex): self.display_html(rich_output.mathjax()) elif isinstance(rich_output, OutputImageGif): self.display_file(rich_output.gif.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageJpg): self.display_file(rich_output.jpg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePdf): self.display_file(rich_output.pdf.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePng): self.display_file(rich_output.png.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageSvg): self.display_file(rich_output.svg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputSceneCanvas3d): self.display_file(rich_output.canvas3d.filename(), 'application/x-canvas3d') elif isinstance(rich_output, OutputSceneJmol): path = tempfile.mkdtemp(suffix=".jmol", dir=".") os.chmod(path, stat.S_IRWXU + stat.S_IXGRP + stat.S_IXOTH) rich_output.scene_zip.save_as(os.path.join(path, 'scene.zip')) rich_output.preview_png.save_as(os.path.join(path, 'preview.png')) display_message({ 'text/plain': 'application/x-jmol file', 'application/x-jmol': path }) else: raise TypeError( 'rich_output type not supported, got {0}'.format(rich_output)) return {u'text/plain': None}, {}
def display_immediately(self, plain_text, rich_output): """ Show output immediately. This method is similar to the rich output :meth:`displayhook`, except that it can be invoked at any time. INPUT: Same as :meth:`displayhook`. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_cell import BackendCell sage: backend = BackendCell() sage: _ = backend.display_immediately(plain_text, plain_text) Example plain text output """ if isinstance(rich_output, OutputPlainText): return {u'text/plain': rich_output.text.get_unicode()}, {} if isinstance(rich_output, OutputAsciiArt): return {u'text/plain': rich_output.ascii_art.get_unicode()}, {} if isinstance(rich_output, OutputLatex): display_html(rich_output.mathjax()) elif isinstance(rich_output, OutputHtml): display_html(rich_output.html.get_unicode()) elif isinstance(rich_output, OutputImageGif): display_file(rich_output.gif.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageJpg): display_file(rich_output.jpg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePdf): display_file(rich_output.pdf.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePng): display_file(rich_output.png.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageSvg): display_file(rich_output.svg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputSceneJmol): path = tempfile.mkdtemp(suffix=".jmol", dir=".") os.chmod(path, stat.S_IRWXU + stat.S_IXGRP + stat.S_IXOTH) rich_output.scene_zip.save_as(os.path.join(path, 'scene.zip')) rich_output.preview_png.save_as(os.path.join(path, 'preview.png')) display_message({ 'text/plain': 'application/x-jmol file', 'application/x-jmol': path }) elif isinstance(rich_output, OutputSceneThreejs): path = tempfile.mkstemp(suffix='.html', dir='.')[1] path = os.path.relpath(path) rich_output.html.save_as(path) os.chmod(path, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH) display_html(""" <iframe scrolling="no" src="cell://{}" style=" border: 1px silver solid; height: 500px; min-width: 500px; width: 75%; " > </iframe> """.format(path)) sys._sage_.sent_files[path] = os.path.getmtime(path) else: raise TypeError( 'rich_output type not supported, got {0}'.format(rich_output)) return {u'text/plain': None}, {}
def display_immediately(self, plain_text, rich_output): """ Show output immediately. This method is similar to the rich output :meth:`displayhook`, except that it can be invoked at any time. INPUT: Same as :meth:`displayhook`. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_cell import BackendCell sage: backend = BackendCell() sage: _ = backend.display_immediately(plain_text, plain_text) Example plain text output """ if isinstance(rich_output, OutputPlainText): return {u'text/plain': rich_output.text.get()}, {} if isinstance(rich_output, OutputAsciiArt): return {u'text/plain': rich_output.ascii_art.get()}, {} if isinstance(rich_output, OutputLatex): display_html(rich_output.mathjax()) elif isinstance(rich_output, OutputHtml): display_html(rich_output.html.get()) elif isinstance(rich_output, OutputImageGif): display_file(rich_output.gif.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageJpg): display_file(rich_output.jpg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePdf): display_file(rich_output.pdf.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImagePng): display_file(rich_output.png.filename(), 'text/image-filename') elif isinstance(rich_output, OutputImageSvg): display_file(rich_output.svg.filename(), 'text/image-filename') elif isinstance(rich_output, OutputSceneCanvas3d): display_file( rich_output.canvas3d.filename(), 'application/x-canvas3d') elif isinstance(rich_output, OutputSceneJmol): path = tempfile.mkdtemp(suffix=".jmol", dir=".") os.chmod(path, stat.S_IRWXU + stat.S_IXGRP + stat.S_IXOTH) rich_output.scene_zip.save_as(os.path.join(path, 'scene.zip')) rich_output.preview_png.save_as(os.path.join(path, 'preview.png')) display_message({'text/plain': 'application/x-jmol file', 'application/x-jmol': path}) elif isinstance(rich_output, OutputSceneThreejs): path = tempfile.mkstemp(suffix='.html', dir='.')[1] path = os.path.relpath(path) rich_output.html.save_as(path) os.chmod(path, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH) display_html(""" <iframe scrolling="no" src="cell://{}" style=" border: 1px silver solid; height: 500px; min-width: 500px; width: 75%; " > </iframe> """.format(path)) sys._sage_.sent_files[path] = os.path.getmtime(path) else: raise TypeError('rich_output type not supported, got {0}'.format(rich_output)) return {u'text/plain': None}, {}