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

writemime(stream, mime, x)

The `writemime` function is used to write an object `x` as a specific `mime` type to a given I/O `stream`. It is commonly used by the `display` functions to provide rich multimedia representations of user-defined types. To create a custom representation for a type `T`, you can define a new `writemime` method for `T`.

Arguments:
- `stream`: The output I/O stream where the content will be written.
- `mime`: The MIME type specifying the format of the content.
- `x`: The object to be written.

Common Examples:

1. **Writing an image to a file:**
```julia
julia> img = load("image.jpg");
julia> file = open("output.png", "w");
julia> writemime(file, MIME("image/png"), img);
julia> close(file);

This example loads an image from a file, creates a new file to write the modified image, and uses writemime to write the modified image as a PNG file.

  1. Displaying a custom multimedia representation:
    
    julia> struct MyType
           data::String
       end

julia> function Base.writemime(stream::IO, mime::MIME"text/html", x::MyType) println(stream, "

Custom representation: $(x.data)

") end

julia> x = MyType("Hello, world!"); julia> display(x, "text/html")

In this example, a custom `writemime` method is defined for a user-defined type `MyType` to display a custom HTML representation. When `display` is called with the specified MIME type, the custom representation is shown.

Note: `writemime` has different method signatures depending on the version of Julia. The example provided is for Julia 1.x. For Julia 0.6, the method signature is `writemime(mime::MIME, x)`.

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.

*Required Field
Details

Checking you are not a robot: