IOCaml is a basic OCaml kernel for IPython.
Code can be written in notebook cells and is executed by the OCaml toploop when ctrl-return or shift-return is pressed
In [1]:
let a = 23
Out[1]:
Note that you do not need ";;" at the end of the statement. The code in each cell is read as if it were a file being loaded into the top loop. Multiple expressions can be entered in each cell.
In [2]:
let b = "hello"
let c = "world"
let d = b ^ " " ^ c
Out[2]:
Out[2]:
Out[2]:
You can load system libraries as normal.
In [1]:
#use "topfind"
In [4]:
#require "bigarray"
In [5]:
Bigarray.Array1.dim
Out[5]:
Errors are labelled with file [cell number]
In [6]:
let a = 1
let b = 2
let c = a + b + 1.0
Out[6]:
Out[6]:
Out[6]:
There are three basic output types; messages from the compiler, stdout and stderr. All three are sent to the notebook.
In [7]:
Printf.printf "hello";
Printf.fprintf stderr "world"
Data is sent to the current cell as it is generated.
In [8]:
#thread;;
In [9]:
Thread.create (fun () -> for i=0 to 5 do Printf.printf "hello world %i\n%!" i; Unix.sleep 1 done) ()
The current cell is the one just executed, so the above could get confusing...
In [10]:
Thread.create (fun () -> for i=0 to 5 do Printf.printf "hello world %i\n%!" i; Unix.sleep 1 done) ()
In [12]:
Printf.printf "hello world in the middle\n%!"
Out[12]:
Some functions are available for controlling interaction with the toploop and notebook in the Iocaml
module
In [2]:
#require "iocaml-kernel.notebook"
There are a few functions for disabling output.
In [3]:
Iocaml.suppress_stdout;;
Iocaml.suppress_stderr;;
Iocaml.suppress_compiler;;
Iocaml.suppress_all;;
Out[3]:
Out[3]:
Out[3]:
Out[3]:
In [4]:
Iocaml.suppress_stdout true;
Printf.printf "where am I?\n%!";
Iocaml.suppress_stdout false;
Printf.printf "I'm back!\n%!";
Out[4]:
The kernel has a command line option -package pkg
which automatically #use
's topfind and loads the required package. It can be specified multiple times to load extra packages. The default ipython configuration file automatically loads the iocaml package.
Data can be sent to the front end and displayed as media. The Iocaml API provides 2 ways to do this.
In [5]:
Iocaml.display
Out[5]:
Iocaml.display ~base64 mime_type "data to display"
displays the data with the given mime type. The optional base64 parameter will encode the data if that is required.
In [6]:
Iocaml.mime;;
Iocaml.send_mime;;
Out[6]:
Out[6]:
Alternatively data can be written to Iocaml.mime : out_channel
. When all the data is ready call Iocaml.send_mime ~base64 mime_type
to display it.
In [7]:
Iocaml.display "text/html" "<b>hello </b><i>world</i>"
Out[7]:
This is a more complex example which renders a string array array
as a table
In [8]:
open Printf
let to_table a =
let open Iocaml in
let open Printf in
fprintf mime "<table>\n";
Array.iter (fun data ->
fprintf mime "<tr>\n";
Array.iter (fun data ->
fprintf mime "<td>%s</td>" data
) data;
fprintf mime "</tr>\n";
) a;
fprintf mime "</table>"
Out[8]:
In [9]:
to_table (Array.init 8 (fun row -> Array.init 6 (fun col -> sprintf "row=%i col=%i\n" row col)));;
Iocaml.send_mime "text/html"
Out[9]:
Out[9]:
You can embed images from a url with html.
In [10]:
Iocaml.display "text/html" "<img src=\"http://tineye.com/images/imageurl-firefox.png\" width=\"400\" height=\"400\"></image>"
Out[10]:
Images work similarly except they require base64 encoding.
In [11]:
let () = Iocaml.suppress_all true;;
#mod_use "/home/andyman/dev/tools/ocaml/iocaml/iocaml/png.ml";;
let width, height = 32,32;;
let pixels = Array.init (width*height) (fun i -> i);;
In [12]:
Iocaml.suppress_all false;
Png.write_png_chnl Iocaml.mime pixels width height;
Iocaml.send_mime ~base64:true "image/png"
Out[12]:
In [13]:
Out[13]:
In [ ]: