SageNB Backend for the Sage Rich Output System¶
This module defines the IPython backends for
sage.repl.rich_output
.
EXAMPLES:
Install the SageNB displayhook while doctesting for the rest of this file. This somewhat odd incantation is how SageNB installed its displayhook before:
sage: from sage.misc.displayhook import DisplayHook
sage: import sys
sage: sys.displayhook = DisplayHook()
sage: from sage.repl.rich_output import get_display_manager
sage: get_display_manager()
The Sage display manager using the SageNB backend
We also enable the SageNB magic global variable:
sage: import sage.plot.plot
sage: sage.plot.plot.EMBEDDED_MODE = True
And switch to a temporary directory so our current directory will not get cluttered with temporary files:
sage: os.chdir(tmp_dir())
The SageNB notebook is based on saving data files with predictable filenames:
sage: os.path.exists('sage0.png')
False
sage: Graphics()
sage: os.path.exists('sage0.png')
True
sage: os.remove('sage0.png')
Tables are typeset as html in SageNB:
sage: table([1, 2, 3])
<html><div class="notruncate">
<table class="table_form">
<tbody>
<tr class ="row-a">
<td><script type="math/tex">1</script></td>
<td><script type="math/tex">2</script></td>
<td><script type="math/tex">3</script></td>
</tr>
</tbody>
</table>
</div></html>
-
class
sage.repl.rich_output.backend_sagenb.
BackendSageNB
¶ Bases:
sage.repl.rich_output.backend_base.BackendBase
-
display_immediately
(plain_text, rich_output)¶ Show output without waiting for the prompt.
INPUT:
plain_text
– instance ofOutputPlainText
. The plain text version of the output.rich_output
– instance of an output container class (subclass ofOutputBase
). Guaranteed to be one of the output containers returned fromsupported_output()
, possibly the same asplain_text
.
OUTPUT:
This method does not return anything.
EXAMPLES:
sage: import sage.repl.rich_output.output_catalog as catalog sage: plain_text = catalog.OutputPlainText.example() sage: from sage.repl.rich_output.backend_sagenb import BackendSageNB sage: backend = BackendSageNB() sage: backend.display_immediately(plain_text, plain_text) Example plain text output sage: latex = catalog.OutputLatex.example() sage: backend.display_immediately(plain_text, latex) <html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\int \sin\left(x\right)\,{d x}</script></html>
-
embed_image
(output_buffer, file_ext)¶ Embed Image in the SageNB worksheet
SageNB scans per-cell directories for image files, so all we have to do here is to save the image at the right place.
INPUT:
output_buffer
–Buffer
. A buffer holding the image data.file_ext
– string. The file extension to use for saving the image.
OUTPUT:
Nothing is returned. The image file is saved in the appropriate place for SageNB.
EXAMPLES:
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: rich_output = dm.types.OutputImagePng.example() sage: os.path.exists('sage0.png') False sage: dm._backend.embed_image(rich_output.png, '.png') sage: os.path.exists('sage0.png') True
-
embed_video
(video_output)¶
-
supported_output
()¶ Return the outputs that are supported by the SageNB backend.
OUTPUT:
Iterable of output container classes, that is, subclass of
OutputBase
). The order is ignored.EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import BackendSageNB sage: backend = BackendSageNB() sage: supp = backend.supported_output(); supp # random output set([<class 'sage.repl.rich_output.output_graphics.OutputPlainText'>, ..., <class 'sage.repl.rich_output.output_graphics.OutputCanvas3d'>]) sage: from sage.repl.rich_output.output_basic import OutputLatex sage: OutputLatex in supp True
-
threejs_offline_scripts
()¶ Three.js scripts for the Sage notebook
OUTPUT:
String containing script tags
EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import BackendSageNB sage: backend = BackendSageNB() sage: backend.threejs_offline_scripts() '...<script ...</script>...'
-
-
class
sage.repl.rich_output.backend_sagenb.
SageNbOutputSceneJmol
(scene_zip, preview_png)¶ Bases:
sage.repl.rich_output.output_graphics3d.OutputSceneJmol
Adapt Jmol rich output container for SageNB.
For legacy reasons, SageNB expects Jmol files saved under strange names. This class takes care of that.
EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: SageNbOutputSceneJmol.example() SageNbOutputSceneJmol container
-
embed
()¶ Save all files necessary to embed jmol
EXAMPLES:
Switch to a new empty temporary directory:
sage: os.chdir(tmp_dir()) sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: j = SageNbOutputSceneJmol.example() sage: j.embed() sage: sorted(os.listdir('.')) ['.jmol_images', 'sage0-size32-....jmol.zip', 'sage0-size32.jmol'] sage: sorted(os.listdir('.jmol_images')) ['sage0-size32.jmol.png']
-
preview_filename
()¶ Return the filename for the png preview
OUTPUT:
String.
EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: j = SageNbOutputSceneJmol.example() sage: j.preview_filename() './.jmol_images/sage0-size32.jmol.png'
-
sagenb_launch_script_filename
()¶ Return the launch script filename used by SageNB
OUTPUT:
String.
EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: j = SageNbOutputSceneJmol.example() sage: j.sagenb_launch_script_filename() 'sage0-size32.jmol'
-
save_launch_script
()¶ Save the Jmol launch script
See
sagenb_launch_script_filename()
.EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: j = SageNbOutputSceneJmol.example() sage: os.path.exists('sage0-size32.jmol') False sage: j.save_launch_script() sage: os.path.exists('sage0-size32.jmol') True
-
save_preview
()¶ Save the preview PNG image
See
preview_filename()
.EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: j = SageNbOutputSceneJmol.example() sage: import shutil sage: shutil.rmtree('.jmol_images', ignore_errors=True) sage: j.save_preview() sage: os.listdir('.jmol_images') ['sage1-size32.jmol.png']
-
scene_zip_filename
()¶ Return the filename for the scene zip archive
OUTPUT:
String.
EXAMPLES:
sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol sage: j = SageNbOutputSceneJmol.example() sage: j.scene_zip_filename() 'sage0-size32-....jmol.zip'
-
-
sage.repl.rich_output.backend_sagenb.
world_readable
(filename)¶ All SageNB temporary files must be world-readable.
Discussion of this design choice can be found at trac ticket #17743.
EXAMPLES:
sage: import os, stat sage: f = tmp_filename()
At least on a sane system the temporary files are only readable by the user, but not by others in the group or total strangers:
sage: mode = os.stat(f).st_mode sage: bool(mode & stat.S_IRUSR), bool(mode & stat.S_IRGRP), bool(mode & stat.S_IROTH) # random output (True, False, False)
This function disables that protection:
sage: from sage.repl.rich_output.backend_sagenb import world_readable sage: world_readable(f) sage: mode = os.stat(f).st_mode sage: bool(mode & stat.S_IRUSR), bool(mode & stat.S_IRGRP), bool(mode & stat.S_IROTH) (True, True, True)