Example #1
0
    def _make_options(self, x):
        """Standardize the options list format.

        The returned list should be in the format [('label', value), ('label', value), ...].

        The input can be
        * a Mapping of labels to values
        * an iterable of values (of which at least one is not a list or tuple of length 2)
        * an iterable with entries that are lists or tuples of the form ('label', value)
        """
        # Return a list of key-value pairs where the keys are strings
        # If x is a dict, convert it to list format.
        if isinstance(x, Mapping):
            return [(unicode_type(k), v) for k, v in x.items()]

        # If any entry of x is not a list or tuple of length 2, convert
        # the entries to unicode for the labels.
        for y in x:
            if not (isinstance(y, (list, tuple)) and len(y) == 2):
                return [(unicode_type(i), i) for i in x]

        # x is already in the correct format: a list of 2-tuples.
        # The first element of each tuple should be unicode, this might
        # not yet be the case.
        return [(unicode_type(k), v) for k, v in x]
Example #2
0
def _make_options(x):
    """Standardize the options tuple format.

    The returned tuple should be in the format (('label', value), ('label', value), ...).

    The input can be
    * an iterable of (label, value) pairs
    * an iterable of values, and labels will be generated
    """
    # Check if x is a mapping of labels to values
    if isinstance(x, Mapping):
        import warnings

        warnings.warn(
            "Support for mapping types has been deprecated and will be dropped in a future release.",
            DeprecationWarning,
        )
        return tuple((unicode_type(k), v) for k, v in x.items())

    # only iterate once through the options.
    xlist = tuple(x)

    # Check if x is an iterable of (label, value) pairs
    if all((isinstance(i, (list, tuple)) and len(i) == 2) for i in xlist):
        return tuple((unicode_type(k), v) for k, v in xlist)

    # Otherwise, assume x is an iterable of values
    return tuple((unicode_type(i), i) for i in xlist)
Example #3
0
def new_text_cell(cell_type, source=None, rendered=None):
    """Create a new text cell."""
    cell = NotebookNode()
    if source is not None:
        cell.source = unicode_type(source)
    if rendered is not None:
        cell.rendered = unicode_type(rendered)
    cell.cell_type = cell_type
    return cell
Example #4
0
def new_text_cell(cell_type, source=None, rendered=None):
    """Create a new text cell."""
    cell = NotebookNode()
    if source is not None:
        cell.source = unicode_type(source)
    if rendered is not None:
        cell.rendered = unicode_type(rendered)
    cell.cell_type = cell_type
    return cell
Example #5
0
def wrap_exception(engine_info={}):
    etype, evalue, tb = sys.exc_info()
    stb = traceback.format_exception(etype, evalue, tb)
    exc_content = {
        'status' : 'error',
        'traceback' : stb,
        'ename' : unicode_type(etype.__name__),
        'evalue' : unicode_type(evalue),
        'engine_info' : engine_info
    }
    return exc_content
Example #6
0
def wrap_exception(engine_info={}):
    etype, evalue, tb = sys.exc_info()
    stb = traceback.format_exception(etype, evalue, tb)
    exc_content = {
        'status': 'error',
        'traceback': stb,
        'ename': unicode_type(etype.__name__),
        'evalue': unicode_type(evalue),
        'engine_info': engine_info
    }
    return exc_content
Example #7
0
def new_author(name=None, email=None, affiliation=None, url=None):
    """Create a new author."""
    author = NotebookNode()
    if name is not None:
        author.name = unicode_type(name)
    if email is not None:
        author.email = unicode_type(email)
    if affiliation is not None:
        author.affiliation = unicode_type(affiliation)
    if url is not None:
        author.url = unicode_type(url)
    return author
Example #8
0
def new_author(name=None, email=None, affiliation=None, url=None):
    """Create a new author."""
    author = NotebookNode()
    if name is not None:
        author.name = unicode_type(name)
    if email is not None:
        author.email = unicode_type(email)
    if affiliation is not None:
        author.affiliation = unicode_type(affiliation)
    if url is not None:
        author.url = unicode_type(url)
    return author
Example #9
0
    def _make_options(self, x):
        # Return a list of key-value pairs where the keys are strings
        # If x is a dict, convert it to list format.
        if isinstance(x, Mapping):
            return [(unicode_type(k), v) for k, v in x.items()]

        # If x is an ordinary list, use the option values as names.
        for y in x:
            if not isinstance(y, (list, tuple)) or len(y) < 2:
                return [(unicode_type(i), i) for i in x]

        # Value is already in the correct format.
        return x
    def _make_options(self, x):
        # Return a list of key-value pairs where the keys are strings
        # If x is a dict, convert it to list format.
        if isinstance(x, (OrderedDict, dict)):
            return [(unicode_type(k), v) for k, v in x.items()]

        # If x is an ordinary list, use the option values as names.
        for y in x:
            if not isinstance(y, (list, tuple)) or len(y) < 2:
                return [(unicode_type(i), i) for i in x]

        # Value is already in the correct format.
        return x
Example #11
0
def new_output(output_type=None,
               output_text=None,
               output_png=None,
               output_html=None,
               output_svg=None,
               output_latex=None,
               output_json=None,
               output_javascript=None,
               output_jpeg=None,
               prompt_number=None,
               etype=None,
               evalue=None,
               traceback=None):
    """Create a new code cell with input and output"""
    output = NotebookNode()
    if output_type is not None:
        output.output_type = unicode_type(output_type)

    if output_type != 'pyerr':
        if output_text is not None:
            output.text = unicode_type(output_text)
        if output_png is not None:
            output.png = bytes(output_png)
        if output_jpeg is not None:
            output.jpeg = bytes(output_jpeg)
        if output_html is not None:
            output.html = unicode_type(output_html)
        if output_svg is not None:
            output.svg = unicode_type(output_svg)
        if output_latex is not None:
            output.latex = unicode_type(output_latex)
        if output_json is not None:
            output.json = unicode_type(output_json)
        if output_javascript is not None:
            output.javascript = unicode_type(output_javascript)

    if output_type == u'pyout':
        if prompt_number is not None:
            output.prompt_number = int(prompt_number)

    if output_type == u'pyerr':
        if etype is not None:
            output.etype = unicode_type(etype)
        if evalue is not None:
            output.evalue = unicode_type(evalue)
        if traceback is not None:
            output.traceback = [
                unicode_type(frame) for frame in list(traceback)
            ]

    return output
Example #12
0
    def _make_options(self, x):
        # Return a list of key-value pairs where the keys are strings
        # If x is a dict, convert it to list format.
        if isinstance(x, Mapping):
            return [(unicode_type(k), v) for k, v in x.items()]

        # If x is an ordinary list, use the option values as names.
        for y in x:
            if not isinstance(y, (list, tuple)) or len(y) < 2:
                return [(unicode_type(i), i) for i in x]

        # x is already in the correct format: a list of 2-tuples.
        # The first element of each tuple should be unicode, this might
        # not yet be the case.
        return [(unicode_type(k), v) for k, v in x]
Example #13
0
    def _make_options(self, x):
        # Return a list of key-value pairs where the keys are strings
        # If x is a dict, convert it to list format.
        if isinstance(x, Mapping):
            return [(unicode_type(k), v) for k, v in x.items()]

        # If x is an ordinary list, use the option values as names.
        for y in x:
            if not isinstance(y, (list, tuple)) or len(y) < 2:
                return [(unicode_type(i), i) for i in x]

        # x is already in the correct format: a list of 2-tuples.
        # The first element of each tuple should be unicode, this might
        # not yet be the case.
        return [(unicode_type(k), v) for k, v in x]
    def start_kernel(self, kernel_name=None, **kwargs):
        """Start a new kernel.

        The caller can pick a kernel_id by passing one in as a keyword arg,
        otherwise one will be picked using a uuid.

        To silence the kernel's stdout/stderr, call this using::

            km.start_kernel(stdout=PIPE, stderr=PIPE)

        """
        kernel_id = kwargs.pop('kernel_id', unicode_type(uuid.uuid4()))
        if kernel_id in self:
            raise DuplicateKernelError('Kernel already exists: %s' % kernel_id)

        if kernel_name is None:
            kernel_name = self.default_kernel_name
        # kernel_manager_factory is the constructor for the KernelManager
        # subclass we are using. It can be configured as any Configurable,
        # including things like its transport and ip.
        kwargs = {}
        if self.kernel_spec_manager:
            kwargs['kernel_spec_manager'] = self.kernel_spec_manager
        km = self.kernel_manager_factory(connection_file=os.path.join(
                    self.connection_dir, "kernel-%s.json" % kernel_id),
                    parent=self, autorestart=True, log=self.log, kernel_name=kernel_name,
                    **kwargs
        )
        km.start_kernel(**kwargs)
        self._kernels[kernel_id] = km
        return kernel_id
def _widget_abbrev(o):
    """Make widgets from abbreviations: single values, lists or tuples."""
    float_or_int = (float, int)
    if isinstance(o, (list, tuple)):
        if o and all(isinstance(x, string_types) for x in o):
            return Dropdown(options=[unicode_type(k) for k in o])
        elif _matches(o, (float_or_int, float_or_int)):
            min, max, value = _get_min_max_value(o[0], o[1])
            if all(isinstance(_, int) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max)
        elif _matches(o, (float_or_int, float_or_int, float_or_int)):
            step = o[2]
            if step <= 0:
                raise ValueError("step must be >= 0, not %r" % step)
            min, max, value = _get_min_max_value(o[0], o[1], step=step)
            if all(isinstance(_, int) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max, step=step)
    else:
        return _widget_abbrev_single_value(o)
Example #16
0
 def writes(self, nb, **kwargs):
     lines = [u'# -*- coding: utf-8 -*-']
     lines.extend([u'# <nbformat>2</nbformat>',''])
     for ws in nb.worksheets:
         for cell in ws.cells:
             if cell.cell_type == u'code':
                 input = cell.get(u'input')
                 if input is not None:
                     lines.extend([u'# <codecell>',u''])
                     lines.extend(input.splitlines())
                     lines.append(u'')
             elif cell.cell_type == u'html':
                 input = cell.get(u'source')
                 if input is not None:
                     lines.extend([u'# <htmlcell>',u''])
                     lines.extend([u'# ' + line for line in input.splitlines()])
                     lines.append(u'')
             elif cell.cell_type == u'markdown':
                 input = cell.get(u'source')
                 if input is not None:
                     lines.extend([u'# <markdowncell>',u''])
                     lines.extend([u'# ' + line for line in input.splitlines()])
                     lines.append(u'')
     lines.append('')
     return unicode_type('\n'.join(lines))
Example #17
0
 def writes(self, nb, **kwargs):
     lines = [u'# -*- coding: utf-8 -*-']
     lines.extend([u'# <nbformat>2</nbformat>', ''])
     for ws in nb.worksheets:
         for cell in ws.cells:
             if cell.cell_type == u'code':
                 input = cell.get(u'input')
                 if input is not None:
                     lines.extend([u'# <codecell>', u''])
                     lines.extend(input.splitlines())
                     lines.append(u'')
             elif cell.cell_type == u'html':
                 input = cell.get(u'source')
                 if input is not None:
                     lines.extend([u'# <htmlcell>', u''])
                     lines.extend(
                         [u'# ' + line for line in input.splitlines()])
                     lines.append(u'')
             elif cell.cell_type == u'markdown':
                 input = cell.get(u'source')
                 if input is not None:
                     lines.extend([u'# <markdowncell>', u''])
                     lines.extend(
                         [u'# ' + line for line in input.splitlines()])
                     lines.append(u'')
     lines.append('')
     return unicode_type('\n'.join(lines))
    def new_kernel_id(self, **kwargs):
        """Override used to provide a mechanism by which clients can specify a kernel's id.  In this case
           that mechanism is via the per-kernel environment variable: KERNEL_ID.  If specified, its value
           will be validated and returned, otherwise the result from the superclass method is returned.

           NOTE: This method exists in jupyter_client.multikernelmanager.py for releases > 5.2.3.  If you
           find that this method is not getting invoked, then you likely need to update the version of
           jupyter_client.  The Enterprise Gateway dependency will be updated once new releases of
           jupyter_client are more prevalent.
        """
        kernel_id = None
        env = kwargs.get('env')
        if env and env.get('KERNEL_ID'):   # If there's a KERNEL_ID in the env, check it out
            # convert string back to UUID - validating string in the process.
            str_kernel_id = env.get('KERNEL_ID')
            try:
                str_v4_kernel_id = str(uuid.UUID(str_kernel_id, version=4))
                if str_kernel_id != str_v4_kernel_id:  # Given string is not uuid v4 compliant
                    raise ValueError("value is not uuid v4 compliant")
            except ValueError as ve:
                self.log.error("Invalid v4 UUID value detected in ['env']['KERNEL_ID']: '{}'!  Error: {}".
                               format(str_kernel_id, ve))
                raise ve
            # user-provided id is valid, use it
            kernel_id = unicode_type(str_kernel_id)
            self.log.debug("Using user-provided kernel_id: {}".format(kernel_id))
        else:
            kernel_id = super(RemoteMappingKernelManager, self).new_kernel_id(**kwargs)

        return kernel_id
Example #19
0
def test_embed_kernel_reentrant():
    """yap_ipython.embed_kernel() can be called multiple times"""
    cmd = '\n'.join([
        'from yap_ipython import embed_kernel',
        'count = 0',
        'def go():',
        '    global count',
        '    embed_kernel()',
        '    count = count + 1',
        '',
        'while True:'
        '    go()',
        '',
    ])

    with setup_kernel(cmd) as client:
        for i in range(5):
            msg_id = client.inspect('count')
            msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = msg['content']
            assert content['found']
            text = content['data']['text/plain']
            assert unicode_type(i) in text

            # exit from embed_kernel
            client.execute("get_ipython().exit_now = True")
            msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            time.sleep(0.2)
Example #20
0
def _widget_abbrev(o):
    """Make widgets from abbreviations: single values, lists or tuples."""
    if isinstance(o, list):
        return Dropdown(options=[unicode_type(k) for k in o])

    elif isinstance(o, tuple):
        if _matches(o, (Real, Real)):
            min, max, value = _get_min_max_value(o[0], o[1])
            if all(isinstance(_, Integral) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max)
        elif _matches(o, (Real, Real, Real)):
            step = o[2]
            if step <= 0:
                raise ValueError("step must be >= 0, not %r" % step)
            min, max, value = _get_min_max_value(o[0], o[1], step=step)
            if all(isinstance(_, Integral) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max, step=step)

    else:
        return _widget_abbrev_single_value(o)
Example #21
0
def test_embed_kernel_reentrant():
    """IPython.embed_kernel() can be called multiple times"""
    cmd = '\n'.join([
        'from IPython import embed_kernel',
        'count = 0',
        'def go():',
        '    global count',
        '    embed_kernel()',
        '    count = count + 1',
        '',
        'while True:'
        '    go()',
        '',
    ])

    with setup_kernel(cmd) as client:
        for i in range(5):
            msg_id = client.inspect('count')
            msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = msg['content']
            nt.assert_true(content['found'])
            text = content['data']['text/plain']
            nt.assert_in(unicode_type(i), text)

            # exit from embed_kernel
            client.execute("get_ipython().exit_now = True")
            msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            time.sleep(0.2)
Example #22
0
def new_text_cell(text=None):
    """Create a new text cell."""
    cell = NotebookNode()
    if text is not None:
        cell.text = unicode_type(text)
    cell.cell_type = u'text'
    return cell
    def start_kernel(self, kernel_name=None, **kwargs):
        """Start a new kernel.

        The caller can pick a kernel_id by passing one in as a keyword arg,
        otherwise one will be picked using a uuid.

        The kernel ID for the newly started kernel is returned.
        """
        kernel_id = kwargs.pop('kernel_id', unicode_type(uuid.uuid4()))
        if kernel_id in self:
            raise DuplicateKernelError('Kernel already exists: %s' % kernel_id)

        if kernel_name is None:
            kernel_name = self.default_kernel_name
        # kernel_manager_factory is the constructor for the KernelManager
        # subclass we are using. It can be configured as any Configurable,
        # including things like its transport and ip.
        constructor_kwargs = {}
        if self.kernel_spec_manager:
            constructor_kwargs[
                'kernel_spec_manager'] = self.kernel_spec_manager
        km = self.kernel_manager_factory(connection_file=os.path.join(
            self.connection_dir, "kernel-%s.json" % kernel_id),
                                         parent=self,
                                         log=self.log,
                                         kernel_name=kernel_name,
                                         **constructor_kwargs)
        km.start_kernel(**kwargs)
        self._kernels[kernel_id] = km
        return kernel_id
Example #24
0
 def to_work_dir(self):
     wd = self.work_dir
     if unicode_type(wd) != py3compat.getcwd():
         os.chdir(wd)
         self.log.info("Changing to working dir: %s" % wd)
     # This is the working dir by now.
     sys.path.insert(0, '')
Example #25
0
def new_text_cell(text=None):
    """Create a new text cell."""
    cell = NotebookNode()
    if text is not None:
        cell.text = unicode_type(text)
    cell.cell_type = u'text'
    return cell
Example #26
0
 def to_work_dir(self):
     wd = self.work_dir
     if unicode_type(wd) != py3compat.getcwd():
         os.chdir(wd)
         self.log.info("Changing to working dir: %s" % wd)
     # This is the working dir by now.
     sys.path.insert(0, '')
    def new_kernel_id(self, **kwargs):
        """Override used to provide a mechanism by which clients can specify a kernel's id.  In this case
           that mechanism is via the per-kernel environment variable: KERNEL_ID.  If specified, its value
           will be validated and returned, otherwise the result from the superclass method is returned.

           NOTE: This method exists in jupyter_client.multikernelmanager.py for releases > 5.2.3.  If you
           find that this method is not getting invoked, then you likely need to update the version of
           jupyter_client.  The Enterprise Gateway dependency will be updated once new releases of
           jupyter_client are more prevalent.
        """
        kernel_id = None
        env = kwargs.get('env')
        if env and env.get(
                'KERNEL_ID'
        ):  # If there's a KERNEL_ID in the env, check it out
            # convert string back to UUID - validating string in the process.
            str_kernel_id = env.get('KERNEL_ID')
            try:
                str_v4_kernel_id = str(uuid.UUID(str_kernel_id, version=4))
                if str_kernel_id != str_v4_kernel_id:  # Given string is not uuid v4 compliant
                    raise ValueError("value is not uuid v4 compliant")
            except ValueError as ve:
                self.log.error(
                    "Invalid v4 UUID value detected in ['env']['KERNEL_ID']: '{}'!  Error: {}"
                    .format(str_kernel_id, ve))
                raise ve
            # user-provided id is valid, use it
            kernel_id = unicode_type(str_kernel_id)
            self.log.debug(
                "Using user-provided kernel_id: {}".format(kernel_id))
        else:
            kernel_id = super(RemoteMappingKernelManager,
                              self).new_kernel_id(**kwargs)

        return kernel_id
Example #28
0
    def _showtraceback(self, etype, evalue, stb):
        # try to preserve ordering of tracebacks and print statements
        sys.stdout.flush()
        sys.stderr.flush()

        exc_content = {
            u'traceback': stb,
            u'ename': unicode_type(etype.__name__),
            u'evalue': py3compat.safe_unicode(evalue),
        }

        dh = self.displayhook
        # Send exception info over pub socket for other clients than the caller
        # to pick up
        topic = None
        if dh.topic:
            topic = dh.topic.replace(b'execute_result', b'error')

        exc_msg = dh.session.send(dh.pub_socket,
                                  u'error',
                                  json_clean(exc_content),
                                  dh.parent_header,
                                  ident=topic)

        # FIXME - Once we rely on Python 3, the traceback is stored on the
        # exception object, so we shouldn't need to store it here.
        self._last_traceback = stb
Example #29
0
    def _showtraceback(self, etype, evalue, stb):
        # try to preserve ordering of tracebacks and print statements
        sys.stdout.flush()
        sys.stderr.flush()

        exc_content = {
            u'traceback' : stb,
            u'ename' : unicode_type(etype.__name__),
            u'evalue' : py3compat.safe_unicode(evalue),
        }

        dh = self.displayhook
        # Send exception info over pub socket for other clients than the caller
        # to pick up
        topic = None
        if dh.topic:
            topic = dh.topic.replace(b'execute_result', b'error')

        exc_msg = dh.session.send(dh.pub_socket, u'error', json_clean(exc_content), dh.parent_header, ident=topic)

        # FIXME - Hack: store exception info in shell object.  Right now, the
        # caller is reading this info after the fact, we need to fix this logic
        # to remove this hack.  Even uglier, we need to store the error status
        # here, because in the main loop, the logic that sets it is being
        # skipped because runlines swallows the exceptions.
        exc_content[u'status'] = u'error'
        self._reply_content = exc_content
        # /FIXME

        return exc_content
Example #30
0
    def do_execute(self, code, silent, store_history=True,
                   user_expressions=None, allow_stdin=False):
        shell = self.shell # we'll need this a lot here

        self._forward_input(allow_stdin)

        reply_content = {}
        try:
            res = shell.run_cell(code, store_history=store_history, silent=silent)
        finally:
            self._restore_input()

        if res.error_before_exec is not None:
            err = res.error_before_exec
        else:
            err = res.error_in_exec

        if res.success:
            reply_content[u'status'] = u'ok'
        else:
            reply_content[u'status'] = u'error'

            reply_content.update({
                u'traceback': shell._last_traceback or [],
                u'ename': unicode_type(type(err).__name__),
                u'evalue': safe_unicode(err),
            })

            # FIXME: deprecated piece for ipyparallel (remove in 5.0):
            e_info = dict(engine_uuid=self.ident, engine_id=self.int_id,
                          method='execute')
            reply_content['engine_info'] = e_info


        # Return the execution counter so clients can display prompts
        reply_content['execution_count'] = shell.execution_count - 1

        if 'traceback' in reply_content:
            self.log.info("Exception in execute request:\n%s", '\n'.join(reply_content['traceback']))


        # At this point, we can tell whether the main code execution succeeded
        # or not.  If it did, we proceed to evaluate user_expressions
        if reply_content['status'] == 'ok':
            reply_content[u'user_expressions'] = \
                         shell.user_expressions(user_expressions or {})
        else:
            # If there was an error, don't even try to compute expressions
            reply_content[u'user_expressions'] = {}

        # Payloads should be retrieved regardless of outcome, so we can both
        # recover partial output (that could have been generated early in a
        # block, before an error) and always clear the payload system.
        reply_content[u'payload'] = shell.payload_manager.read_payload()
        # Be aggressive about clearing the payload because we don't want
        # it to sit in memory until the next execute_request comes in.
        shell.payload_manager.clear_payload()

        return reply_content
Example #31
0
    def do_execute(self, code, silent, store_history=True,
                   user_expressions=None, allow_stdin=False):
        shell = self.shell # we'll need this a lot here

        self._forward_input(allow_stdin)

        reply_content = {}
        try:
            res = self.engine.run_cell(code, store_history=store_history, silent=silent)
        finally:
            self._restore_input()

        if res.error_before_exec is not None:
            err = res.error_before_exec
        else:
            err = res.error_in_exec

        if res.success:
            reply_content[u'status'] = u'ok'
        else:
            reply_content[u'status'] = u'error'

            reply_content.update({
                u'traceback': shell._last_traceback or [],
                u'ename': unicode_type(type(err).__name__),
                u'evalue': safe_unicode(err),
            })

            # FIXME: deprecated piece for ipyparallel (remove in 5.0):
            e_info = dict(engine_uuid=self.ident, engine_id=self.int_id,
                          method='execute')
            reply_content['engine_info'] = e_info


        # Return the execution counter so clients can display prompts
        reply_content['execution_count'] = shell.execution_count - 1

        if 'traceback' in reply_content:
            self.log.info("Exception in execute request:\n%s", '\n'.join(reply_content['traceback']))


        # At this point, we can tell whether the main code execution succeeded
        # or not.  If it did, we proceed to evaluate user_expressions
        if reply_content['status'] == 'ok':
            reply_content[u'user_expressions'] = \
                         shell.user_expressions(user_expressions or {})
        else:
            # If there was an error, don't even try to compute expressions
            reply_content[u'user_expressions'] = {}

        # Payloads should be retrieved regardless of outcome, so we can both
        # recover partial output (that could have been generated early in a
        # block, before an error) and always clear the payload system.
        reply_content[u'payload'] = shell.payload_manager.read_payload()
        # Be aggressive about clearing the payload because we don't want
        # it to sit in memory until the next execute_request comes in.
        shell.payload_manager.clear_payload()

        return reply_content
Example #32
0
 def _get_edited_history(self, index):
     """ Retrieves a history item, possibly with temporary edits.
     """
     if index in self._history_edits:
         return self._history_edits[index]
     elif index == len(self._history):
         return unicode_type()
     return self._history[index]
 def new_kernel_id(self, **kwargs):
     """
     Returns the id to associate with the kernel for this request. Subclasses may override
     this method to substitute other sources of kernel ids.
     :param kwargs:
     :return: string-ized version 4 uuid
     """
     return unicode_type(uuid.uuid4())
Example #34
0
def new_metadata(name=None, authors=None, license=None, created=None,
    modified=None, gistid=None):
    """Create a new metadata node."""
    metadata = NotebookNode()
    if name is not None:
        metadata.name = unicode_type(name)
    if authors is not None:
        metadata.authors = list(authors)
    if created is not None:
        metadata.created = unicode_type(created)
    if modified is not None:
        metadata.modified = unicode_type(modified)
    if license is not None:
        metadata.license = unicode_type(license)
    if gistid is not None:
        metadata.gistid = unicode_type(gistid)
    return metadata
 def new_kernel_id(self, **kwargs):
     """
     Returns the id to associate with the kernel for this request. Subclasses may override
     this method to substitute other sources of kernel ids.
     :param kwargs:
     :return: string-ized version 4 uuid
     """
     return unicode_type(uuid.uuid4())
Example #36
0
def new_code_cell(code=None, prompt_number=None):
    """Create a new code cell with input and output"""
    cell = NotebookNode()
    cell.cell_type = u'code'
    if code is not None:
        cell.code = unicode_type(code)
    if prompt_number is not None:
        cell.prompt_number = int(prompt_number)
    return cell
Example #37
0
def new_code_cell(code=None, prompt_number=None):
    """Create a new code cell with input and output"""
    cell = NotebookNode()
    cell.cell_type = u'code'
    if code is not None:
        cell.code = unicode_type(code)
    if prompt_number is not None:
        cell.prompt_number = int(prompt_number)
    return cell
Example #38
0
def new_worksheet(name=None, cells=None):
    """Create a worksheet by name with with a list of cells."""
    ws = NotebookNode()
    if name is not None:
        ws.name = unicode_type(name)
    if cells is None:
        ws.cells = []
    else:
        ws.cells = list(cells)
    return ws
Example #39
0
def new_code_cell(input=None, prompt_number=None, outputs=None,
    language=u'python', collapsed=False):
    """Create a new code cell with input and output"""
    cell = NotebookNode()
    cell.cell_type = u'code'
    if language is not None:
        cell.language = unicode_type(language)
    if input is not None:
        cell.input = unicode_type(input)
    if prompt_number is not None:
        cell.prompt_number = int(prompt_number)
    if outputs is None:
        cell.outputs = []
    else:
        cell.outputs = outputs
    if collapsed is not None:
        cell.collapsed = bool(collapsed)

    return cell
Example #40
0
 def test_execute_displaypub(self):
     """execute tracks display_pub output"""
     view = self.client[:]
     view.execute("from IPython.core.display import *")
     ar = view.execute("[ display(i) for i in range(5) ]", block=True)
     
     expected = [ {u'text/plain' : unicode_type(j)} for j in range(5) ]
     for outputs in ar.outputs:
         mimes = [ out['data'] for out in outputs ]
         self.assertEqual(mimes, expected)
Example #41
0
def _widget_abbrev(o):
    """Make widgets from abbreviations: single values, lists or tuples."""
    if isinstance(o, list):
        # --------------------------------------------------------------------
        # Handle deprecated behavior of using lists of length 2 or 3 in place
        # of tuples to specify slider widget attributes. This will be removed
        # in ipywidgets 6.0.
        if len(o) in [2, 3] and all(isinstance(x, Real) for x in o):
            warn("For Sliders, use a tuple: %s" % (tuple(o), ),
                 DeprecationWarning)
            return _widget_abbrev(tuple(o))
        # --------------------------------------------------------------------
        return Dropdown(options=[unicode_type(k) for k in o])

    elif isinstance(o, tuple):
        # --------------------------------------------------------------------
        # Handle deprecated behavior of using tuples for selection widget. This
        # will be removed in ipywidgets 6.0.
        if any(not isinstance(x, Real) for x in o):
            warn("For Selection widgets, use a list %s" % (list(o), ),
                 DeprecationWarning)
            return Dropdown(options=[unicode_type(k) for k in o])
        # --------------------------------------------------------------------
        if _matches(o, (Real, Real)):
            min, max, value = _get_min_max_value(o[0], o[1])
            if all(isinstance(_, Integral) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max)
        elif _matches(o, (Real, Real, Real)):
            step = o[2]
            if step <= 0:
                raise ValueError("step must be >= 0, not %r" % step)
            min, max, value = _get_min_max_value(o[0], o[1], step=step)
            if all(isinstance(_, Integral) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max, step=step)

    else:
        return _widget_abbrev_single_value(o)
Example #42
0
    def test_execute_displaypub(self):
        """execute tracks display_pub output"""
        view = self.client[:]
        view.execute("from IPython.core.display import *")
        ar = view.execute("[ display(i) for i in range(5) ]", block=True)

        expected = [{u'text/plain': unicode_type(j)} for j in range(5)]
        for outputs in ar.outputs:
            mimes = [out['data'] for out in outputs]
            self.assertEqual(mimes, expected)
Example #43
0
def new_worksheet(name=None, cells=None):
    """Create a worksheet by name with with a list of cells."""
    ws = NotebookNode()
    if name is not None:
        ws.name = unicode_type(name)
    if cells is None:
        ws.cells = []
    else:
        ws.cells = list(cells)
    return ws
Example #44
0
    def do_apply(self, content, bufs, msg_id, reply_metadata):
        from .serialize import serialize_object, unpack_apply_message
        shell = self.shell
        try:
            working = shell.user_ns

            prefix = "_"+str(msg_id).replace("-","")+"_"

            f,args,kwargs = unpack_apply_message(bufs, working, copy=False)

            fname = getattr(f, '__name__', 'f')

            fname = prefix+"f"
            argname = prefix+"args"
            kwargname = prefix+"kwargs"
            resultname = prefix+"result"

            ns = { fname : f, argname : args, kwargname : kwargs , resultname : None }
            # print ns
            working.update(ns)
            code = "%s = %s(*%s,**%s)" % (resultname, fname, argname, kwargname)
            try:
                exec(code, shell.user_global_ns, shell.user_ns)
                result = working.get(resultname)
            finally:
                for key in ns:
                    working.pop(key)

            result_buf = serialize_object(result,
                buffer_threshold=self.session.buffer_threshold,
                item_threshold=self.session.item_threshold,
            )

        except BaseException as e:
            # invoke IPython traceback formatting
            shell.showtraceback()
            reply_content = {
                u'traceback': shell._last_traceback or [],
                u'ename': unicode_type(type(e).__name__),
                u'evalue': safe_unicode(e),
            }
            # FIXME: deprecated piece for ipyparallel (remove in 5.0):
            e_info = dict(engine_uuid=self.ident, engine_id=self.int_id, method='apply')
            reply_content['engine_info'] = e_info

            self.send_response(self.iopub_socket, u'error', reply_content,
                                ident=self._topic('error'))
            self.log.info("Exception in apply request:\n%s", '\n'.join(reply_content['traceback']))
            result_buf = []
            reply_content['status'] = 'error'
        else:
            reply_content = {'status' : 'ok'}

        return reply_content, result_buf
Example #45
0
    def do_apply(self, content, bufs, msg_id, reply_metadata):
        from .serialize import serialize_object, unpack_apply_message
        shell = self.shell
        try:
            working = shell.user_ns

            prefix = "_"+str(msg_id).replace("-","")+"_"

            f,args,kwargs = unpack_apply_message(bufs, working, copy=False)

            fname = getattr(f, '__name__', 'f')

            fname = prefix+"f"
            argname = prefix+"args"
            kwargname = prefix+"kwargs"
            resultname = prefix+"result"

            ns = { fname : f, argname : args, kwargname : kwargs , resultname : None }
            # print ns
            working.update(ns)
            code = "%s = %s(*%s,**%s)" % (resultname, fname, argname, kwargname)
            try:
                exec(code, shell.user_global_ns, shell.user_ns)
                result = working.get(resultname)
            finally:
                for key in ns:
                    working.pop(key)

            result_buf = serialize_object(result,
                buffer_threshold=self.session.buffer_threshold,
                item_threshold=self.session.item_threshold,
            )

        except BaseException as e:
            # invoke IPython traceback formatting
            shell.showtraceback()
            reply_content = {
                u'traceback': shell._last_traceback or [],
                u'ename': unicode_type(type(e).__name__),
                u'evalue': safe_unicode(e),
            }
            # FIXME: deprecated piece for ipyparallel (remove in 5.0):
            e_info = dict(engine_uuid=self.ident, engine_id=self.int_id, method='apply')
            reply_content['engine_info'] = e_info

            self.send_response(self.iopub_socket, u'error', reply_content,
                                ident=self._topic('error'))
            self.log.info("Exception in apply request:\n%s", '\n'.join(reply_content['traceback']))
            result_buf = []
            reply_content['status'] = 'error'
        else:
            reply_content = {'status' : 'ok'}

        return reply_content, result_buf
Example #46
0
def _make_options(x):
    """Standardize the options list format.

    The returned list should be in the format [('label', value), ('label', value), ...].

    The input can be
    * a Mapping of labels to values
    * an iterable of (label, value) pairs
    * an iterable of values, and labels will be generated
    """
    # Check if x is a mapping of labels to values
    if isinstance(x, Mapping):
        return tuple((unicode_type(k), v) for k, v in x.items())

    # Check if x is an iterable of (label, value) pairs
    if all((isinstance(i, (list, tuple)) and len(i) == 2) for i in x):
        return tuple((unicode_type(k), v) for k, v in x)

    # Otherwise, assume x is an iterable of values
    return tuple((unicode_type(i), i) for i in x)
Example #47
0
def _make_options(x):
    """Standardize the options list format.

    The returned list should be in the format [('label', value), ('label', value), ...].

    The input can be
    * a Mapping of labels to values
    * an iterable of (label, value) pairs
    * an iterable of values, and labels will be generated
    """
    # Check if x is a mapping of labels to values
    if isinstance(x, Mapping):
        return tuple((unicode_type(k), v) for k, v in x.items())

    # Check if x is an iterable of (label, value) pairs
    if all((isinstance(i, (list, tuple)) and len(i) == 2) for i in x):
        return tuple((unicode_type(k), v) for k, v in x)

    # Otherwise, assume x is an iterable of values
    return tuple((unicode_type(i), i) for i in x)
Example #48
0
def new_kernel_id(**kwargs):
    """
    This method provides a mechanism by which clients can specify a kernel's id.  In this case
    that mechanism is via the per-kernel environment variable: KERNEL_ID.  If specified, its value
    will be validated and returned, otherwise the result from the provided method is returned.

    NOTE: This method exists in jupyter_client.multikernelmanager.py for releases > 5.2.3.  If you
    find that this method is not getting invoked, then you likely need to update the version of
    jupyter_client.  The Enterprise Gateway dependency will be updated once new releases of
    jupyter_client are more prevalent.

    Returns
    -------
    kernel_id : str
        The uuid string to associate with the new kernel
    """
    log = kwargs.pop("log", None) or traitlets_log.get_logger()
    kernel_id_fn = kwargs.pop("kernel_id_fn",
                              None) or (lambda: unicode_type(uuid.uuid4()))

    env = kwargs.get('env')
    if env and env.get(
            'KERNEL_ID'):  # If there's a KERNEL_ID in the env, check it out
        # convert string back to UUID - validating string in the process.
        str_kernel_id = env.get('KERNEL_ID')
        try:
            str_v4_kernel_id = str(uuid.UUID(str_kernel_id, version=4))
            if str_kernel_id != str_v4_kernel_id:  # Given string is not uuid v4 compliant
                raise ValueError("value is not uuid v4 compliant")
        except ValueError as ve:
            log.error(
                "Invalid v4 UUID value detected in ['env']['KERNEL_ID']: '{}'!  Error: {}"
                .format(str_kernel_id, ve))
            raise ve
        # user-provided id is valid, use it
        kernel_id = unicode_type(str_kernel_id)
        log.debug("Using user-provided kernel_id: {}".format(kernel_id))
    else:
        kernel_id = kernel_id_fn(**kwargs)

    return kernel_id
def _widget_abbrev(o):
    """Make widgets from abbreviations: single values, lists or tuples."""
    if isinstance(o, list):
        # --------------------------------------------------------------------
        # Handle deprecated behavior of using lists of length 2 or 3 in place
        # of tuples to specify slider widget attributes. This will be removed
        # in ipywidgets 6.0.
        if len(o) in [2, 3] and all(isinstance(x, Real) for x in o):
            warn("For Sliders, use a tuple: %s" % (tuple(o),), DeprecationWarning)
            return _widget_abbrev(tuple(o))
        # --------------------------------------------------------------------
        return Dropdown(options=[unicode_type(k) for k in o])

    elif isinstance(o, tuple):
        # --------------------------------------------------------------------
        # Handle deprecated behavior of using tuples for selection widget. This
        # will be removed in ipywidgets 6.0.
        if any(not isinstance(x, Real) for x in o):
            warn("For Selection widgets, use a list %s" %(list(o),), DeprecationWarning)
            return Dropdown(options=[unicode_type(k) for k in o])
        # --------------------------------------------------------------------
        if _matches(o, (Real, Real)):
            min, max, value = _get_min_max_value(o[0], o[1])
            if all(isinstance(_, Integral) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max)
        elif _matches(o, (Real, Real, Real)):
            step = o[2]
            if step <= 0:
                raise ValueError("step must be >= 0, not %r" % step)
            min, max, value = _get_min_max_value(o[0], o[1], step=step)
            if all(isinstance(_, Integral) for _ in o):
                cls = IntSlider
            else:
                cls = FloatSlider
            return cls(value=value, min=min, max=max, step=step)

    else:
        return _widget_abbrev_single_value(o)
Example #50
0
def new_metadata(name=None,
                 authors=None,
                 license=None,
                 created=None,
                 modified=None,
                 gistid=None):
    """Create a new metadata node."""
    metadata = NotebookNode()
    if name is not None:
        metadata.name = unicode_type(name)
    if authors is not None:
        metadata.authors = list(authors)
    if created is not None:
        metadata.created = unicode_type(created)
    if modified is not None:
        metadata.modified = unicode_type(modified)
    if license is not None:
        metadata.license = unicode_type(license)
    if gistid is not None:
        metadata.gistid = unicode_type(gistid)
    return metadata
Example #51
0
def new_output(output_type=None, output_text=None, output_png=None,
    output_html=None, output_svg=None, output_latex=None, output_json=None,
    output_javascript=None, output_jpeg=None, prompt_number=None,
    etype=None, evalue=None, traceback=None):
    """Create a new code cell with input and output"""
    output = NotebookNode()
    if output_type is not None:
        output.output_type = unicode_type(output_type)

    if output_type != 'pyerr':
        if output_text is not None:
            output.text = unicode_type(output_text)
        if output_png is not None:
            output.png = bytes(output_png)
        if output_jpeg is not None:
            output.jpeg = bytes(output_jpeg)
        if output_html is not None:
            output.html = unicode_type(output_html)
        if output_svg is not None:
            output.svg = unicode_type(output_svg)
        if output_latex is not None:
            output.latex = unicode_type(output_latex)
        if output_json is not None:
            output.json = unicode_type(output_json)
        if output_javascript is not None:
            output.javascript = unicode_type(output_javascript)

    if output_type == u'pyout':
        if prompt_number is not None:
            output.prompt_number = int(prompt_number)

    if output_type == u'pyerr':
        if etype is not None:
            output.etype = unicode_type(etype)
        if evalue is not None:
            output.evalue = unicode_type(evalue)
        if traceback is not None:
            output.traceback = [unicode_type(frame) for frame in list(traceback)]

    return output
Example #52
0
def new_code_cell(input=None,
                  prompt_number=None,
                  outputs=None,
                  language=u'python',
                  collapsed=False):
    """Create a new code cell with input and output"""
    cell = NotebookNode()
    cell.cell_type = u'code'
    if language is not None:
        cell.language = unicode_type(language)
    if input is not None:
        cell.input = unicode_type(input)
    if prompt_number is not None:
        cell.prompt_number = int(prompt_number)
    if outputs is None:
        cell.outputs = []
    else:
        cell.outputs = outputs
    if collapsed is not None:
        cell.collapsed = bool(collapsed)

    return cell
Example #53
0
 def widget_from_single_value(o):
     """Make widgets from single values, which can be used as parameter defaults."""
     if isinstance(o, string_types):
         return Text(value=unicode_type(o))
     elif isinstance(o, bool):
         return Checkbox(value=o)
     elif isinstance(o, Integral):
         min, max, value = _get_min_max_value(None, None, o)
         return IntSlider(value=o, min=min, max=max)
     elif isinstance(o, Real):
         min, max, value = _get_min_max_value(None, None, o)
         return FloatSlider(value=o, min=min, max=max)
     else:
         return None
Example #54
0
    def get_title(self, index):
        """Gets the title of a container pages.

        Parameters
        ----------
        index : int
            Index of the container page
        """
        # JSON dictionaries have string keys, so we convert index to a string
        index = unicode_type(int(index))
        if index in self._titles:
            return self._titles[index]
        else:
            return None
    def set_title(self, index, title):
        """Sets the title of a container page.

        Parameters
        ----------
        index : int
            Index of the container page
        title : unicode
            New title
        """
        # JSON dictionaries have string keys, so we convert index to a string
        index = unicode_type(int(index))
        self._titles[index] = title
        self.send_state('_titles')
Example #56
0
 def test_apply_displaypub(self):
     """apply tracks display_pub output"""
     view = self.client[:]
     view.execute("from IPython.core.display import *")
     
     @interactive
     def publish():
         [ display(i) for i in range(5) ]
     
     ar = view.apply_async(publish)
     ar.get(5)
     expected = [ {u'text/plain' : unicode_type(j)} for j in range(5) ]
     for outputs in ar.outputs:
         mimes = [ out['data'] for out in outputs ]
         self.assertEqual(mimes, expected)
Example #57
0
def _make_options(x):
    """Standardize the options tuple format.

    The returned tuple should be in the format (('label', value), ('label', value), ...).

    The input can be
    * an iterable of (label, value) pairs
    * an iterable of values, and labels will be generated
    """
    # Check if x is a mapping of labels to values
    if isinstance(x, Mapping):
        import warnings
        warnings.warn("Support for mapping types has been deprecated and will be dropped in a future release.", DeprecationWarning)
        return tuple((unicode_type(k), v) for k, v in x.items())

    # only iterate once through the options.
    xlist = tuple(x)

    # Check if x is an iterable of (label, value) pairs
    if all((isinstance(i, (list, tuple)) and len(i) == 2) for i in xlist):
        return tuple((unicode_type(k), v) for k, v in xlist)

    # Otherwise, assume x is an iterable of values
    return tuple((unicode_type(i), i) for i in xlist)