Esempio n. 1
0
def test_dict_key_completion_unicode_py2():
    """Test handling of unicode in dict key completion"""
    ip = get_ipython()
    complete = ip.Completer.complete

    ip.user_ns['d'] = {u'abc': None,
                       unicode_type('a\xd7\x90', 'utf8'): None}

    _, matches = complete(line_buffer="d[")
    nt.assert_in("u'abc']", matches)
    nt.assert_in("u'a\\u05d0']", matches)

    _, matches = complete(line_buffer="d['a")
    nt.assert_in("abc']", matches)
    nt.assert_not_in("a\\u05d0']", matches)

    _, matches = complete(line_buffer="d[u'a")
    nt.assert_in("abc']", matches)
    nt.assert_in("a\\u05d0']", matches)

    _, matches = complete(line_buffer="d[U'a")
    nt.assert_in("abc']", matches)
    nt.assert_in("a\\u05d0']", matches)

    # query using escape
    _, matches = complete(line_buffer="d[u'a\\u05d0")
    nt.assert_in("u05d0']", matches)  # tokenized after \\

    # query using character
    _, matches = complete(line_buffer=unicode_type("d[u'a\xd7\x90", 'utf8'))
    nt.assert_in("']", matches)
Esempio n. 2
0
 def test_lazy_eval_float(self):
     f = 0.503
     lz = LazyEvaluate(lambda : f)
     
     self.assertEqual(str(lz), str(f))
     self.assertEqual(unicode_type(lz), unicode_type(f))
     self.assertEqual(format(lz), str(f))
     self.assertEqual(format(lz, '.1'), '0.5')
Esempio n. 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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
0
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(values=[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)
Esempio n. 7
0
def reads(s, format, **kwargs):
    """Read a notebook from a string and return the NotebookNode object.

    This function properly handles notebooks of any version. The notebook
    returned will always be in the current version's format.

    Parameters
    ----------
    s : unicode
        The raw unicode string to read the notebook from.
    format : (u'json', u'ipynb', u'py')
        The format that the string is in.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.
    """
    format = unicode_type(format)
    if format == u'json' or format == u'ipynb':
        return reads_json(s, **kwargs)
    elif format == u'py':
        return reads_py(s, **kwargs)
    else:
        raise NBFormatError('Unsupported format: %s' % format)
Esempio n. 8
0
    def __init__(self, data=None, url=None, filename=None):
        """Create a display object given raw data.

        When this object is returned by an expression or passed to the
        display function, it will result in the data being displayed
        in the frontend. The MIME type of the data should match the
        subclasses used, so the Png subclass should be used for 'image/png'
        data. If the data is a URL, the data will first be downloaded
        and then displayed. If

        Parameters
        ----------
        data : unicode, str or bytes
            The raw data or a URL or file to load the data from
        url : unicode
            A URL to download the data from.
        filename : unicode
            Path to a local file to load the data from.
        """
        if data is not None and isinstance(data, string_types):
            if data.startswith('http') and url is None:
                url = data
                filename = None
                data = None
            elif _safe_exists(data) and filename is None:
                url = None
                filename = data
                data = None

        self.data = data
        self.url = url
        self.filename = None if filename is None else unicode_type(filename)

        self.reload()
        self._check_data()
Esempio n. 9
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, '')
Esempio n. 10
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
Esempio n. 11
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)
Esempio n. 12
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))
Esempio n. 13
0
    def __init__(self, data=None, url=None, filename=None):
        """Create a display object given raw data.

        When this object is returned by an expression or passed to the
        display function, it will result in the data being displayed
        in the frontend. The MIME type of the data should match the
        subclasses used, so the Png subclass should be used for 'image/png'
        data. If the data is a URL, the data will first be downloaded
        and then displayed. If

        Parameters
        ----------
        data : unicode, str or bytes
            The raw data or a URL or file to load the data from
        url : unicode
            A URL to download the data from.
        filename : unicode
            Path to a local file to load the data from.
        """
        if data is not None and isinstance(data, string_types):
            if data.startswith('http') and url is None:
                url = data
                filename = None
                data = None
            elif _safe_exists(data) and filename is None:
                url = None
                filename = data
                data = None

        self.data = data
        self.url = url
        self.filename = None if filename is None else unicode_type(filename)

        self.reload()
        self._check_data()
Esempio n. 14
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))
Esempio n. 15
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
Esempio n. 16
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
Esempio n. 17
0
    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.
        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,
        )
        km.start_kernel(**kwargs)
        self._kernels[kernel_id] = km
        return kernel_id
Esempio n. 18
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)
Esempio n. 19
0
    def start_kernel(self, **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)
        # 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.
        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,
        )
        km.start_kernel(**kwargs)
        self._kernels[kernel_id] = km
        return kernel_id
Esempio n. 20
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, '')
Esempio n. 21
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'pyout', b'pyerr')
        
        exc_msg = dh.session.send(dh.pub_socket, u'pyerr', 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
Esempio n. 22
0
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 DropdownWidget(values=[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 = IntSliderWidget
            else:
                cls = FloatSliderWidget
            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 = IntSliderWidget
            else:
                cls = FloatSliderWidget
            return cls(value=value, min=min, max=max, step=step)
    else:
        return _widget_abbrev_single_value(o)
Esempio n. 23
0
def reads(s, format, **kwargs):
    """Read a notebook from a string and return the NotebookNode object.

    This function properly handles notebooks of any version. The notebook
    returned will always be in the current version's format.

    Parameters
    ----------
    s : unicode
        The raw unicode string to read the notebook from.
    format : (u'json', u'ipynb', u'py')
        The format that the string is in.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.
    """
    format = unicode_type(format)
    if format == u'json' or format == u'ipynb':
        return reads_json(s, **kwargs)
    elif format == u'py':
        return reads_py(s, **kwargs)
    else:
        raise NBFormatError('Unsupported format: %s' % format)
Esempio n. 24
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.object_info('count')
            msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = msg['content']
            nt.assert_true(content['found'])
            nt.assert_equal(content['string_form'], unicode_type(i))
            
            # exit from embed_kernel
            client.execute("get_ipython().exit_now = True")
            msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            time.sleep(0.2)
Esempio n. 25
0
    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.
        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,
        )
        # FIXME: remove special treatment of IPython kernels
        if km.ipython_kernel:
            kwargs.setdefault('extra_arguments', self.ipython_kernel_argv)
        km.start_kernel(**kwargs)
        self._kernels[kernel_id] = km
        return kernel_id
Esempio n. 26
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]
Esempio n. 27
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 _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]
Esempio n. 29
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
Esempio n. 30
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
Esempio n. 31
0
 def __init__(self, *args, **kwargs):
     self.value_lock = Lock()
     self._in_values_changed = False
     if 'values' in kwargs:
         values = kwargs['values']
         # convert list values to an dict of {str(v):v}
         if isinstance(values, list):
             # preserve list order with an OrderedDict
             kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values)
     DOMWidget.__init__(self, *args, **kwargs)
Esempio n. 32
0
 def __init__(self, *args, **kwargs):
     self.value_lock = Lock()
     self._in_values_changed = False
     if 'values' in kwargs:
         values = kwargs['values']
         # convert list values to an dict of {str(v):v}
         if isinstance(values, list):
             # preserve list order with an OrderedDict
             kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values)
     DOMWidget.__init__(self, *args, **kwargs)
Esempio n. 33
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
Esempio n. 34
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)
Esempio n. 35
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
Esempio n. 36
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)
Esempio n. 37
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
Esempio n. 38
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
Esempio n. 39
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
Esempio n. 40
0
def new_output(output_type, 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,
    ename=None, evalue=None, traceback=None, stream=None, metadata=None):
    """Create a new output, to go in the ``cell.outputs`` list of a code cell.
    """
    output = NotebookNode()
    output.output_type = unicode_type(output_type)

    if metadata is None:
        metadata = {}
    if not isinstance(metadata, dict):
        raise TypeError("metadata must be dict")


    if output_type in {u'pyout', 'display_data'}:
        output.metadata = metadata

    if output_type != 'pyerr':
        if output_text is not None:
            output.text = cast_unicode(output_text)
        if output_png is not None:
            output.png = cast_unicode(output_png)
        if output_jpeg is not None:
            output.jpeg = cast_unicode(output_jpeg)
        if output_html is not None:
            output.html = cast_unicode(output_html)
        if output_svg is not None:
            output.svg = cast_unicode(output_svg)
        if output_latex is not None:
            output.latex = cast_unicode(output_latex)
        if output_json is not None:
            output.json = cast_unicode(output_json)
        if output_javascript is not None:
            output.javascript = cast_unicode(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 ename is not None:
            output.ename = cast_unicode(ename)
        if evalue is not None:
            output.evalue = cast_unicode(evalue)
        if traceback is not None:
            output.traceback = [cast_unicode(frame) for frame in list(traceback)]

    if output_type == u'stream':
        output.stream = 'stdout' if stream is None else cast_unicode(stream)
    
    return output
Esempio n. 41
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
Esempio n. 42
0
    def julia(self, line, cell=None):
        """
        Execute code in Julia, and pull some of the results back into the
        Python namespace.
        """
        src = compat.unicode_type(line if cell is None else cell)

        try:
            ans = self._julia.eval(src)
        except JuliaError as e:
            print(e, file=sys.stderr)
            ans = None

        return ans
Esempio n. 43
0
    def julia(self, line, cell=None):
        """
        Execute code in Julia, and pull some of the results back into the
        Python namespace.
        """
        src = compat.unicode_type(line if cell is None else cell)

        try:
            ans = self.julia.eval(src)
        except JuliaError as e:
            print(e.message, file=sys.stderr)
            ans = None

        return ans
Esempio n. 44
0
 def __init__(self, *args, **kwargs):
     self.value_lock = Lock()
     self._in_values_changed = False
     if 'values' in kwargs:
         values = kwargs['values']
         # convert list values to an dict of {str(v):v}
         if isinstance(values, list):
             # preserve list order with an OrderedDict
             kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values)
         # python3.3 turned on hash randomization by default - this means that sometimes, randomly
         # we try to set value before setting values, due to dictionary ordering.  To fix this, force
         # the setting of self.values right now, before anything else runs
         self.values = kwargs.pop('values')
     DOMWidget.__init__(self, *args, **kwargs)
Esempio n. 45
0
def new_output(output_type, 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,
    ename=None, evalue=None, traceback=None, stream=None, metadata=None):
    """Create a new output, to go in the ``cell.outputs`` list of a code cell.
    """
    output = NotebookNode()
    output.output_type = unicode_type(output_type)

    if metadata is None:
        metadata = {}
    if not isinstance(metadata, dict):
        raise TypeError("metadata must be dict")
    output.metadata = metadata

    if output_type != 'pyerr':
        if output_text is not None:
            output.text = cast_unicode(output_text)
        if output_png is not None:
            output.png = cast_unicode(output_png)
        if output_jpeg is not None:
            output.jpeg = cast_unicode(output_jpeg)
        if output_html is not None:
            output.html = cast_unicode(output_html)
        if output_svg is not None:
            output.svg = cast_unicode(output_svg)
        if output_latex is not None:
            output.latex = cast_unicode(output_latex)
        if output_json is not None:
            output.json = cast_unicode(output_json)
        if output_javascript is not None:
            output.javascript = cast_unicode(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 ename is not None:
            output.ename = cast_unicode(ename)
        if evalue is not None:
            output.evalue = cast_unicode(evalue)
        if traceback is not None:
            output.traceback = [cast_unicode(frame) for frame in list(traceback)]

    if output_type == u'stream':
        output.stream = 'stdout' if stream is None else cast_unicode(stream)
    
    return output
Esempio n. 46
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)
Esempio n. 47
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)
Esempio n. 48
0
def _widget_abbrev_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, dict):
        return Dropdown(options=o)
    elif isinstance(o, bool):
        return Checkbox(value=o)
    elif isinstance(o, float):
        min, max, value = _get_min_max_value(None, None, o)
        return FloatSlider(value=o, min=min, max=max)
    elif isinstance(o, int):
        min, max, value = _get_min_max_value(None, None, o)
        return IntSlider(value=o, min=min, max=max)
    else:
        return None
Esempio n. 49
0
def _widget_abbrev_single_value(o):
    """Make widgets from single values, which can be used as parameter defaults."""
    if isinstance(o, string_types):
        return TextWidget(value=unicode_type(o))
    elif isinstance(o, dict):
        return DropdownWidget(values=o)
    elif isinstance(o, bool):
        return CheckboxWidget(value=o)
    elif isinstance(o, float):
        min, max, value = _get_min_max_value(None, None, o)
        return FloatSliderWidget(value=o, min=min, max=max)
    elif isinstance(o, int):
        min, max, value = _get_min_max_value(None, None, o)
        return IntSliderWidget(value=o, min=min, max=max)
    else:
        return None
Esempio n. 50
0
def yield_everything(obj):
    """Yield every item in a container as bytes

    Allows any JSONable object to be passed to an HMAC digester
    without having to serialize the whole thing.
    """
    if isinstance(obj, dict):
        for key in sorted(obj):
            value = obj[key]
            yield cast_bytes(key)
            for b in yield_everything(value):
                yield b
    elif isinstance(obj, (list, tuple)):
        for element in obj:
            for b in yield_everything(element):
                yield b
    elif isinstance(obj, unicode_type):
        yield obj.encode('utf8')
    else:
        yield unicode_type(obj).encode('utf8')
Esempio n. 51
0
def yield_everything(obj):
    """Yield every item in a container as bytes
    
    Allows any JSONable object to be passed to an HMAC digester
    without having to serialize the whole thing.
    """
    if isinstance(obj, dict):
        for key in sorted(obj):
            value = obj[key]
            yield cast_bytes(key)
            for b in yield_everything(value):
                yield b
    elif isinstance(obj, (list, tuple)):
        for element in obj:
            for b in yield_everything(element):
                yield b
    elif isinstance(obj, unicode_type):
        yield obj.encode('utf8')
    else:
        yield unicode_type(obj).encode('utf8')
Esempio n. 52
0
    def julia(self, line, cell=None):
        """
        Execute code in Julia, and pull some of the results back into the
        Python namespace.
        """
        src = compat.unicode_type(line if cell is None else cell)

        # fmt: off

        # We assume the caller's frame is the first parent frame not in the
        # IPython module. This seems to work with IPython back to ~v5, and
        # is at least somewhat immune to future IPython internals changes,
        # although by no means guaranteed to be perfect.
        caller_frame = sys._getframe(3)
        while caller_frame.f_globals.get('__name__').startswith("IPython"):
            caller_frame = caller_frame.f_back

        return self._julia.eval("""
        _PyJuliaHelper.@prepare_for_pyjulia_call begin %s end
        """ % src)(self.shell.user_ns, caller_frame.f_locals)
Esempio n. 53
0
def writes(nb, format, **kwargs):
    """Write a notebook to a string in a given format in the current nbformat version.

    This function always writes the notebook in the current nbformat version.

    Parameters
    ----------
    nb : NotebookNode
        The notebook to write.
    format : (u'json', u'ipynb', u'py')
        The format to write the notebook in.

    Returns
    -------
    s : unicode
        The notebook string.
    """
    format = unicode_type(format)
    if format == u'json' or format == u'ipynb':
        return writes_json(nb, **kwargs)
    elif format == u'py':
        return writes_py(nb, **kwargs)
    else:
        raise NBFormatError('Unsupported format: %s' % format)
Esempio n. 54
0
 def validate(self, obj, value):
     try:
         return py3compat.unicode_type(value)
     except:
         self.error(obj, value)
Esempio n. 55
0
 def _session_default(self):
     u = unicode_type(uuid.uuid4())
     self.bsession = u.encode('ascii')
     return u
Esempio n. 56
0
 def validate(self, obj, value):
     if isinstance(value, py3compat.unicode_type):
         return value
     if isinstance(value, bytes):
         return py3compat.unicode_type(value)
     self.error(obj, value)
Esempio n. 57
0
 def _find_ext(self, s):
     return unicode_type(s.split('.')[-1].lower())
Esempio n. 58
0
    def __init__(self,
                 data=None,
                 url=None,
                 filename=None,
                 format=u'png',
                 embed=None,
                 width=None,
                 height=None,
                 retina=False):
        """Create a PNG/JPEG image object given raw data.

        When this object is returned by an input cell or passed to the
        display function, it will result in the image being displayed
        in the frontend.

        Parameters
        ----------
        data : unicode, str or bytes
            The raw image data or a URL or filename to load the data from.
            This always results in embedded image data.
        url : unicode
            A URL to download the data from. If you specify `url=`,
            the image data will not be embedded unless you also specify `embed=True`.
        filename : unicode
            Path to a local file to load the data from.
            Images from a file are always embedded.
        format : unicode
            The format of the image data (png/jpeg/jpg). If a filename or URL is given
            for format will be inferred from the filename extension.
        embed : bool
            Should the image data be embedded using a data URI (True) or be
            loaded using an <img> tag. Set this to True if you want the image
            to be viewable later with no internet connection in the notebook.

            Default is `True`, unless the keyword argument `url` is set, then
            default value is `False`.

            Note that QtConsole is not able to display images if `embed` is set to `False`
        width : int
            Width to which to constrain the image in html
        height : int
            Height to which to constrain the image in html
        retina : bool
            Automatically set the width and height to half of the measured
            width and height.
            This only works for embedded images because it reads the width/height
            from image data.
            For non-embedded images, you can just set the desired display width
            and height directly.

        Examples
        --------
        # embedded image data, works in qtconsole and notebook
        # when passed positionally, the first arg can be any of raw image data,
        # a URL, or a filename from which to load image data.
        # The result is always embedding image data for inline images.
        Image('http://www.google.fr/images/srpr/logo3w.png')
        Image('/path/to/image.jpg')
        Image(b'RAW_PNG_DATA...')

        # Specifying Image(url=...) does not embed the image data,
        # it only generates `<img>` tag with a link to the source.
        # This will not work in the qtconsole or offline.
        Image(url='http://www.google.fr/images/srpr/logo3w.png')

        """
        if filename is not None:
            ext = self._find_ext(filename)
        elif url is not None:
            ext = self._find_ext(url)
        elif data is None:
            raise ValueError(
                "No image data found. Expecting filename, url, or data.")
        elif isinstance(data, string_types) and (data.startswith('http')
                                                 or _safe_exists(data)):
            ext = self._find_ext(data)
        else:
            ext = None

        if ext is not None:
            format = ext.lower()
            if ext == u'jpg' or ext == u'jpeg':
                format = self._FMT_JPEG
            if ext == u'png':
                format = self._FMT_PNG
        elif isinstance(data, bytes) and format == 'png':
            # infer image type from image data header,
            # only if format might not have been specified.
            if data[:2] == _JPEG:
                format = 'jpeg'

        self.format = unicode_type(format).lower()
        self.embed = embed if embed is not None else (url is None)

        if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
            raise ValueError("Cannot embed the '%s' image format" %
                             (self.format))
        self.width = width
        self.height = height
        self.retina = retina
        super(Image, self).__init__(data=data, url=url, filename=filename)

        if retina:
            self._retina_shape()
Esempio n. 59
0
 def _ident_default(self):
     return unicode_type(uuid.uuid4())