package jupyter

  1. Overview
  2. Docs

Source file iopub.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(* 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. *)

(** Contents on IOPUB channels *)

(** {2 Streams} *)

type stream_name =
  | IOPUB_STDOUT [@name "stdout"]
  | IOPUB_STDERR [@name "stderr"]
[@@deriving yojson]

type stream =
  {
    stream_name : stream_name Json.enum [@key "name"];
    stream_text : string [@key "text"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Display data} *)

type transient =
  {
    display_id : string [@key "display_id"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

type display_data =
  {
    display_data : Json.t [@key "data"];
    display_metadata : Json.t [@key "metadata"];
    display_transient : transient option [@key "transient"] [@default None];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Code inputs} *)

type exec_input =
  {
    exin_code : string [@key "code"];
    exin_count : int [@key "execution_count"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Execution results} *)

type exec_result =
  {
    exres_count : int [@key "execution_count"];
    exres_data : Json.t [@key "data"];
    exres_metadata : Json.t [@key "metadata"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Kernel status} *)

type exec_status =
  | IOPUB_BUSY     [@name "busy"]
  | IOPUB_IDLE     [@name "idle"]
  | IOPUB_STARTING [@name "starting"]
[@@deriving yojson]

type status =
  {
    kernel_state : exec_status Json.enum [@key "execution_state"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Execution errors} *)

type error =
  {
    ename : string [@key "ename"];
    evalue : string [@key "evalue"];
    traceback : string list [@key "traceback"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Clear output} *)

type clear_output =
  {
    clear_wait : bool [@key "wait"];
  } [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 IOPUB Request} *)

type request = unit [@@deriving yojson]

(** {2 IOPUB Reply} *)

type reply =
  | IOPUB_STREAM of stream [@name "stream"]
  | IOPUB_DISPLAY_DATA of display_data [@name "display_data"]
  | IOPUB_UPDATE_DISPLAY_DATA of display_data [@name "update_display_data"]
  | IOPUB_EXECUTE_INPUT of exec_input [@name "execute_input"]
  | IOPUB_EXECUTE_RESULT of exec_result [@name "execute_result"]
  | IOPUB_ERROR of error [@name "error"]
  | IOPUB_STATUS of status [@name "status"]
  | IOPUB_CLEAR_OUTPUT of clear_output [@name "clear_output"]
  | IOPUB_COMM_OPEN of Comm.t [@name "comm_open"]
  | IOPUB_COMM_MSG of Comm.t [@name "comm_msg"]
  | IOPUB_COMM_CLOSE of Comm.t [@name "comm_close"]
[@@deriving yojson]

let stream ~name text =
  IOPUB_STREAM { stream_name = name; stream_text = text; }

let execute_result ?(metadata = `Assoc []) ~count data =
  IOPUB_EXECUTE_RESULT {
    exres_count = count;
    exres_data = data;
    exres_metadata = metadata;
  }

let error ?(name = "error") ~value traceback =
  IOPUB_ERROR {
    ename = name;
    evalue = value;
    traceback;
  }