forked from scaleshift/scaleshift
/
ipython-notebook.go
51 lines (46 loc) · 1.25 KB
/
ipython-notebook.go
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
package lib
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/strslice"
)
// IPython defines a notebook
type IPython struct {
Meta struct {
KernelSpec struct {
Name string `json:"name"`
Lang string `json:"language"`
} `json:"kernelspec"`
Lang struct {
Name string `json:"name"`
FileExt string `json:"file_extension"`
} `json:"language_info"`
} `json:"metadata"`
}
// ParseIPython parses a notebook
func ParseIPython(path string) *IPython {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
ipy := &IPython{}
err = json.Unmarshal(raw, ipy)
if err != nil {
return nil
}
return ipy
}
// ConvertNotebook converts Jupyter notebook to Python scripts
func ConvertNotebook(ctx context.Context, image, ipynb, to, workdir string, mounts []mount.Mount) error {
_, err := RunAndWaitDockerContainer(ctx, "container-name", &container.Config{
Image: image,
Entrypoint: strslice.StrSlice([]string{"jupyter"}),
Cmd: strslice.StrSlice([]string{"nbconvert", "--to", to, ipynb}),
WorkingDir: fmt.Sprintf("%s/workspace", workdir),
}, &container.HostConfig{Mounts: mounts}, nil)
return err
}