Show More
@@ -0,0 +1,99 | |||||
|
1 | """Utilities for connecting to kernels | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Min Ragan-Kelley | |||
|
6 | ||||
|
7 | """ | |||
|
8 | ||||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | # Copyright (C) 2011 The IPython Development Team | |||
|
11 | # | |||
|
12 | # Distributed under the terms of the BSD License. The full license is in | |||
|
13 | # the file COPYING, distributed as part of this software. | |||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | ||||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | # Imports | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | import json | |||
|
21 | import sys | |||
|
22 | from subprocess import Popen, PIPE | |||
|
23 | ||||
|
24 | from IPython.utils.path import filefind | |||
|
25 | from IPython.utils.py3compat import str_to_bytes | |||
|
26 | ||||
|
27 | ||||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | # Functions | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | ||||
|
32 | def get_connection_file(app=None): | |||
|
33 | """Return the path to the connection file of an app | |||
|
34 | ||||
|
35 | Parameters | |||
|
36 | ---------- | |||
|
37 | app : KernelApp instance [optional] | |||
|
38 | If unspecified, the currently running app will be used | |||
|
39 | """ | |||
|
40 | if app is None: | |||
|
41 | from IPython.zmq.kernelapp import KernelApp | |||
|
42 | if not KernelApp.initialized(): | |||
|
43 | raise RuntimeError("app not specified, and not in a running Kernel") | |||
|
44 | ||||
|
45 | app = KernelApp.instance() | |||
|
46 | return filefind(app.connection_file, ['.', app.profile_dir.security_dir]) | |||
|
47 | ||||
|
48 | def get_connection_info(unpack=False): | |||
|
49 | """Return the connection information for the current Kernel. | |||
|
50 | ||||
|
51 | Parameters | |||
|
52 | ---------- | |||
|
53 | unpack : bool [default: False] | |||
|
54 | if True, return the unpacked dict, otherwise just the string contents | |||
|
55 | of the file. | |||
|
56 | ||||
|
57 | Returns | |||
|
58 | ------- | |||
|
59 | The connection dictionary of the current kernel, as string or dict, | |||
|
60 | depending on `unpack`. | |||
|
61 | """ | |||
|
62 | cf = get_connection_file() | |||
|
63 | with open(cf) as f: | |||
|
64 | info = f.read() | |||
|
65 | ||||
|
66 | if unpack: | |||
|
67 | info = json.loads(info) | |||
|
68 | # ensure key is bytes: | |||
|
69 | info['key'] = str_to_bytes(info.get('key', '')) | |||
|
70 | return info | |||
|
71 | ||||
|
72 | def connect_qtconsole(argv=None): | |||
|
73 | """Connect a qtconsole to the current kernel. | |||
|
74 | ||||
|
75 | This is useful for connecting a second qtconsole to a kernel, or to a | |||
|
76 | local notebook. | |||
|
77 | ||||
|
78 | Parameters | |||
|
79 | ---------- | |||
|
80 | argv : list [optional] | |||
|
81 | Any extra args to be passed to the console. | |||
|
82 | ||||
|
83 | Returns | |||
|
84 | ------- | |||
|
85 | subprocess.Popen instance running the qtconsole frontend | |||
|
86 | """ | |||
|
87 | argv = [] if argv is None else argv | |||
|
88 | ||||
|
89 | # get connection file from current kernel | |||
|
90 | cf = get_connection_file() | |||
|
91 | ||||
|
92 | cmd = ';'.join([ | |||
|
93 | "from IPython.frontend.qt.console import qtconsoleapp", | |||
|
94 | "qtconsoleapp.main()" | |||
|
95 | ]) | |||
|
96 | ||||
|
97 | return Popen([sys.executable, '-c', cmd, '--existing', cf] + argv, stdout=PIPE, stderr=PIPE) | |||
|
98 | ||||
|
99 |
@@ -31,9 +31,13 from IPython.core.displaypub import DisplayPublisher | |||||
31 | from IPython.core.macro import Macro |
|
31 | from IPython.core.macro import Macro | |
32 | from IPython.core.magic import MacroToEdit |
|
32 | from IPython.core.magic import MacroToEdit | |
33 | from IPython.core.payloadpage import install_payload_page |
|
33 | from IPython.core.payloadpage import install_payload_page | |
|
34 | from IPython.lib.kernel import ( | |||
|
35 | get_connection_file, get_connection_info, connect_qtconsole | |||
|
36 | ) | |||
34 | from IPython.utils import io |
|
37 | from IPython.utils import io | |
35 | from IPython.utils.jsonutil import json_clean |
|
38 | from IPython.utils.jsonutil import json_clean | |
36 | from IPython.utils.path import get_py_filename |
|
39 | from IPython.utils.path import get_py_filename | |
|
40 | from IPython.utils.process import arg_split | |||
37 | from IPython.utils.traitlets import Instance, Type, Dict, CBool |
|
41 | from IPython.utils.traitlets import Instance, Type, Dict, CBool | |
38 | from IPython.utils.warn import warn, error |
|
42 | from IPython.utils.warn import warn, error | |
39 | from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary |
|
43 | from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary | |
@@ -442,18 +446,14 class ZMQInteractiveShell(InteractiveShell): | |||||
442 | $> ipython <app> --existing |
|
446 | $> ipython <app> --existing | |
443 |
|
447 | |||
444 | """ |
|
448 | """ | |
445 | from IPython.zmq.kernelapp import KernelApp |
|
|||
446 | if not KernelApp.initialized(): |
|
|||
447 | error("KernelApp is not initialized. I cannot find the connection info") |
|
|||
448 | return |
|
|||
449 | app = KernelApp.instance() |
|
|||
450 | try: |
|
449 | try: | |
451 | with open(app.connection_file) as f: |
|
450 | connection_file = get_connection_file() | |
452 | s = f.read() |
|
451 | info = get_connection_info(unpack=False) | |
453 | except Exception as e: |
|
452 | except Exception as e: | |
454 |
error("Could not |
|
453 | error("Could not get connection info: %r" % e) | |
455 | return |
|
454 | return | |
456 |
|
|
455 | ||
|
456 | print (info + '\n') | |||
457 | print ("Paste the above JSON into a file, and connect with:\n" |
|
457 | print ("Paste the above JSON into a file, and connect with:\n" | |
458 | " $> ipython <app> --existing <file>\n" |
|
458 | " $> ipython <app> --existing <file>\n" | |
459 | "or, if you are local, you can connect with just:\n" |
|
459 | "or, if you are local, you can connect with just:\n" | |
@@ -461,7 +461,7 class ZMQInteractiveShell(InteractiveShell): | |||||
461 | "or even just:\n" |
|
461 | "or even just:\n" | |
462 | " $> ipython <app> --existing\n" |
|
462 | " $> ipython <app> --existing\n" | |
463 | "if this is the most recent IPython session you have started." |
|
463 | "if this is the most recent IPython session you have started." | |
464 |
% os.path.basename( |
|
464 | % os.path.basename(connection_file) | |
465 | ) |
|
465 | ) | |
466 |
|
466 | |||
467 | def magic_qtconsole(self, arg_s): |
|
467 | def magic_qtconsole(self, arg_s): | |
@@ -470,20 +470,12 class ZMQInteractiveShell(InteractiveShell): | |||||
470 | Useful for connecting a qtconsole to running notebooks, for better |
|
470 | Useful for connecting a qtconsole to running notebooks, for better | |
471 | debugging. |
|
471 | debugging. | |
472 | """ |
|
472 | """ | |
473 | from IPython.zmq.kernelapp import KernelApp |
|
473 | try: | |
474 |
|
474 | p = connect_qtconsole(arg_split(arg_s, os.name=='posix')) | ||
475 | if not KernelApp.initialized(): |
|
475 | except Exception as e: | |
476 | error("KernelApp is not initialized. %qtconsole magic must be run from a Kernel") |
|
476 | error("Could not start qtconsole: %r" % e) | |
477 | return |
|
477 | return | |
478 | app = KernelApp.instance() |
|
|||
479 |
|
||||
480 | cmd = ';'.join([ |
|
|||
481 | "from IPython.frontend.qt.console import qtconsoleapp", |
|
|||
482 | "qtconsoleapp.main()" |
|
|||
483 | ]) |
|
|||
484 |
|
478 | |||
485 | return Popen([sys.executable, '-c', cmd, '--existing', app.connection_file], |
|
|||
486 | stdout=PIPE,stderr=PIPE) |
|
|||
487 |
|
479 | |||
488 | def set_next_input(self, text): |
|
480 | def set_next_input(self, text): | |
489 | """Send the specified text to the frontend to be presented at the next |
|
481 | """Send the specified text to the frontend to be presented at the next |
General Comments 0
You need to be logged in to leave comments.
Login now