package jupyter

  1. Overview
  2. Docs
An OCaml kernel for Jupyter notebook

Install

Dune Dependency

Authors

Maintainers

Sources

jupyter-2.8.3.tbz
sha256=1db1bb6fd1e9f2bafb7b038a7a98a351f67a1d25df2ecdb94115c6f94e5cccf5

doc/src/jupyter.notebook/jupyter_notebook.ml.html

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
(* ocaml-jupyter --- An OCaml kernel for Jupyter

   Copyright (c) 2017 Akinori ABE

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE. *)

(** 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
OCaml

Innovation. Community. Security.