writemime
writemime(stream, mime, x)
The display
functions ultimately call writemime
in order to write an object x
as a given mime
type to a given I/O stream
(usually a memory buffer), if possible. In order to provide a rich multimedia representation of a user-defined type T
, it is only necessary to define a new writemime
method for T
, via: writemime(stream, ::MIME"mime", x::T) = ...
, where mime
is a MIME-type string and the function body calls write
(or similar) to write that representation of x
to stream
. (Note that the MIME""
notation only supports literal strings; to construct MIME
types in a more flexible manner use MIME{symbol("")}
.)
For example, if you define a MyImage
type and know how to write it to a PNG file, you could define a function writemime(stream, ::MIME"image/png", x::MyImage) = ...
to allow your images to be displayed on any PNG-capable Display
(such as IJulia). As usual, be sure to import Base.writemime
in order to add new methods to the built-in Julia function writemime
.
Technically, the MIME"mime"
macro defines a singleton type for the given mime
string, which allows us to exploit Julia's dispatch mechanisms in determining how to display objects of any given type.
Examples
See Also
deserialize, eachline, eof, fd, flush, IOBuffer, ismarked, isopen, isreadonly, mark, nb_available, open, pipeline, position, read, read!, readavailable, readbytes, readbytes!, readline, redirect_stderr, redirect_stdin, reset, seek, seekend, seekstart, serialize, skip, skipchars, TextDisplay, unmark, write, writemime,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.