def example(cls): r""" Construct a sample Canvas3D output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputSceneCanvas3d`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneWavefront sage: rich_output = OutputSceneWavefront.example(); rich_output OutputSceneWavefront container sage: rich_output.obj buffer containing 227 bytes sage: rich_output.obj.get_str() 'mtllib scene.mtl\ng obj_1\n...\nf 1 5 6 2\nf 1 4 7 5\nf 6 5 7 8\nf 7 4 3 8\nf 3 2 6 8\n' sage: rich_output.mtl buffer containing 80 bytes sage: rich_output.mtl.get_str() 'newmtl texture177\nKa 0.2 0.2 0.5\nKd 0.4 0.4 1.0\nKs 0.0 0.0 0.0\nillum 1\nNs 1\nd 1\n' """ from sage.env import SAGE_EXTCODE with_path = lambda x: os.path.join( SAGE_EXTCODE, 'doctest', 'rich_output', 'example_wavefront', x) return cls( OutputBuffer.from_file(with_path('scene.obj')), OutputBuffer.from_file(with_path('scene.mtl')), )
def example(cls): r""" Construct a sample Canvas3D output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputSceneCanvas3d`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneWavefront sage: rich_output = OutputSceneWavefront.example(); rich_output OutputSceneWavefront container sage: rich_output.obj buffer containing 227 bytes sage: rich_output.obj.get() 'mtllib scene.mtl\ng obj_1\n...\nf 1 5 6 2\nf 1 4 7 5\nf 6 5 7 8\nf 7 4 3 8\nf 3 2 6 8\n' sage: rich_output.mtl buffer containing 80 bytes sage: rich_output.mtl.get() 'newmtl texture177\nKa 0.2 0.2 0.5\nKd 0.4 0.4 1.0\nKs 0.0 0.0 0.0\nillum 1\nNs 1\nd 1\n' """ from sage.env import SAGE_EXTCODE with_path = lambda x: os.path.join( SAGE_EXTCODE, 'doctest', 'rich_output', 'example_wavefront', x) return cls( OutputBuffer.from_file(with_path('scene.obj')), OutputBuffer.from_file(with_path('scene.mtl')), )
def example(cls): r""" Construct a sample Canvas3D output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputSceneCanvas3d`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneCanvas3d sage: rich_output = OutputSceneCanvas3d.example(); rich_output OutputSceneCanvas3d container sage: rich_output.canvas3d buffer containing 829 bytes sage: rich_output.canvas3d.get_str() '[{"vertices":[{"x":1,"y":1,"z":1},...{"x":1,"y":-1,"z":-1}],"faces":[[0,1,2,3]],"color":"008000"}]' """ from sage.env import SAGE_EXTCODE filename = os.path.join( SAGE_EXTCODE, 'doctest', 'rich_output', 'example.canvas3d') return cls(OutputBuffer.from_file(filename))
def example(cls): r""" Construct a sample video output container This static method is meant for doctests, so they can easily construct an example. The method is implemented in the abstract :class:`OutputVideoBase` class, but should get invoked on a concrete subclass for which an actual example can exist. OUTPUT: An instance of the class on which this method is called. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputVideoOgg sage: OutputVideoOgg.example() OutputVideoOgg container sage: OutputVideoOgg.example().video buffer containing 5612 bytes sage: OutputVideoOgg.example().ext '.ogv' sage: OutputVideoOgg.example().mimetype 'video/ogg' """ from sage.env import SAGE_EXTCODE filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example' + cls.ext) return cls(OutputBuffer.from_file(filename), {'controls': True, 'loop': False})
def example(cls): r""" Construct a sample Canvas3D output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputSceneCanvas3d`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneCanvas3d sage: rich_output = OutputSceneCanvas3d.example(); rich_output OutputSceneCanvas3d container sage: rich_output.canvas3d buffer containing 829 bytes sage: rich_output.canvas3d.get() '[{"vertices":[{"x":1,"y":1,"z":1},...{"x":1,"y":-1,"z":-1}],"faces":[[0,1,2,3]],"color":"008000"}]' """ from sage.env import SAGE_EXTCODE filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.canvas3d') return cls(OutputBuffer.from_file(filename))
def example(cls): r""" Construct a sample video output container This static method is meant for doctests, so they can easily construct an example. The method is implemented in the abstract :class:`OutputVideoBase` class, but should get invoked on a concrete subclass for which an actual example can exist. OUTPUT: An instance of the class on which this method is called. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputVideoOgg sage: OutputVideoOgg.example() OutputVideoOgg container sage: OutputVideoOgg.example().video buffer containing 5612 bytes sage: OutputVideoOgg.example().ext '.ogv' sage: OutputVideoOgg.example().mimetype 'video/ogg' """ from sage.env import SAGE_EXTCODE filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example' + cls.ext) return cls(OutputBuffer.from_file(filename), { 'controls': True, 'loop': False })
def graphics_from_save(self, save_function, save_kwds, file_extension, output_container, figsize=None, dpi=None): r""" Helper to construct graphics. This method can be used to simplify the implementation of a ``_rich_repr_`` method of a graphics object if there is already a function to save graphics to a file. INPUT: - ``save_function`` -- callable that can save graphics to a file and accepts options like :meth:`sage.plot.graphics.Graphics.save`. - ``save_kwds`` -- dictionary. Keyword arguments that are passed to the save function. - ``file_extension`` -- string starting with ``'.'``. The file extension of the graphics file. - ``output_container`` -- subclass of :class:`sage.repl.rich_output.output_basic.OutputBase`. The output container to use. Must be one of the types in :meth:`supported_output`. - ``figsize`` -- pair of integers (optional). The desired graphics size in pixels. Suggested, but need not be respected by the output. - ``dpi`` -- integer (optional). The desired resolution in dots per inch. Suggested, but need not be respected by the output. OUTPUT: Return an instance of ``output_container``. EXAMPLES:: sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: plt = plot(sin) sage: out = dm.graphics_from_save(plt.save, dict(), '.png', dm.types.OutputImagePng) sage: out OutputImagePng container sage: out.png.get().startswith('\x89PNG') True sage: out.png.filename() # random '/home/user/.sage/temp/localhost.localdomain/23903/tmp_pu5woK.png' """ import os if not file_extension.startswith(os.path.extsep): raise ValueError('file_extension must start with a period') if output_container not in self.supported_output(): raise OutputTypeException('output_container is not supported by backend') from sage.misc.temporary_file import tmp_filename filename = tmp_filename(ext=file_extension) # Call the save_function with the right arguments kwds = dict(save_kwds) if figsize is not None: kwds['figsize'] = figsize if dpi is not None: kwds['dpi'] = dpi save_function(filename, **kwds) from sage.repl.rich_output.buffer import OutputBuffer buf = OutputBuffer.from_file(filename) return output_container(buf)
def graphics_from_save(self, save_function, save_kwds, file_extension, output_container, figsize=None, dpi=None): r""" Helper to construct graphics. This method can be used to simplify the implementation of a ``_rich_repr_`` method of a graphics object if there is already a function to save graphics to a file. INPUT: - ``save_function`` -- callable that can save graphics to a file and accepts options like :meth:`sage.plot.graphics.Graphics.save`. - ``save_kwds`` -- dictionary. Keyword arguments that are passed to the save function. - ``file_extension`` -- string starting with ``'.'``. The file extension of the graphics file. - ``output_container`` -- subclass of :class:`sage.repl.rich_output.output_basic.OutputBase`. The output container to use. Must be one of the types in :meth:`supported_output`. - ``figsize`` -- pair of integers (optional). The desired graphics size in pixels. Suggested, but need not be respected by the output. - ``dpi`` -- integer (optional). The desired resolution in dots per inch. Suggested, but need not be respected by the output. OUTPUT: Return an instance of ``output_container``. EXAMPLES:: sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: plt = plot(sin) sage: out = dm.graphics_from_save(plt.save, dict(), '.png', dm.types.OutputImagePng) sage: out OutputImagePng container sage: out.png.get().startswith('\x89PNG') True sage: out.png.filename() # random '/home/user/.sage/temp/localhost.localdomain/23903/tmp_pu5woK.png' """ import os if not file_extension.startswith(os.path.extsep): raise ValueError('file_extension must start with a period') if output_container not in self.supported_output(): raise OutputTypeException( 'output_container is not supported by backend') from sage.misc.temporary_file import tmp_filename filename = tmp_filename(ext=file_extension) # Call the save_function with the right arguments kwds = dict(save_kwds) if figsize is not None: kwds['figsize'] = figsize if dpi is not None: kwds['dpi'] = dpi save_function(filename, **kwds) from sage.repl.rich_output.buffer import OutputBuffer buf = OutputBuffer.from_file(filename) return output_container(buf)