Source file jupyter_notebook.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(** A library for Jupyter notebooks *)
open Jupyter.Iopub
type ctx = Unsafe.ctx
type display_id = string
(** {2 Display} *)
let display ?ctx ?display_id ?(metadata = `Assoc []) ?(base64 = false) mime data =
let data =
if base64 then Base64.encode data
|> function
| Ok result -> result
| Error (`Msg erro) -> raise @@ Failure erro
else data
in
let send content = Unsafe.send_iopub ?ctx content in
match display_id with
| None ->
let display_id = Uuidm.(to_string (v `V4)) in
send (IOPUB_DISPLAY_DATA {
display_data = `Assoc [mime, `String data];
display_metadata = metadata;
display_transient = Some { display_id };
}) ;
display_id
| Some display_id ->
send (IOPUB_UPDATE_DISPLAY_DATA {
display_data = `Assoc [mime, `String data];
display_metadata = metadata;
display_transient = Some { display_id };
}) ;
display_id
let display_file ?ctx ?display_id ?metadata ?base64 mime filename =
let ic = open_in_bin filename in
let n = in_channel_length ic in
let s = really_input_string ic n in
close_in ic ;
display ?ctx ?display_id ?metadata ?base64 mime s
let clear_output ?ctx ?(wait = false) () =
Unsafe.send_iopub ?ctx (IOPUB_CLEAR_OUTPUT { clear_wait = wait })
let cell_context () =
match !Unsafe.context with
| None -> failwith "JupyterNotebook has no execution context"
| Some ctx -> ctx
(** {2 Printf} *)
let formatter_buf = Buffer.create 128
let formatter = Format.formatter_of_buffer formatter_buf
let printf fmt = Format.fprintf formatter fmt
let display_formatter ?ctx ?display_id ?metadata ?base64 mime =
Format.pp_print_flush formatter () ;
let data = Buffer.contents formatter_buf in
Buffer.clear formatter_buf ;
display ?ctx ?display_id ?metadata ?base64 mime data
(** {2 Utilities} *)
module Bench = Jupyter_notebook__Bench
module Process = Jupyter_notebook__Process
module Eval = Jupyter_notebook__Eval