New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add IPython.embed_kernel() #1357
Changes from all commits
25e21eb
8f53ec7
a92b812
a96b676
a2ec8bd
75b852d
087e9c3
030fc46
a61f445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,3 +85,16 @@ def debugx(expr,pre_msg=''): | |
# deactivate it by uncommenting the following line, which makes it a no-op | ||
#def debugx(expr,pre_msg=''): pass | ||
|
||
def extract_module_locals(depth=0): | ||
"""Returns (module, locals) of the funciton `depth` frames away from the caller""" | ||
f = sys._getframe(depth + 1) | ||
global_ns = f.f_globals | ||
module = sys.modules[global_ns['__name__']] | ||
return (module, f.f_locals) | ||
|
||
def extract_module_locals_above(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function seems overly specific. Why not just replace its one use with Maybe I just bristle at the word above 'above'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I agree that the more general function is probably sufficient for the limited use of this, though. |
||
"""Returns (module, locals) of the funciton calling the caller. | ||
Like extract_module_locals() with a specified depth of 1.""" | ||
# we're one frame away from the target, the function we call would be two frames away | ||
return extract_module_locals(1 + 1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,8 @@ class Kernel(Configurable): | |
iopub_socket = Instance('zmq.Socket') | ||
stdin_socket = Instance('zmq.Socket') | ||
log = Instance(logging.Logger) | ||
user_module = Instance('types.ModuleType') | ||
user_ns = Dict(default_value=None) | ||
|
||
# Private interface | ||
|
||
|
@@ -110,6 +112,8 @@ def __init__(self, **kwargs): | |
# Initialize the InteractiveShell subclass | ||
self.shell = ZMQInteractiveShell.instance(config=self.config, | ||
profile_dir = self.profile_dir, | ||
user_module = self.user_module, | ||
user_ns = self.user_ns, | ||
) | ||
self.shell.displayhook.session = self.session | ||
self.shell.displayhook.pub_socket = self.iopub_socket | ||
|
@@ -585,6 +589,8 @@ def init_kernel(self): | |
stdin_socket=self.stdin_socket, | ||
log=self.log, | ||
profile_dir=self.profile_dir, | ||
user_module=self.user_module, | ||
user_ns=self.user_ns, | ||
) | ||
self.kernel = kernel | ||
kernel.record_ports(self.ports) | ||
|
@@ -639,6 +645,10 @@ def launch_kernel(*args, **kwargs): | |
return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', | ||
*args, **kwargs) | ||
|
||
def embed_kernel(module, local_ns): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function needs a docstring. @scottt: our dev guide describes our doc guidelines (which basically amount to pep8 and using the numpy docstring standard). |
||
app = IPKernelApp.instance(user_module=module, user_ns=local_ns) | ||
app.initialize([]) | ||
app.start() | ||
|
||
def main(): | ||
"""Run an IPKernel as an application""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this logic to get the calling scope should be inside the
embed_kernel
function inipkernel
, so that it has exactly the same API if it's imported from there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @takluyver here.