Skip to content

Commit

Permalink
renamed %%kernel to %%kx (kernel execute, to match %px parallel execu…
Browse files Browse the repository at this point in the history
…te); added %kx CODE; added %%px -e to also evalute in the current kernel
  • Loading branch information
Doug Blank committed Sep 10, 2014
1 parent 9a21220 commit eeeb1c9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
44 changes: 38 additions & 6 deletions jupyter_kernel/magics/kernel_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@

class KernelMagic(Magic):
kernels = {}
kernel_name = None

@option(
'-k', '--kernel_name', action='store', default="default",
help='kernel name given to use for execution'
)
def line_kernel(self, module_name, class_name, kernel_name="default"):
"""
%kernel MODULE CLASS [NAME] - construct a kernel for sending code.
%kernel MODULE CLASS [-k NAME] - construct a kernel for sending code.
Also returns the kernel as output.
Example:
%kernel bash_kernel BashKernel bash
%kernel bash_kernel BashKernel -k bash
Use `%%kernel bash` to send code to the kernel.
Use `%kx` or `%%kx` to send code to the kernel.
"""
self.kernel_name = kernel_name
module = __import__(module_name)
class_ = getattr(module, class_name)
# FIXME: monkeypatch to replace methods of class
Expand All @@ -28,9 +34,13 @@ def line_kernel(self, module_name, class_name, kernel_name="default"):
self.kernels[kernel_name] = class_()
self.retval = self.kernels[kernel_name]

def cell_kernel(self, kernel_name="default"):
@option(
'-k', '--kernel_name', action='store', default=None,
help='kernel name given to use for execution'
)
def cell_kx(self, kernel_name=None):
"""
%%kernel [NAME] - send the cell code to the kernel.
%%kx [-k NAME] - send the cell code to the kernel.
Returns the result of the execution as output.
Expand All @@ -39,11 +49,33 @@ def cell_kernel(self, kernel_name="default"):
%%kernel bash
ls -al
Use `%kernel MODULE CLASS [NAME]` to create a kernel.
Use `%kernel MODULE CLASS [-k NAME]` to create a kernel.
"""
if kernel_name is None:
kernel_name = self.kernel_name
self.retval = self.kernels[kernel_name].do_execute_direct(self.code)
self.evaluate = False

@option(
'-k', '--kernel_name', action='store', default=None,
help='kernel name given to use for execution'
)
def line_kx(self, code, kernel_name=None):
"""
%kx CODE [-k NAME] - send the code to the kernel.
Returns the result of the execution as output.
Example:
%kernel ls -al
Use `%kernel MODULE CLASS [-k NAME]` to create a kernel.
"""
if kernel_name is None:
kernel_name = self.kernel_name
self.retval = self.kernels[kernel_name].do_execute_direct(code)

def post_process(self, retval):
return self.retval

Expand Down
9 changes: 7 additions & 2 deletions jupyter_kernel/magics/parallel_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ def _clean_code(self, expr):
'-k', '--kernel_name', action='store', default=None,
help='kernel name given to use for execution'
)
def cell_px(self, kernel_name=None):
@option(
'-e', '--evaluate', action='store_true', default=False,
help=('evaluate code in the current kernel, too. The current ' +
'kernel should be of the same language as the cluster.')
)
def cell_px(self, kernel_name=None, evaluate=False):
"""
%%px - send cell to the cluster.
Expand All @@ -125,7 +130,7 @@ def cell_px(self, kernel_name=None):
kernel_name = self.kernel_name
self.retval = self.view["kernels['%s'].do_execute_direct(\"%s\")" % (
kernel_name, self._clean_code(self.code))]
self.evaluate = False
self.evaluate = evaluate

def post_process(self, retval):
return self.retval
Expand Down

0 comments on commit eeeb1c9

Please sign in to comment.