forked from jamesgao/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipythonhttp.py
More file actions
100 lines (87 loc) · 4.21 KB
/
ipythonhttp.py
File metadata and controls
100 lines (87 loc) · 4.21 KB
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
""" A minimal application using the IPython web notebook frontend.
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Local imports
import os
from IPython.external.argparse import ArgumentParser
from IPython.frontend.html.kernelmanager import HttpKernelManager, \
IPyHttpServer, IPyHttpHandler
#-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------
LOCALHOST = '127.0.0.1'
#-----------------------------------------------------------------------------
# Main entry point
#-----------------------------------------------------------------------------
def main():
""" Entry point for application.
"""
# Parse command line arguments.
parser = ArgumentParser()
kgroup = parser.add_argument_group('kernel options')
kgroup.add_argument('-e', '--existing', action='store_true',
help='connect to an existing kernel')
kgroup.add_argument('--ip', type=str, default=LOCALHOST,
help='set the kernel\'s IP address [default localhost]')
kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0,
help='set the XREQ channel port [default random]')
kgroup.add_argument('--sub', type=int, metavar='PORT', default=0,
help='set the SUB channel port [default random]')
kgroup.add_argument('--rep', type=int, metavar='PORT', default=0,
help='set the REP channel port [default random]')
kgroup.add_argument('--hb', type=int, metavar='PORT', default=0,
help='set the heartbeat port [default: random]')
egroup = kgroup.add_mutually_exclusive_group()
egroup.add_argument('--pure', action='store_true', help = \
'use a pure Python kernel instead of an IPython kernel')
egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
const='auto', help = \
"Pre-load matplotlib and numpy for interactive use. If GUI is not \
given, the GUI backend is matplotlib's, otherwise use one of: \
['tk', 'gtk', 'qt', 'wx', 'inline'].")
wgroup = parser.add_argument_group('widget options')
wgroup.add_argument('--paging', type=str, default='inside',
choices = ['inside', 'hsplit', 'vsplit', 'none'],
help='set the paging style [default inside]')
wgroup.add_argument('--rich', action='store_true',
help='enable rich text support')
wgroup.add_argument('--gui-completion', action='store_true',
help='use a GUI widget for tab completion')
args = parser.parse_args()
# Don't let ZMQ swallow KeyboardInterupts.
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Create a KernelManager and start a kernel.
kernel_manager = HttpKernelManager(xreq_address=(args.ip, args.xreq),
sub_address=(args.ip, args.sub),
rep_address=(args.ip, args.rep),
hb_address=(args.ip, args.hb))
if args.ip == LOCALHOST and not args.existing:
if args.pure:
kernel_manager.start_kernel(ipython=False)
elif args.pylab:
kernel_manager.start_kernel(pylab=args.pylab)
else:
kernel_manager.start_kernel()
kernel_manager.start_channels()
#Start the web server
server = IPyHttpServer(("", 8080), IPyHttpHandler)
server.serve_forever()
if __name__ == '__main__':
#FIXME: Ctrl-C is not trapped by this try-except, gets lost somewhere in kernel manager
try:
import threading
def defer():
"""Defer the startup of the browser
"""
import time
time.sleep(2)
import webbrowser
webbrowser.open("http://localhost:8080/notebook")
#I need threading here because the main function blocks -- better ideas?
threading.Thread(target=defer).start()
main()
except KeyboardInterrupt:
pass