Updating cells

This requires IOcaml > 0.3.1.

There are a couple of special API calls which all you to manipulate a cells output area a bit more finely.

val cell_context : unit -> cell_context

This returns a handle which refers to the current cells (ie the one in which this call is made) output area.

val send_clear : unit -> unit

Clears the output area.

Further to this the standard calls like send_mime,display etc now take an optional argument called context which can direct output to the appropriate cell.


In [1]:
let context = Iocaml.cell_context()


val context : Iocaml.cell_context = <abstr>
Hello from over here

context refers to the area just above here.

We can send some data to it.


In [2]:
Iocaml.display ~context "text/html" "<b>Hello from over here</b>"


- : unit = ()

It's possible to do some interactive stuff as well.


In [3]:
#require "unix"




In [6]:
let context = Iocaml.cell_context()
let _ = Thread.create 
    (fun () ->
        for i=0 to 10 do 
            Iocaml.send_clear ~context ();
            Iocaml.display ~context "text/html" 
                (Printf.sprintf "<b>I can count: %i</b>" i);
            Unix.sleep 1
        done
    ) ()


I can count: 10
the mime channel should still work

Note that stdout, stderr and so on are not controlled by the mechanism and will still be directed to the last executed cell.

We could emulate a printf like call with something like:

let cell_printf fmt ... = Iocaml.display "text/raw" (sprintf fmt ...)

Not sure if that's possible in ocaml

The mime channel stuff should also work ok. However, if you are going to use mime from multiple threads you are going to have problems...


In [8]:
Printf.fprintf Iocaml.mime "<i>the mime channel should still work</i>";
Iocaml.send_mime ~context "text/html"


- : unit = ()

In [10]:
Printf.fprintf Iocaml.mime "<i>and again without a context</i>";
Iocaml.send_mime "text/html"


and again without a context
- : unit = ()

In [ ]: