Exemple #1
0
def clear_output(stdout=True, stderr=True, other=True):
    """Clear the output of the current cell receiving output.

    Optionally, each of stdout/stderr or other non-stream data (e.g. anything
    produced by display()) can be excluded from the clear event.

    By default, everything is cleared.

    Parameters
    ----------
    stdout : bool [default: True]
        Whether to clear stdout.
    stderr : bool [default: True]
        Whether to clear stderr.
    other : bool [default: True]
        Whether to clear everything else that is not stdout/stderr
        (e.g. figures,images,HTML, any result of display()).
    """
    from IPython.core.interactiveshell import InteractiveShell
    if InteractiveShell.initialized():
        InteractiveShell.instance().display_pub.clear_output(
            stdout=stdout, stderr=stderr, other=other,
        )
    else:
        from IPython.utils import io
        if stdout:
            print('\033[2K\r', file=io.stdout, end='')
            io.stdout.flush()
        if stderr:
            print('\033[2K\r', file=io.stderr, end='')
            io.stderr.flush()
Exemple #2
0
def running_from_terminal():
    try:
        # Check if it is running inside IPython
        __IPYTHON__
        # Check that it is available (i.e. that we are not in the
        # standard terminal vs the Notebook, QtConsole...
        #clear_output()
        from IPython.core.interactiveshell import InteractiveShell
        InteractiveShell.instance().display_pub.clear_output
        from IPython.core.display import clear_output
        return False
    except:
        return True
Exemple #3
0
def clear_output(wait=False):
    """Clear the output of the current cell receiving output.

    Parameters
    ----------
    wait : bool [default: false]
        Wait to clear the output until new output is available to replace it."""
    from IPython.core.interactiveshell import InteractiveShell
    if InteractiveShell.initialized():
        InteractiveShell.instance().display_pub.clear_output(wait)
    else:
        print('\033[2K\r', end='')
        sys.stdout.flush()
        print('\033[2K\r', end='')
        sys.stderr.flush()
Exemple #4
0
def format_display_data(obj, include=None, exclude=None):
    """Return a format data dict for an object.

    By default all format types will be computed.

    Parameters
    ----------
    obj : object
        The Python object whose format data will be computed.

    Returns
    -------
    format_dict : dict
        A dictionary of key/value pairs, one or each format that was
        generated for the object. The keys are the format types, which
        will usually be MIME type strings and the values and JSON'able
        data structure containing the raw data for the representation in
        that format.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type string (MIME types) to exclue in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    from IPython.core.interactiveshell import InteractiveShell

    return InteractiveShell.instance().display_formatter.format(
        obj,
        include,
        exclude
    )
Exemple #5
0
        def get_ip (self):

            """Get the global InteractiveShell instance."""

            shell = InteractiveShell.instance()

            return shell
Exemple #6
0
    def register_to_ipython_shell(
            shell: Optional[InteractiveShell] = None) -> None:
        """Register transpiler hooks to IPython shell."""
        shell_inst: InteractiveShell = shell or InteractiveShell.instance()

        @magics_class
        class DialectMagics(Magics):  # type: ignore
            def __init__(self, shell: InteractiveShell, **kwargs: Any) -> None:
                super().__init__(shell, **kwargs)
                for transformer in shell.ast_transformers:
                    if isinstance(transformer, DialectNodeTransformer):
                        break
                else:
                    shell.ast_transformers.insert(0, DialectNodeTransformer())

            @cell_magic  # type: ignore
            def dialect(self, cell_dialect: str, raw_cell: str) -> None:
                reducer = dialect_reducer(cell_dialect)
                _dialect_reducer_fifo_queue.append(reducer)

                self.shell.run_cell(
                    # We need to prepend this since we can't look for
                    # the dialect comment when transforming the AST.
                    f"_DIALECT_ = {cell_dialect!r}\n" +
                    reducer.transform_src(raw_cell))

        shell_inst.register_magics(DialectMagics)
def add_display_formatter(new_formatter):
    from IPython.core.formatters import FormatterABC
    FormatterABC.register(new_formatter)
    from IPython.core.interactiveshell import InteractiveShell
    inst = InteractiveShell.instance()
    f = new_formatter(config=inst.display_formatter.config)
    inst.display_formatter.formatters[f.format_type] = f
Exemple #8
0
def set_matplotlib_formats(*formats, **kwargs):
    """Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::
    
        c.InlineBackend.figure_formats = {'png', 'jpeg'}
        c.InlineBackend.print_figure_kwargs.update({'quality' : 90})

    Parameters
    ----------
    *formats : strs
        One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    **kwargs :
        Keyword args will be relayed to ``figure.canvas.print_figure``.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    # build kwargs, starting with InlineBackend config
    kw = {}
    from ipykernel.pylab.config import InlineBackend
    cfg = InlineBackend.instance()
    kw.update(cfg.print_figure_kwargs)
    kw.update(**kwargs)
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, **kw)
Exemple #9
0
def display(*objs, **kwargs):
    """Display a Python object in all frontends.

    By default all representations will be computed and sent to the frontends.
    Frontends can decide which representation is used and how.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type string (MIME types) to exclue in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    include = kwargs.get('include')
    exclude = kwargs.get('exclude')

    from IPython.core.interactiveshell import InteractiveShell
    inst = InteractiveShell.instance()
    format = inst.display_formatter.format
    publish = inst.display_pub.publish

    for obj in objs:
        format_dict = format(obj, include=include, exclude=exclude)
        publish('IPython.core.display.display', format_dict)
Exemple #10
0
 def register_orgformatter():
     from IPython.core.interactiveshell import InteractiveShell
     formatter = InteractiveShell.instance().display_formatter
     orgformatter = IPyOrgFormatter(parent=formatter)
     formatter.formatters[orgformatter.format_type] = orgformatter
     formatter.active_types.append(orgformatter.format_type)
     orgformatter.for_type(DataFrame, to_org)
Exemple #11
0
def run_docstring_examples(obj, verbose=True):
    from IPython.core.interactiveshell import InteractiveShell
    import doctest

    inst = InteractiveShell.instance()
    globs = inst.user_ns
    return doctest.run_docstring_examples(obj, globs, verbose=verbose)
def importNotebook(name):

    fileName = "./" + name + ".ipynb"

    #check whether or the file name exists
    if (not os.path.isfile(fileName)):
        print("File " + fileName + " does not exist, please try again.")
        return -1

    #createa a new module to hold the Juypyter notbook functions
    module = types.ModuleType(name)
    module.__file__ = fileName
    module.__dict__["get_ipython"] = get_ipython
    shell = InteractiveShell.instance()

    #load data from the notebook
    with open(fileName, 'r') as f:
        notebookData = nbformat.read(f, 4)["cells"]

    #make all text code executable
    for cell in notebookData:

        #check if cell contains code
        if (cell.cell_type == "code"):
            code = shell.input_transformer_manager.transform_cell(cell.source)
            exec(code, module.__dict__)

    return module
Exemple #13
0
def notebook_dependencies(notebook_name,
                          include_minor_dependencies=True,
                          path=None):
    # notebook_path = import_notebooks.find_notebook(notebook_name, path)
    notebook_path = notebook_name

    # load the notebook
    with io.open(notebook_path, 'r', encoding='utf-8') as f:
        notebook = nbformat.read(f, 4)

    shell = InteractiveShell.instance()

    modules = set()
    for cell in notebook.cells:
        if cell.cell_type == 'code':
            # transform the input to executable Python
            code = shell.input_transformer_manager.transform_cell(cell.source)
            if not include_minor_dependencies and code.find('# minor') >= 0:
                continue
            for match in re.finditer(RE_IMPORT, code):
                modules.add(match.group(1))
            for match in re.finditer(RE_FROM, code):
                modules.add(match.group(1))

    return modules
Exemple #14
0
def format_display_data(obj, include=None, exclude=None):
    """Return a format data dict for an object.

    By default all format types will be computed.

    Parameters
    ----------
    obj : object
        The Python object whose format data will be computed.

    Returns
    -------
    format_dict : dict
        A dictionary of key/value pairs, one or each format that was
        generated for the object. The keys are the format types, which
        will usually be MIME type strings and the values and JSON'able
        data structure containing the raw data for the representation in
        that format.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type string (MIME types) to exclue in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    from IPython.core.interactiveshell import InteractiveShell

    return InteractiveShell.instance().display_formatter.format(
        obj, include, exclude)
def shell():
    from IPython.core.interactiveshell import InteractiveShell

    shell = InteractiveShell.instance()
    shell.magic("load_ext pylada")

    return shell
Exemple #16
0
def clear_output(wait=False):
    """Clear the output of the current cell receiving output.

    Parameters
    ----------
    wait : bool [default: false]
        Wait to clear the output until new output is available to replace it."""
    from IPython.core.interactiveshell import InteractiveShell
    if InteractiveShell.initialized():
        InteractiveShell.instance().display_pub.clear_output(wait)
    else:
        from IPython.utils import io
        print('\033[2K\r', file=io.stdout, end='')
        io.stdout.flush()
        print('\033[2K\r', file=io.stderr, end='')
        io.stderr.flush()
Exemple #17
0
def _scala(code):
    global _interpreter, _stdout, _io
    _io.truncate(0)
    _io.seek(0)
    if _interpreter is None:
        raise RuntimeError("no spark session. call get_or_crate first!")

    ns = {}
    if InteractiveShell.initialized():
        ns.update(InteractiveShell.instance().user_ns)
    try:
        frame = sys._getframe(2)
    except ValueError:
        pass
    else:
        ns.update(frame.f_locals)
    if 'self' in ns:
        del ns['self']
    code = Template(code).render(**ns)
    res = _interpreter.interpret(code)
    if _stdout is not None:
        _stdout.write(res + '\n')
    _io.seek(0)
    ret = _io.read().strip().rsplit('\n', 1)[-1]
    return ret
Exemple #18
0
def display(*objs, **kwargs):
    """Display a Python object in all frontends.

    By default all representations will be computed and sent to the frontends.
    Frontends can decide which representation is used and how.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type strings (MIME types) to exclude in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    include = kwargs.get('include')
    exclude = kwargs.get('exclude')

    from IPython.core.interactiveshell import InteractiveShell
    inst = InteractiveShell.instance()
    format = inst.display_formatter.format
    publish = inst.display_pub.publish

    for obj in objs:
        format_dict = format(obj, include=include, exclude=exclude)
        publish('IPython.core.display.display', format_dict)
Exemple #19
0
def cargaCodigoDinamico(path,name,code_key = '# Cargar Celda'):
    """
    Carga el código creado en el notebook para poder ser 
    utilizado desde clases externas al notebook
    
    Parámetros: 
        
    """
    moduleName = name 
    shell = InteractiveShell.instance()
    # carga el notebook en la variable nb
    with io.open(path, 'r', encoding='utf-8') as f:
        nb = read(f, 4)
    
    # se crea un modulo dinamicamente con nombre moduleName
    mod = types.ModuleType(moduleName)
    mod.__dict__['get_ipython'] = get_ipython
    sys.modules[moduleName] = mod 
    
    code_to_load = ""
    
    # Carga las celdas de tipo code, que empiezan por "# Cargar Celda"
    for cell in nb.cells:
        if cell.cell_type == 'code':
            # convierte la entrada en codigo ejecutable
            code = shell.input_transformer_manager.transform_cell(cell.source)
            
            if code.startswith(code_key):
                # añade esta celda al codigo que se va a cargar
                code_to_load=code_to_load+"\n"+code  
    return importCode(code_to_load,moduleName)
Exemple #20
0
def set_matplotlib_formats(*formats, **kwargs):
    """Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::

        c.InlineBackend.figure_formats = {'png', 'jpeg'}
        c.InlineBackend.print_figure_kwargs.update({'quality' : 90})

    Parameters
    ----------
    *formats : strs
        One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    **kwargs :
        Keyword args will be relayed to ``figure.canvas.print_figure``.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    # build kwargs, starting with InlineBackend config
    kw = {}
    from ipykernel.pylab.config import InlineBackend
    cfg = InlineBackend.instance()
    kw.update(cfg.print_figure_kwargs)
    kw.update(**kwargs)
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, **kw)
def format_display_data(obj, include=None, exclude=None):
    """Return a format data dict for an object.

    By default all format types will be computed.

    The following MIME types are currently implemented:

    * text/plain
    * text/html
    * text/markdown
    * text/latex
    * application/json
    * application/javascript
    * application/pdf
    * image/png
    * image/jpeg
    * image/svg+xml

    Parameters
    ----------
    obj : object
        The Python object whose format data will be computed.

    Returns
    -------
    format_dict : dict
        A dictionary of key/value pairs, one or each format that was
        generated for the object. The keys are the format types, which
        will usually be MIME type strings and the values and JSON'able
        data structure containing the raw data for the representation in
        that format.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type string (MIME types) to exclue in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    from IPython.core.interactiveshell import InteractiveShell

    InteractiveShell.instance().display_formatter.format(
        obj,
        include,
        exclude
    )
Exemple #22
0
def format_display_data(obj, include=None, exclude=None):
    """Return a format data dict for an object.

    By default all format types will be computed.

    The following MIME types are currently implemented:

    * text/plain
    * text/html
    * text/markdown
    * text/latex
    * application/json
    * application/javascript
    * application/pdf
    * image/png
    * image/jpeg
    * image/svg+xml

    Parameters
    ----------
    obj : object
        The Python object whose format data will be computed.

    Returns
    -------
    format_dict : dict
        A dictionary of key/value pairs, one or each format that was
        generated for the object. The keys are the format types, which
        will usually be MIME type strings and the values and JSON'able
        data structure containing the raw data for the representation in
        that format.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type string (MIME types) to exclue in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    from IPython.core.interactiveshell import InteractiveShell

    InteractiveShell.instance().display_formatter.format(
        obj,
        include,
        exclude
    )
Exemple #23
0
def page(strng,
         start=0,
         screen_lines=0,
         pager_cmd=None,
         html=None,
         auto_html=False):
    """Print a string, piping through a pager.

    This version ignores the screen_lines and pager_cmd arguments and uses
    IPython's payload system instead.

    Parameters
    ----------
    strng : str
      Text to page.

    start : int
      Starting line at which to place the display.
    
    html : str, optional
      If given, an html string to send as well.

    auto_html : bool, optional
      If true, the input string is assumed to be valid reStructuredText and is
      converted to HTML with docutils.  Note that if docutils is not found,
      this option is silently ignored.

    Notes
    -----

    Only one of the ``html`` and ``auto_html`` options can be given, not
    both.
    """

    # Some routines may auto-compute start offsets incorrectly and pass a
    # negative value.  Offset to 0 for robustness.
    start = max(0, start)
    shell = InteractiveShell.instance()

    if auto_html:
        try:
            # These defaults ensure user configuration variables for docutils
            # are not loaded, only our config is used here.
            defaults = {
                'file_insertion_enabled': 0,
                'raw_enabled': 0,
                '_disable_config': 1
            }
            html = publish_string(strng,
                                  writer_name='html',
                                  settings_overrides=defaults)
        except:
            pass

    payload = dict(source='page',
                   text=strng,
                   html=html,
                   start_line_number=start)
    shell.payload_manager.write_payload(payload)
Exemple #24
0
    def __init__(self, backend):
        import matplotlib.pyplot as plt
        from IPython.core.interactiveshell import InteractiveShell
        from IPython.core.pylabtools import backend2gui

        self.shell = InteractiveShell.instance()
        self.old_backend = backend2gui[str(plt.get_backend())]
        self.new_backend = backend
Exemple #25
0
    def __init__(self, backend):
        import matplotlib.pyplot as plt
        from IPython.core.interactiveshell import InteractiveShell
        from IPython.core.pylabtools import backend2gui

        self.shell = InteractiveShell.instance()
        self.old_backend = backend2gui[str(plt.get_backend())]
        self.new_backend = backend
Exemple #26
0
def get_ipython():
    """Get the global InteractiveShell instance.

    Returns None if no InteractiveShell instance is registered.
    """
    from IPython.core.interactiveshell import InteractiveShell
    if InteractiveShell.initialized():
        return InteractiveShell.instance()
Exemple #27
0
 def render(self):
     from IPython.core.interactiveshell import InteractiveShell
     display_pub = InteractiveShell.instance().display_pub
     try:
         display_pub.register_hook(self.hook_msg)
         super(PixieDustApp, self).render()
     finally:
         display_pub.unregister_hook(self.hook_msg)
Exemple #28
0
def loadNoMagic():
    '''
    Load the magic functions when running iPython
    '''
    if common.runningUnderIPython():
        from IPython.core.interactiveshell import InteractiveShell
        if InteractiveShell.initialized():
            localIP = InteractiveShell.instance()
            load_ipython_extension(localIP)
Exemple #29
0
def display(*objs, **kwargs):
    """Display a Python object in all frontends.

    By default all representations will be computed and sent to the frontends.
    Frontends can decide which representation is used and how.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display.
    raw : bool, optional
        Are the objects to be displayed already mimetype-keyed dicts of raw display data,
        or Python objects that need to be formatted before display? [default: False]
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type strings (MIME types) to exclude in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    metadata : dict, optional
        A dictionary of metadata to associate with the output.
        mime-type keys in this dictionary will be associated with the individual
        representation formats, if they exist.
    """
    raw = kwargs.get('raw', False)
    include = kwargs.get('include')
    exclude = kwargs.get('exclude')
    metadata = kwargs.get('metadata')

    from IPython.core.interactiveshell import InteractiveShell

    if not raw:
        format = InteractiveShell.instance().display_formatter.format

    for obj in objs:

        # If _ipython_display_ is defined, use that to display this object.
        display_method = _safe_get_formatter_method(obj, '_ipython_display_')
        if display_method is not None:
            try:
                display_method(**kwargs)
            except NotImplementedError:
                pass
            else:
                continue
        if raw:
            publish_display_data(data=obj, metadata=metadata)
        else:
            format_dict, md_dict = format(obj,
                                          include=include,
                                          exclude=exclude)
            if metadata:
                # kwarg-specified metadata gets precedence
                _merge(md_dict, metadata)
            publish_display_data(data=format_dict, metadata=md_dict)
Exemple #30
0
def loadNoMagic():
    '''
    Load the magic functions when running iPython
    '''
    if common.runningUnderIPython():
        from IPython.core.interactiveshell import InteractiveShell
        if InteractiveShell.initialized():        
            localIP = InteractiveShell.instance()    
            load_ipython_extension(localIP)
Exemple #31
0
def display(*objs, **kwargs):
    """Display a Python object in all frontends.

    By default all representations will be computed and sent to the frontends.
    Frontends can decide which representation is used and how.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display.
    raw : bool, optional
        Are the objects to be displayed already mimetype-keyed dicts of raw display data,
        or Python objects that need to be formatted before display? [default: False]
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type strings (MIME types) to exclude in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    metadata : dict, optional
        A dictionary of metadata to associate with the output.
        mime-type keys in this dictionary will be associated with the individual
        representation formats, if they exist.
    """
    raw = kwargs.get('raw', False)
    include = kwargs.get('include')
    exclude = kwargs.get('exclude')
    metadata = kwargs.get('metadata')

    from IPython.core.interactiveshell import InteractiveShell

    if not raw:
        format = InteractiveShell.instance().display_formatter.format

    for obj in objs:

        # If _ipython_display_ is defined, use that to display this object.
        display_method = _safe_get_formatter_method(obj, '_ipython_display_')
        if display_method is not None:
            try:
                display_method(**kwargs)
            except NotImplementedError:
                pass
            else:
                continue
        if raw:
            publish_display_data(data=obj, metadata=metadata)
        else:
            format_dict, md_dict = format(
                obj, include=include, exclude=exclude)
            if metadata:
                # kwarg-specified metadata gets precedence
                _merge(md_dict, metadata)
            publish_display_data(data=format_dict, metadata=md_dict)
Exemple #32
0
def publish_display_data(source, data, metadata=None):
    """Publish data and metadata to all frontends.

    See the ``display_data`` message in the messaging documentation for
    more details about this message type.

    The following MIME types are currently implemented:

    * text/plain
    * text/html
    * text/markdown
    * text/latex
    * application/json
    * application/javascript
    * image/png
    * image/jpeg
    * image/svg+xml

    Parameters
    ----------
    source : str
        A string that give the function or method that created the data,
        such as 'IPython.core.page'.
    data : dict
        A dictionary having keys that are valid MIME types (like
        'text/plain' or 'image/svg+xml') and values that are the data for
        that MIME type. The data itself must be a JSON'able data
        structure. Minimally all data should have the 'text/plain' data,
        which can be displayed by all frontends. If more than the plain
        text is given, it is up to the frontend to decide which
        representation to use.
    metadata : dict
        A dictionary for metadata related to the data. This can contain
        arbitrary key, value pairs that frontends can use to interpret
        the data. mime-type keys matching those in data can be used
        to specify metadata about particular representations.
        """
    from IPython.core.interactiveshell import InteractiveShell
    InteractiveShell.instance().display_pub.publish(
        source,
        data,
        metadata
    )
def add_plot_payload(format, data, metadata={}):
    """ Add a plot payload to the current execution reply.

    Parameters:
    -----------
    format : str
        Identifies the format of the plot data.

    data : str
        The raw plot data.

    metadata : dict, optional [default empty]
        Allows for specification of additional information about the plot data.
    """
    payload = dict(source='IPython.zmq.pylab.backend_payload.add_plot_payload',
                   format=format,
                   data=data,
                   metadata=metadata)
    InteractiveShell.instance().payload_manager.write_payload(payload)
Exemple #34
0
def loadNoMagic():
    '''
    Load the magic functions of load_ipython_extension when running IPython
    '''
    if common.runningUnderIPython():
        # noinspection PyPackageRequirements
        from IPython.core.interactiveshell import InteractiveShell  # type: ignore
        if InteractiveShell.initialized():
            localIP = InteractiveShell.instance()
            load_ipython_extension(localIP)
Exemple #35
0
def register_text_format(cls):
    interactive = InteractiveShell.initialized()
    if interactive:  # for ipython terminal
        text_format = InteractiveShell.instance(
        ).display_formatter.formatters['text/plain']
        text_format.for_type(cls,
                             progressbar_formatter)  # doesn't affect notebooks
    else:  # for pure python in terminal
        # TODO patch without invoking ipython instance
        pass
def add_plot_payload(format, data, metadata={}):
    """ Add a plot payload to the current execution reply.

    Parameters:
    -----------
    format : str
        Identifies the format of the plot data.

    data : str
        The raw plot data.

    metadata : dict, optional [default empty]
        Allows for specification of additional information about the plot data.
    """
    payload = dict(
        source='IPython.zmq.pylab.backend_payload.add_plot_payload', 
        format=format, data=data, metadata=metadata
    )
    InteractiveShell.instance().payload_manager.write_payload(payload)
def pYPKa_ZE_ipynb_generator(tp, dir_="pYPKa_ZE_vectors"):

    cwd = os.getcwd()

    try:
        os.makedirs(dir_)
    except OSError as exception:
        if exception.errno == errno.EEXIST:
            pass
        else:
            print("The {} directory could not be created".format(dir_))
            return None

    os.chdir(dir_)

    with open("standard_primers.txt","w") as f: f.write(read_data_file("standard_primers.txt"))
    with open("pYPKa.gb","w") as f: f.write(read_data_file("pYPKa.gb"))
    with open("pYPK_ZE.png","w") as f: f.write(read_bin_file("pYPK_ZE.png"))
    with open(tp.id+".gb","w") as f: f.write(tp.format("gb"))

    nbtemp = read_data_file("nb_template_pYPKa_ZE_insert.md")

    name = "pYPKa_ZE_{}.ipynb".format(tp.id)

    obj = notedown.MarkdownReader()

    nb = obj.to_notebook(nbtemp.format(tp=tp.id))

    pp = ExecutePreprocessor()
    pp.timeout = 120 # seconds
    pp.interrupt_on_timeout = True

    shell = InteractiveShell.instance()

    nb_executed, resources = pp.preprocess(nb, resources={})

    g={}
    l={}

    from io import StringIO
    old_stdout = sys.stdout
    redirected_output = sys.stdout = StringIO()

    for cell in nb.cells:
        if cell.cell_type == 'code':
            code = shell.input_transformer_manager.transform_cell(cell.source)
            exec(code, g, l)

    sys.stdout = old_stdout

    nbformat.write(nb, name)

    os.chdir(cwd)

    return FileLinks(dir_)
Exemple #38
0
def publish_display_data(data,
                         metadata=None,
                         source=None,
                         *,
                         transient=None,
                         **kwargs):
    """Publish data and metadata to all frontends.

    See the ``display_data`` message in the messaging documentation for
    more details about this message type.

    The following MIME types are currently implemented:

    * text/plain
    * text/html
    * text/markdown
    * text/latex
    * application/json
    * application/javascript
    * image/png
    * image/jpeg
    * image/svg+xml

    Parameters
    ----------
    data : dict
        A dictionary having keys that are valid MIME types (like
        'text/plain' or 'image/svg+xml') and values that are the data for
        that MIME type. The data itself must be a JSON'able data
        structure. Minimally all data should have the 'text/plain' data,
        which can be displayed by all frontends. If more than the plain
        text is given, it is up to the frontend to decide which
        representation to use.
    metadata : dict
        A dictionary for metadata related to the data. This can contain
        arbitrary key, value pairs that frontends can use to interpret
        the data. mime-type keys matching those in data can be used
        to specify metadata about particular representations.
    source : str, deprecated
        Unused.
    transient : dict, keyword-only
        A dictionary of transient data, such as display_id.
        """
    from IPython.core.interactiveshell import InteractiveShell

    display_pub = InteractiveShell.instance().display_pub

    # only pass transient if supplied,
    # to avoid errors with older ipykernel.
    # TODO: We could check for ipykernel version and provide a detailed upgrade message.
    if transient:
        kwargs['transient'] = transient

    display_pub.publish(data=data, metadata=metadata, **kwargs)
def page(strng, start=0, screen_lines=0, pager_cmd=None,
         html=None, auto_html=False):
    """Print a string, piping through a pager.

    This version ignores the screen_lines and pager_cmd arguments and uses
    IPython's payload system instead.

    Parameters
    ----------
    strng : str
      Text to page.

    start : int
      Starting line at which to place the display.
    
    html : str, optional
      If given, an html string to send as well.

    auto_html : bool, optional
      If true, the input string is assumed to be valid reStructuredText and is
      converted to HTML with docutils.  Note that if docutils is not found,
      this option is silently ignored.

    Note
    ----

    Only one of the ``html`` and ``auto_html`` options can be given, not
    both.
    """

    # Some routines may auto-compute start offsets incorrectly and pass a
    # negative value.  Offset to 0 for robustness.
    start = max(0, start)
    shell = InteractiveShell.instance()

    if auto_html:
        try:
            # These defaults ensure user configuration variables for docutils
            # are not loaded, only our config is used here.
            defaults = {'file_insertion_enabled': 0,
                        'raw_enabled': 0,
                        '_disable_config': 1}
            html = publish_string(strng, writer_name='html',
                                  settings_overrides=defaults)
        except:
            pass
        
    payload = dict(
        source='IPython.zmq.page.page',
        text=strng,
        html=html,
        start_line_number=start
        )
    shell.payload_manager.write_payload(payload)
Exemple #40
0
    def __init__(self, **kw):
        self.shell = InteractiveShell.instance()
        self._print = print
        self.path = []
        self.node_spec_map = {}

        default_urignore = Path.cwd() / '.urignore'
        if default_urignore.exists():
            self.urignores = [UrIgnore(PathNode(default_urignore))]
        else:
            self.urignores = []
Exemple #41
0
def ipython2python(code):
    """Transform IPython syntax to pure Python syntax

    Parameters
    ----------

    code : str
        IPython code, to be transformed to pure Python
    """
    shell = InteractiveShell.instance()
    return shell.input_transformer_manager.transform_cell(code)
Exemple #42
0
def ipython2python(code):
    """Transform IPython syntax to pure Python syntax

    Parameters
    ----------

    code : str
        IPython code, to be transformed to pure Python
    """
    shell = InteractiveShell.instance()
    return shell.input_transformer_manager.transform_cell(code)
Exemple #43
0
def publish_display_data(data, metadata=None, source=None, *, transient=None, **kwargs):
    """Publish data and metadata to all frontends.

    See the ``display_data`` message in the messaging documentation for
    more details about this message type.

    The following MIME types are currently implemented:

    * text/plain
    * text/html
    * text/markdown
    * text/latex
    * application/json
    * application/javascript
    * image/png
    * image/jpeg
    * image/svg+xml

    Parameters
    ----------
    data : dict
        A dictionary having keys that are valid MIME types (like
        'text/plain' or 'image/svg+xml') and values that are the data for
        that MIME type. The data itself must be a JSON'able data
        structure. Minimally all data should have the 'text/plain' data,
        which can be displayed by all frontends. If more than the plain
        text is given, it is up to the frontend to decide which
        representation to use.
    metadata : dict
        A dictionary for metadata related to the data. This can contain
        arbitrary key, value pairs that frontends can use to interpret
        the data. mime-type keys matching those in data can be used
        to specify metadata about particular representations.
    source : str, deprecated
        Unused.
    transient : dict, keyword-only
        A dictionary of transient data, such as display_id.
        """
    from IPython.core.interactiveshell import InteractiveShell

    display_pub = InteractiveShell.instance().display_pub

    # only pass transient if supplied,
    # to avoid errors with older ipykernel.
    # TODO: We could check for ipykernel version and provide a detailed upgrade message.
    if transient:
        kwargs['transient'] = transient

    display_pub.publish(
        data=data,
        metadata=metadata,
        **kwargs
    )
Exemple #44
0
    def exec(self, globals):
        # extra work to ensure that magics that would affect the user_ns
        # actually affect the notebook module's ns
        shell = InteractiveShell.instance()
        save_user_ns = shell.user_ns
        shell.user_ns = globals

        try:
            code = shell.input_transformer_manager.transform_cell(self.content)
            exec(code, globals)
        finally:
            shell.user_ns = save_user_ns
Exemple #45
0
 def setUpClass(cls):
     pytest.importorskip('IPython')
     try:
         import mock
     except ImportError:
         try:
             from unittest import mock
         except ImportError:
             pytest.skip("Mock is not installed")
     cls.mock = mock
     from IPython.core.interactiveshell import InteractiveShell
     cls.display_formatter = InteractiveShell.instance().display_formatter
Exemple #46
0
def test_no_widget_view():
    # ensure IPython shell is instantiated
    # otherwise display() just calls print
    shell = InteractiveShell.instance()

    with capture_output() as cap:
        w = Widget()
        display(w)

    assert cap.outputs == [], repr(cap.outputs)
    assert cap.stdout == '', repr(cap.stdout)
    assert cap.stderr == '', repr(cap.stderr)
Exemple #47
0
 def setup_class(cls):
     pytest.importorskip('IPython')
     try:
         import mock
     except ImportError:
         try:
             from unittest import mock
         except ImportError:
             pytest.skip("Mock is not installed")
     cls.mock = mock
     from IPython.core.interactiveshell import InteractiveShell
     cls.display_formatter = InteractiveShell.instance().display_formatter
def process_repository(session, status, repository, query_iter):
    query_iter = list(query_iter)
    zip_path = None
    tarzip = None
    if not repository.path.exists():
        if not repository.zip_path.exists():
            repository.processed |= consts.R_UNAVAILABLE_FILES
            session.add(repository)
            status.count += len(query_iter)
            return "Failed. Repository not found: {}".format(repository)
        tarzip =  tarfile.open(str(repository.zip_path))
        zip_path = Path(repository.hash_dir2)

    shell = InteractiveShell.instance()
    group = groupby(
        query_iter,
        lambda x: (x[1])
    )
    for notebook, new_iter in group:
        cells = list(query_iter)
        vprint(1, "Processing notebook: {}. Found {} cells".format(notebook, len(cells)))
        name = notebook.name
        vprint(2, "Loading notebook file")
        if tarzip:
            notebook = nbf.read(
                tarzip.extractfile(tarzip.getmember(str(zip_path / name))),
                nbf.NO_CONVERT
            )
        else:
            with open(str(repository.path / name)) as ofile:
                notebook = nbf.read(ofile, nbf.NO_CONVERT)
        notebook = nbf.convert(notebook, 4)
        metadata = notebook["metadata"]
        language_info = metadata.get("language_info", {})
        language_name = language_info.get("name", "unknown")

        for cell, _, _ in new_iter:
            vprint(2, "Loading cell {}".format(cell.index))

            index = int(cell.index)
            notebook_cell = notebook["cells"][index]
            source = notebook_cell.get("source", "")
            if language_name == "python" and notebook_cell.get("cell_type") == "code":
                try:
                    source = shell.input_transformer_manager.transform_cell(source)
                except (IndentationError, SyntaxError):
                    pass
            cell.source = source
            if cell.processed & consts.C_MARKED_FOR_EXTRACTION:
                cell.processed -= consts.C_MARKED_FOR_EXTRACTION
            session.add(cell)
        session.commit()
    return "ok"
Exemple #49
0
def test_no_widget_view():
    # ensure IPython shell is instantiated
    # otherwise display() just calls print
    shell = InteractiveShell.instance()

    with capture_output() as cap:
        w = Widget()
        display(w)

    assert cap.outputs == [], repr(cap.outputs)
    assert cap.stdout == '', repr(cap.stdout)
    assert cap.stderr == '', repr(cap.stderr)
Exemple #50
0
def custom_display(lhs, rhs):
    """
    lhs: left hand side
    rhs: right hand side
    
    This function serves to inject the string for the left hand side
    of an assignment
    """
    
    # This code is mainly copied from IPython/display.py
    # (IPython version 2.3.0)
    kwargs = {}
    raw = kwargs.get('raw', False)
    include = kwargs.get('include')
    exclude = kwargs.get('exclude')
    metadata = kwargs.get('metadata')

    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.displaypub import publish_display_data

    format = InteractiveShell.instance().display_formatter.format
    format_dict, md_dict = format(rhs, include=include, exclude=exclude)
    
    # exampl format_dict (for a sympy expression):
    # {u'image/png': '\x89PNG\r\n\x1a\n\x00 ...\x00\x00IEND\xaeB`\x82',
    #  u'text/latex': '$$- 2 \\pi \\sin{\\left (2 \\pi t \\right )}$$',
    # u'text/plain': u'-2\u22c5\u03c0\u22c5sin(2\u22c5\u03c0\u22c5t)'}
    
    # it is up to IPython which item value is finally used
    
    # now merge the lhs into the dict:
    
    if not isinstance(lhs, basestring):
        raise TypeError('unexpexted Type for lhs object: %s' %type(lhs))
    
    new_format_dict = {}
    for key, value in format_dict.items():
        if 'text/' in key:
            new_value = lhs+' := '+value
            new_format_dict[key] = new_value
        else:
            # this happens e.g. for mime-type (i.e. key) 'image/png'
            new_format_dict = format_dict
            
    # legacy IPython 2.x support
    if IPython.__version__.startswith('2.'):
        publish_display_data('display', new_format_dict, md_dict)
    else:
        # indeed, I dont know with which version the api changed
        # but it does not really matter (for me)
        publish_display_data(data=new_format_dict, metadata=md_dict)
Exemple #51
0
 def _get_interactive_locals():
     """
     If we are running under IPython, extracts the local variables;
     otherwise, returns an empty dict.
     """
     try:
         __IPYTHON__  # pylint: disable=E0602
         from IPython.core.interactiveshell import InteractiveShell
         return {k: v for k, v in
                 InteractiveShell.instance().user_ns.items()
                 if k[0] != '_' and k not in (
                     "Out", "In", "exit", "quit", "get_ipython")}
     except NameError:
         return {}
def test_widget_view():
    # ensure IPython shell is instantiated
    # otherwise display() just calls print
    shell = InteractiveShell.instance()

    with capture_output() as cap:
        w = Button()
        display(w)

    assert len(cap.outputs) == 1, "expect 1 output"
    mime_bundle = cap.outputs[0].data
    assert mime_bundle['text/plain'] == repr(w), "expected plain text output"
    assert 'application/vnd.jupyter.widget-view+json' in mime_bundle, "widget should have have a view"
    assert cap.stdout == '', repr(cap.stdout)
    assert cap.stderr == '', repr(cap.stderr)
Exemple #53
0
 def __init__(self, local_ns=None, global_ns=None, default_value=None, **metadata):
     if local_ns is None and global_ns is None:
         try:
             #Avoid creating a new interactiveshell instance
             __IPYTHON__
         except NameError:
             local_ns = globals()
             global_ns = globals()
         else:
             shell = InteractiveShell.instance()
             local_ns = shell.user_ns
             global_ns = shell.user_global_ns
     self.locals = local_ns
     self.globals = global_ns
     super(ExecutableTrait,self).__init__(default_value, **metadata)
    def append_display_data(self, display_object):
        """Append a display object as an output.

        Parameters
        ----------
        display_object : IPython.core.display.DisplayObject
            The object to display (e.g., an instance of
            `IPython.display.Markdown` or `IPython.display.Image`).
        """
        fmt = InteractiveShell.instance().display_formatter.format
        data, metadata = fmt(display_object)
        self.outputs += (
            {
                'output_type': 'display_data',
                'data': data,
                'metadata': metadata
            },
        )
Exemple #55
0
def create_inputhook_gevent(mgr):
    """Create an input hook for running the gevent event loop.

    Parameters
    ----------
    mgr : an InputHookManager

    Returns
    -------
    An inputhook 
    """

    # Re-use previously created inputhook if any
    ip = InteractiveShell.instance()
    if hasattr(ip, '_inputhook_gevent'):
        return ip._inputhook_gevent

    got_kbdint = [False]

    def inputhook_gevent():
        """PyOS_InputHook python hook for Gevent.
        """
        try:
            ignore_CTRL_C()
            gevent.sleep(0.01)
            while not stdin_ready():
                gevent.sleep(0.05)
        except:
            ignore_CTRL_C()
            from traceback import print_exc
            print_exc()
        finally:
            allow_CTRL_C()
        return 0

    def preprompthook_gevent(ishell):
        if got_kbdint[0]:
            mgr.set_inputhook(inputhook_gevent)
        got_kbdint[0] = False

    ip._inputhook_gevent = inputhook_gevent
    ip.set_hook('pre_prompt_hook', preprompthook_gevent)

    return inputhook_gevent
Exemple #56
0
def save_n_explore(folder, path):
  """ Save and explore job-folder. 

      For use with ipython interactive terminal only.
  """ 
  from .. import is_interactive
  from ..error import interactive as ierror
  if not is_interactive: raise ierror('Not in interactive session.')

  from IPython.core.interactiveshell import InteractiveShell
  from ..ipython.explore import explore
  from ..ipython.savefolders import savefolders
  import pylada
  
  pylada.interactive.jobfolder = folder.root
  pylada.interactive.jobfolder_path = path
  shell = InteractiveShell.instance()
  savefolders(shell, path)
  explore(shell, '{0}  --file'.format(path))