-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernels.py
executable file
·109 lines (81 loc) · 3.1 KB
/
kernels.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import os
from distutils.version import LooseVersion
import IPython
from IPython.html.utils import url_path_join
try:
if LooseVersion(IPython.__version__) < LooseVersion('3.0'):
raise ImportError("kernel service requires IPython ≥ 3.0, found %s" % IPython.__version__)
except TypeError:
pass
import tornado
import tornado.options
from tornado import httpserver
from tornado import web
from tornado.log import app_log
from IPython.kernel.kernelspec import KernelSpecManager
from IPython.html.services.kernels.kernelmanager import MappingKernelManager
from IPython.html.services.kernels.handlers import default_handlers as kernels_handlers
from IPython.html.services.kernelspecs.handlers import default_handlers as kernelspecs_handlers
default_handlers = kernels_handlers + kernelspecs_handlers
def fix_base_path(base_path):
if not base_path.startswith('/'):
base_path = '/' + base_path
if not base_path.endswith('/'):
base_path = base_path + '/'
return base_path
class WebApp(web.Application):
def __init__(self, kernel_manager, settings):
base_path = settings['base_path']
handlers = [ tuple([url_path_join(base_path, handler[0])] + list(handler[1:])) for handler in default_handlers ]
super(WebApp, self).__init__(handlers, **settings)
def main():
tornado.options.define('base_path', default='/',
help="Base path for the server (e.g. /jupyter/)"
)
tornado.options.define('port', default=8000,
help="Port to serve on, defaults to 8000"
)
tornado.options.parse_command_line()
opts = tornado.options.options
kernel_manager = MappingKernelManager()
# Start the default kernel automatically
kernel_id = kernel_manager.start_kernel()
base_path = fix_base_path(opts.base_path)
# Loosey goosey
headers = {
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "",
}
allow_origin='*'
allow_origin_pat=''
from jinja2 import Environment, FileSystemLoader
template_path=IPython.html.DEFAULT_TEMPLATE_PATH_LIST
env = Environment(loader=FileSystemLoader(template_path))
settings = dict(
cookie_secret=os.environ.get('COOKIE_SECRET', 'secret'),
cookie_name='ignored',
kernel_manager=kernel_manager,
base_path=base_path,
headers=headers,
allow_origin=allow_origin,
allow_origin_pat=allow_origin_pat,
kernel_spec_manager=KernelSpecManager(),
static_path=IPython.html.DEFAULT_STATIC_FILES_PATH,
static_file_path=[IPython.html.DEFAULT_STATIC_FILES_PATH],
jinja2_env=env,
)
app = WebApp(kernel_manager, settings)
server = httpserver.HTTPServer(app)
server.listen(opts.port)
app_log.info("Serving at http://127.0.0.1:{}{}api/kernels".format(opts.port, base_path))
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
app_log.info("Interrupted...")
finally:
kernel_manager.shutdown_all()
if __name__ == '__main__':
main()