##// END OF EJS Templates
remove hardcoded hb port from internal example...
MinRK -
Show More
@@ -1,68 +1,65 b''
1 #-----------------------------------------------------------------------------
1 #-----------------------------------------------------------------------------
2 # Imports
2 # Imports
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4
4
5 import subprocess
5 import subprocess
6 import sys
6 import sys
7
7
8 from IPython.zmq.ipkernel import IPKernelApp
8 from IPython.zmq.ipkernel import IPKernelApp
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Functions and classes
11 # Functions and classes
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 def pylab_kernel(gui):
13 def pylab_kernel(gui):
14 """Launch and return an IPython kernel with pylab support for the desired gui
14 """Launch and return an IPython kernel with pylab support for the desired gui
15 """
15 """
16 kernel = IPKernelApp()
16 kernel = IPKernelApp()
17 # FIXME: we're hardcoding the heartbeat port b/c of a bug in IPython 0.11
17 kernel.initialize(['python', '--pylab=%s' % gui,
18 # that will set it to 0 if not specified. Once this is fixed, the --hb
19 # parameter can be omitted and all ports will be automatic
20 kernel.initialize(['python', '--pylab=%s' % gui, '--hb=19999',
21 #'--log-level=10'
18 #'--log-level=10'
22 ])
19 ])
23 return kernel
20 return kernel
24
21
25
22
26 def qtconsole_cmd(kernel):
23 def qtconsole_cmd(kernel):
27 """Compute the command to connect a qt console to an already running kernel
24 """Compute the command to connect a qt console to an already running kernel
28 """
25 """
29 ports = ['--%s=%d' % (name, port) for name, port in kernel.ports.items()]
26 ports = ['--%s=%d' % (name, port) for name, port in kernel.ports.items()]
30 return ['ipython', 'qtconsole', '--existing'] + ports
27 return ['ipython', 'qtconsole', '--existing'] + ports
31
28
32
29
33 class InternalIPKernel(object):
30 class InternalIPKernel(object):
34
31
35 def init_ipkernel(self, backend):
32 def init_ipkernel(self, backend):
36 # Start IPython kernel with GUI event loop and pylab support
33 # Start IPython kernel with GUI event loop and pylab support
37 self.ipkernel = pylab_kernel(backend)
34 self.ipkernel = pylab_kernel(backend)
38 # To create and track active qt consoles
35 # To create and track active qt consoles
39 self._qtconsole_cmd = qtconsole_cmd(self.ipkernel)
36 self._qtconsole_cmd = qtconsole_cmd(self.ipkernel)
40 self.consoles = []
37 self.consoles = []
41
38
42 # This application will also act on the shell user namespace
39 # This application will also act on the shell user namespace
43 self.namespace = self.ipkernel.shell.user_ns
40 self.namespace = self.ipkernel.shell.user_ns
44 # Keys present at startup so we don't print the entire pylab/numpy
41 # Keys present at startup so we don't print the entire pylab/numpy
45 # namespace when the user clicks the 'namespace' button
42 # namespace when the user clicks the 'namespace' button
46 self._init_keys = set(self.namespace.keys())
43 self._init_keys = set(self.namespace.keys())
47
44
48 # Example: a variable that will be seen by the user in the shell, and
45 # Example: a variable that will be seen by the user in the shell, and
49 # that the GUI modifies (the 'Counter++' button increments it):
46 # that the GUI modifies (the 'Counter++' button increments it):
50 self.namespace['app_counter'] = 0
47 self.namespace['app_counter'] = 0
51 #self.namespace['ipkernel'] = self.ipkernel # dbg
48 #self.namespace['ipkernel'] = self.ipkernel # dbg
52
49
53 def print_namespace(self, evt=None):
50 def print_namespace(self, evt=None):
54 print "\n***Variables in User namespace***"
51 print "\n***Variables in User namespace***"
55 for k, v in self.namespace.iteritems():
52 for k, v in self.namespace.iteritems():
56 if k not in self._init_keys and not k.startswith('_'):
53 if k not in self._init_keys and not k.startswith('_'):
57 print '%s -> %r' % (k, v)
54 print '%s -> %r' % (k, v)
58 sys.stdout.flush()
55 sys.stdout.flush()
59
56
60 def new_qt_console(self, evt=None):
57 def new_qt_console(self, evt=None):
61 self.consoles.append(subprocess.Popen(self._qtconsole_cmd))
58 self.consoles.append(subprocess.Popen(self._qtconsole_cmd))
62
59
63 def count(self, evt=None):
60 def count(self, evt=None):
64 self.namespace['app_counter'] += 1
61 self.namespace['app_counter'] += 1
65
62
66 def cleanup_consoles(self, evt=None):
63 def cleanup_consoles(self, evt=None):
67 for c in self.consoles:
64 for c in self.consoles:
68 c.kill()
65 c.kill()
General Comments 0
You need to be logged in to leave comments. Login now