Skip to content

Instantly share code, notes, and snippets.

@ivarne
Last active December 21, 2015 13:59
Show Gist options
  • Save ivarne/a04426ff124962cf64d0 to your computer and use it in GitHub Desktop.
Save ivarne/a04426ff124962cf64d0 to your computer and use it in GitHub Desktop.
Simple multimedia in Julia

writemime options

MIME types is not completely unambiguous, and the writemime function is able to generate different representations conforming to the same MIME type. A classic example is how a matrix is written in MIME"text/plain" to the terminal, and the writemime function uses the extra options passed to truncate the number of rows so that the matrix fitts in one page on the terminal. The various display functions might add options like character_colls, but never override or remove them. The writemime function should always have defaults for the options it supports and not raise errors for extra options or if impossible options are given.

character_colls
The desired length of lines when written to a text based format. If not specified in the call to display the Display object will set this to the character width of the terminal.
character_rows
The desired height/number of lines of the text based format. If not specified in the call to display the Display object will set this option to the height of the terminal.
truncate
Boolean parameter on whether to ignore parts of the object to reduce the size to fit within character_cols and character_rows
pixel_width
If the type can be transformed to a bitmap based format, the writemime function might use this property to generate the image with appropriate resolution.
pixel_height
Number of pixels in the vertical direction for bitmap based images.

Then some suggestions for what might be added

space_indentation
Number of spaces that should be used for each indentation level for indented text. -1 indicates TAB indentation
fps
Number of frames per second to generate for video and animations.
duration
Length of generated video/animation in seconds.

Module writers that implement writemime functions for their types might extend this list with their own options, and if others implement the same functionality it might be added to this standard list.

Draft for more Julia multimedia documentation.

Julia has a simple API for adding basic multimedia capabilities to your programs. The design is inspired by the web browser and based on the MIME Content Types in RFC2046. The system is divided in two functions with many methods. The first function is the writemime(io, MIME"text/plain", x::YourType; opts...) that is implemented for most of the types in the standard library. This function takes an object of YourType and writes a text/plain representation to the io object. The other function is the display(d::Display, mime, x; opts...) function that calls the correct writemime function and shows the result to the user. There are also convenience functions where all the arguments but x is optional.

.. function:: display(d::Display, mime, x; opts...)
              display(x; opts...)
              display(d::Display, x; opts...)
              display(mime, x; opts...)

To generalize the multimedia functionality abstract Display is the common type for the simple MIME displays in julia. When you want to display something; you can either give a specific Display instance to the display function, or julia will iterate through the global display-backend stack and use the first that supports one of the MIME types that your object supports. The default Display is the TextDisplay that prefers MIME"text/plain" and prints to STDOUT. The only requirement for a subclass of Display is that it must have a function display(d::DisplayType, mime, x; opts...) that is responsible for taking your object x and transforming it to the MIME representation with the help of writemime and somehow show the result to the user, or raise a MethodError if the correct writemime function does not exist. If you do not like the default prioritization of the MIME types you might also make a specialized version without the mime parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment