Wrapper for Graphics Files

class sage.structure.graphics_file.GraphicsFile(filename, mime_type=None)

Bases: sage.structure.sage_object.SageObject

Wrapper around a graphics file.

data()

Return a byte string containing the image file.

filename()
launch_viewer()

Launch external viewer for the graphics file.

Note

Does not actually launch a new process when doctesting.

EXAMPLES:

sage: from sage.structure.graphics_file import GraphicsFile
sage: g = GraphicsFile('/tmp/test.png', 'image/png')
sage: g.launch_viewer()
mime()
sagenb_embedding()

Embed in SageNB

This amounts to just placing the file in the cell directory. The notebook will then try to guess what we want with it.

save_as(filename)

Make the file available under a new filename.

INPUT:

  • filename – string. The new filename.

The newly-created filename will be a hardlink if possible. If not, an independent copy is created.

class sage.structure.graphics_file.Mime

Bases: object

classmethod extension(mime_type)

Return file extension.

INPUT:

  • mime_type – mime type as string.

OUTPUT:

String containing the usual file extension for that type of file. Excludes os.extsep.

EXAMPLES:

sage: from sage.structure.graphics_file import Mime
sage: Mime.extension('image/png')
'png'
classmethod validate(value)

Check that input is known mime type

INPUT:

  • value – string.

OUTPUT:

Unicode string of that mime type. A ValueError is raised if input is incorrect / unknown.

EXAMPLES:

sage: from sage.structure.graphics_file import Mime
sage: Mime.validate('image/png')
u'image/png'
sage: Mime.validate('foo/bar')
Traceback (most recent call last):
...
ValueError: unknown mime type
sage.structure.graphics_file.graphics_from_save(save_function, preferred_mime_types, allowed_mime_types=None, figsize=None, dpi=None)

Helper function to construct a graphics file.

INPUT:

  • save_function – callable that can save graphics to a file and accepts options like sage.plot.graphics.Graphics.save().

  • preferred_mime_types – list of mime types. The graphics output mime types in order of preference (i.e. best quality to worst).

  • allowed_mime_types – set of mime types (as strings). The graphics types that we can display. Output, if any, will be one of those.

  • 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 sage.structure.graphics_file.GraphicsFile encapsulating a suitable image file. Image is one of the preferred_mime_types. If allowed_mime_types is specified, the resulting file format matches one of these.

Alternatively, this function can return None to indicate that textual representation is preferable and/or no graphics with the desired mime type can be generated.