Skip to content

Commit 25ba84b

Browse files
jonludlamcraigfe
authored andcommitted
First cut of jupyter output
Signed-off-by: Jon Ludlam <[email protected]>
1 parent e5a86b0 commit 25ba84b

File tree

4 files changed

+169
-2
lines changed

4 files changed

+169
-2
lines changed

bin/dune

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(library
22
(name cli)
33
(modules cli)
4-
(libraries cmdliner fmt.cli logs.fmt fmt.tty logs.cli mdx))
4+
(libraries cmdliner fmt.cli logs.fmt fmt.tty logs.cli mdx yojson atdgen))
55

66
(executable
77
(name main)
@@ -10,6 +10,18 @@
1010
(modules :standard \ cli)
1111
(libraries cli mdx))
1212

13+
(rule
14+
(targets notebook_j.ml
15+
notebook_j.mli)
16+
(deps notebook.atd)
17+
(action (run atdgen -j -j-std %{deps})))
18+
19+
(rule
20+
(targets notebook_t.ml
21+
notebook_t.mli)
22+
(deps notebook.atd)
23+
(action (run atdgen -t %{deps})))
24+
1325
(install
1426
(section bin)
1527
(files

bin/jupyter.ml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
open Cmdliner
2+
3+
let txt t =
4+
let cell = Notebook_t.{
5+
cell_type = `Markdown;
6+
metadata = {
7+
collapsed = None;
8+
scrolled = None;
9+
};
10+
source = t;
11+
outputs = None;
12+
execution_count = None;
13+
} in
14+
cell
15+
16+
let execution_count = ref 1
17+
18+
let ocaml contents =
19+
let cell = Notebook_t.{
20+
cell_type = `Code;
21+
metadata = {
22+
collapsed = None;
23+
scrolled = None;
24+
};
25+
source = String.concat "\n" contents;
26+
outputs = Some [];
27+
execution_count = (Some !execution_count);
28+
} in
29+
incr execution_count;
30+
cell
31+
32+
33+
let toplevel x =
34+
let cell = Notebook_t.{
35+
cell_type = `Code;
36+
metadata = {
37+
collapsed = None;
38+
scrolled = None;
39+
};
40+
source = String.concat "\n" x.Mdx.Toplevel.command;
41+
outputs = Some [];
42+
execution_count = (Some !execution_count);
43+
}
44+
in
45+
incr execution_count;
46+
cell
47+
48+
49+
let run _setup syntax file =
50+
let cells = ref [] in
51+
Mdx.run ?syntax file ~f:(fun _file_contents items ->
52+
let rec collapse_text = function
53+
| Mdx.Text x :: Mdx.Text y :: xs ->
54+
collapse_text (Mdx.Text (x ^ "\n" ^ y) :: xs)
55+
| x::ys -> x :: collapse_text ys
56+
| [] -> []
57+
in
58+
List.iter (function
59+
| Mdx.Text x ->
60+
cells := (txt x) :: !cells
61+
| Mdx.Block {value=OCaml; contents; _} ->
62+
cells := (ocaml contents) :: !cells
63+
| Mdx.Block {value=Toplevel xs; _} ->
64+
let newcells = List.rev_map toplevel xs in
65+
cells := newcells @ !cells
66+
| _ -> ()
67+
) (collapse_text items);
68+
"OK"
69+
);
70+
let notebook = Notebook_t.{
71+
metadata = {
72+
kernelspec = {
73+
display_name = "OCaml 4.07.1";
74+
language = "OCaml";
75+
name = "ocaml-jupyter";
76+
};
77+
language_info = {
78+
name = "OCaml";
79+
version = "4.07.1";
80+
codemirror_mode = Some "text/x-ocaml";
81+
file_extension = ".ml";
82+
mimetype = "text/x-ocaml";
83+
nbconverter_exporter = None;
84+
pygments_lexer = "OCaml";
85+
};
86+
};
87+
nbformat = 4;
88+
nbformat_minor = 2;
89+
cells = List.rev !cells;
90+
} in
91+
Printf.fprintf stdout "%s" (Notebook_j.string_of_notebook notebook);
92+
0
93+
94+
95+
96+
let cmd: int Term.t * Term.info =
97+
let doc = "Convert an mdx file to a jupyter notebook." in
98+
Term.(pure run $ Cli.setup $ Cli.syntax $ Cli.file ),
99+
Term.info "jupyter" ~doc

bin/main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
open Cmdliner
1818

19-
let cmds = [ Test.cmd; Pp.cmd; Rule.cmd; Deps.cmd ]
19+
let cmds = [ Test.cmd; Pp.cmd; Rule.cmd; Deps.cmd; Jupyter.cmd ]
2020

2121
let main (`Setup ()) = `Help (`Pager, None)
2222

bin/notebook.atd

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
type kernelspec = {
2+
display_name : string;
3+
language: string;
4+
name: string;
5+
}
6+
7+
type language_info = {
8+
name: string;
9+
version: string;
10+
codemirror_mode: string nullable;
11+
file_extension: string;
12+
mimetype: string;
13+
nbconverter_exporter: string nullable;
14+
pygments_lexer: string;
15+
}
16+
17+
type metadata = {
18+
kernelspec: kernelspec;
19+
language_info: language_info;
20+
}
21+
22+
type cell_metadata = {
23+
?collapsed: bool nullable;
24+
?scrolled: bool nullable;
25+
}
26+
27+
type cell_type = [
28+
| Code <json name="code">
29+
| Markdown <json name="markdown">
30+
]
31+
32+
type output_type = [
33+
Stream <json name="stream">
34+
| Display_data <json name="display_data">
35+
| Execute_result <json name="execute_result">
36+
| Error <json name="error">
37+
]
38+
39+
type output = {
40+
output_type : output_type;
41+
}
42+
43+
type cell = {
44+
cell_type : cell_type;
45+
metadata: cell_metadata;
46+
source: string;
47+
?outputs: output list nullable;
48+
?execution_count: int option;
49+
} <json keep_nulls false>
50+
51+
type notebook = {
52+
metadata: metadata;
53+
nbformat: int;
54+
nbformat_minor: int;
55+
cells: cell list
56+
}

0 commit comments

Comments
 (0)