Skip to content

Instantly share code, notes, and snippets.

@andrewray
Created May 17, 2015 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewray/b67ca67885b49d7a5e98 to your computer and use it in GitHub Desktop.
Save andrewray/b67ca67885b49d7a5e98 to your computer and use it in GitHub Desktop.

To access Iocaml require the package iocaml-kernel.notebook

From a discussion on using plplot in iocaml

yes, either Iocaml.dispaly or Iocaml.send_mime could do the job in sligthly different ways:

?context:Iocaml.cell_context -> ?base64:bool -> string -> string -> unit

The last two options are "mime_type" and "data as a string". You also need the base64 option here so we would do:

Iocaml.display ~base64:true "image/png" png_data_loaded_from_file

So you need to load the png data as a string from the file. Various ways to do this in http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html, but heres some untested code;

let load_file_as_string file =
 let b = Buffer.create 1024 in 
 let rec f () = 
  try Buffer.add_char (Char.chr (input_byte file)); f() 
  with End_of_file -> Buffer.contents b 
  in
 f()

Using the mime stuff can be done with a function like copy_stream : in_channel -> out_channel -> unit ie

let rec copy_stream in_chan out_chan = 
 try 
  output_byte out_chan (input_byte in_chan)); 
  copy_stream in_chan out_chan 
 with _ -> ()

Then

copy_stream in_file Iocaml.mime; 
Iocaml.send_mime ~base64:true "image/png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment