install.py 2.7 KB
Newer Older
1
2
3
4
5
import argparse
import json
import os
import sys

6
from .interview_kernel import Interview
7

8
9
from jupyter_client.kernelspec import KernelSpecManager
from IPython.utils.tempdir import TemporaryDirectory
10
from shutil import copyfile
11

12
kernel_json = Interview.kernel_json
13

14
15
16
17
18
19

def install_my_kernel_spec(user=True, prefix=None):
    with TemporaryDirectory() as td:
        os.chmod(td, 0o755) # Starts off as 700, not user readable
        with open(os.path.join(td, 'kernel.json'), 'w') as f:
            json.dump(kernel_json, f, sort_keys=True)
20
21
22
        try:
            interview = Interview()
            with open(os.path.join(td, 'kernel.js'), 'w') as f:
23
                # javascript code that sets an initial markdown cell in every new notebook
24
25
26
27
28
29
30
                js = """define(['base/js/namespace'], function(Jupyter)
                        {{
                            function onload()
                            {{
                                if (Jupyter.notebook.get_cells().length ===1)
                                {{
                                    Jupyter.notebook.insert_cell_above('markdown').set_text(`{}`);
31
                                    Jupyter.notebook.get_cell(0).render();
32
33
34
35
36
37
38
39
40
41
42
43
44
45
                                }}
                                console.log("interview kernel.js loaded")
                            }}
                            return {{
                                onload: onload
                            }};
                        }});""".format(interview.my_markdown_greeting.replace("`", "\\`"))

                f.write(js)
                print(js)

        except Exception:
            print('could not copy kernel.js, will not see initial message in notebook')
            raise
46

47
48
        print("Installing Jupyter kernel spec")
        KernelSpecManager().install_kernel_spec(td, 'Interview', user=user, prefix=prefix)
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

def _is_root():
    try:
        return os.geteuid() == 0
    except AttributeError:
        return False # assume not an admin on non-Unix platforms

def main(argv=None):
    ap = argparse.ArgumentParser()
    ap.add_argument('--user', action='store_true',
        help="Install to the per-user kernels registry. Default if not root.")
    ap.add_argument('--sys-prefix', action='store_true',
        help="Install to sys.prefix (e.g. a virtualenv or conda env)")
    ap.add_argument('--prefix',
        help="Install to the given prefix. "
             "Kernelspec will be installed in {PREFIX}/share/jupyter/kernels/")
    args = ap.parse_args(argv)

    if args.sys_prefix:
        args.prefix = sys.prefix
    if not args.prefix and not _is_root():
        args.user = True

    install_my_kernel_spec(user=args.user, prefix=args.prefix)

if __name__ == '__main__':
    main()