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()
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 #3
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 #4
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 #5
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 #6
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)
        def get_ip (self):

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

            shell = InteractiveShell.instance()

            return shell
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 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 #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 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
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 #14
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 #15
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 #16
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)
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 #19
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 #20
0
def hello_world():
    from IPython.core.interactiveshell import InteractiveShell

    # I replace \r\n with \n...this might cause problems for code that has legitimate \r characters in it
    # (like in a string)
    code = request.values.get('c','').replace('\r\n','\n')
    if len(code)>0:
        s="Code<br/><pre>%r</pre><hr/>"%code
        try:
            a=InteractiveShell()
            with capture() as out:
                a.run_cell(code)
#            c=compile(code,'<string>','exec')
#            with capture() as out:
#                exec c
            s+="Standard out<br/><pre>%s</pre><hr/>Standard Error<br/><pre>%s</pre>"%tuple(out)
        except Exception as e:
            s+="Error: %s"%e
        return s
    return "<form><textarea name='c' cols='100' rows='20'></textarea><br/><input type='submit'></form>"
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 #22
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 #23
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 #24
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
Exemple #25
0
def crash_handler_lite(etype, evalue, tb):
    """a light excepthook, adding a small message to the usual traceback"""
    traceback.print_exception(etype, evalue, tb)
    
    from IPython.core.interactiveshell import InteractiveShell
    if InteractiveShell.initialized():
        # we are in a Shell environment, give %magic example
        config = "%config "
    else:
        # we are not in a shell, show generic config
        config = "c."
    print(_lite_message_template.format(email=author_email, config=config), file=sys.stderr)
Exemple #26
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 #27
0
 def _load_ipyhon(self):
     """ Try loading IPython shell. The result is set in self._ipython
     (can be None if IPython not available).
     """
     
     # Init
     self._ipython = None
     import __main__
     
     # Try importing IPython
     try:
         import IPython
     except ImportError:
         return
     
     # Version ok?
     if IPython.version_info < (1,):
         return
     
     # Create an IPython shell
     from IPython.core.interactiveshell import InteractiveShell 
     self._ipython = InteractiveShell(user_module=__main__)
     
     # Set some hooks / event callbacks
     # Run hook (pre_run_code_hook is depreacted in 2.0)
     pre_run_cell_hook  = self.ipython_pre_run_cell_hook
     if IPython.version_info < (2,):
         self._ipython.set_hook('pre_run_code_hook', pre_run_cell_hook)
     else:
         self._ipython.events.register('pre_run_cell', pre_run_cell_hook)
     # Other hooks
     self._ipython.set_hook('editor', self.ipython_editor_hook)
     self._ipython.set_custom_exc((bdb.BdbQuit,), self.dbstop_handler)
     
     # Some patching
     self._ipython.ask_exit = self.ipython_ask_exit
     
     # Make output be shown on Windows
     if sys.platform.startswith('win'):
         # IPython wraps std streams just like we do below, but
         # pyreadline adds *another* wrapper, which is where it
         # goes wrong. Here we set it back to bypass pyreadline.
         from IPython.utils import io
         io.stdin = io.IOStream(sys.stdin)
         io.stdout = io.IOStream(sys.stdout)
         io.stderr = io.IOStream(sys.stderr)
         
         # Ipython uses msvcrt e.g. for pausing between pages
         # but this does not work in pyzo
         import msvcrt
         msvcrt.getwch = msvcrt.getch = input  # input is deffed above
Exemple #28
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 {}
Exemple #29
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 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)
    def _ipython_display_(self, **kwargs):

        # from IPython.Widget._ipython_display_
        if InteractiveShell.initialized():
            if self.widget._view_name is not None:
                plaintext = repr(self)
                data = {
                    "text/plain": plaintext,
                    "application/vnd.jupyter.widget-view+json": {
                        "version_major": 2,
                        "version_minor": 0,
                        "model_id": self.widget._model_id,
                    },
                }
                IPython.display.display(data, raw=True)
                self.widget._handle_displayed(**kwargs)
Exemple #32
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 {}
Exemple #33
0
    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 #34
0
def ip():
    """
    Get an instance of IPython.InteractiveShell.

    Will raise a skip if IPython is not installed.
    """
    pytest.importorskip("IPython", minversion="6.0.0")
    from IPython.core.interactiveshell import InteractiveShell

    # GH#35711 make sure sqlite history file handle is not leaked
    from traitlets.config import Config  # isort:skip

    c = Config()
    c.HistoryManager.hist_file = ":memory:"

    return InteractiveShell(config=c)
Exemple #35
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

    return InteractiveShell.instance().display_formatter.format(
        obj,
        include,
        exclude
    )
def publish_display_data(data, metadata=None, source=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.

    display_pub.publish(data=data, metadata=metadata, **kwargs)
Exemple #37
0
def write_notebook_source(path):
    nb = nbf.read(path, as_version=nbf.NO_CONVERT) # validate nbformat 4 only
        
    # transform the input to executable Python (clearly doesn't remove ipython code!!!)
    code = InteractiveShell.instance().input_transformer_manager.transform_cell(code_from_ipynb(nb))
        
    # add imports to code
    add_imports = []
    code_lines = code.splitlines()
    for code_line in code_lines:
        # many caveats - command line parameters :  
        # http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-run
        if code_line.strip().startswith("get_ipython().run_line_magic('run'"): # what about quotes?
            nb_path = code_line.split(', ')[1].strip(")'") # check if endswith .ipy[nb]
            nb_path = os.path.normpath(nb_path)
            if not os.path.isabs(nb_path):
                nb_path = os.path.normpath(os.path.dirname(path) + os.path.sep + nb_path)
            
            # recursive call to deal with run magics
            write_notebook_source(nb_path)
            
            # add imports
            common_path = os.path.commonpath([path, nb_path])
            # if not common path bail!
            path_to_file = os.path.relpath(nb_path, common_path)
            path_to_file = os.path.dirname(path_to_file)
            # replace path separator with . following rules:
            # https://docs.pytest.org/en/latest/goodpractices.html
            path_to_file = (path_to_file.replace(os.path.sep, '.') + '.'
                + os.path.split(nb_path)[1])
            path_to_file, _ = os.path.splitext(path_to_file)
            import_statement = ('from ' + path_to_file + ' import * ')
            add_imports.append(import_statement)
            
    # sort imports
    code = SortImports(file_contents=code, add_imports=add_imports).output
            
    # extract imports, function defs, class defs and constants
    code = filter_ast(ast.parse(code)) #code_from_ipynb(nb)
        
    # write the file
    py_path = os.path.splitext(path)[0] + '.py'
    with open(py_path, 'w', encoding='utf-8') as py_file:
        py_file.write(to_source(code))
        print('Written file ', py_path)
        py_paths_written.append(py_path)
Exemple #38
0
class IPythonStore:
    """
    A connector for getting (one-way) items stored with IPython/Jupyter
    %store magic.

    It wraps the underlying PickleStoreDB (_db) most thinly, stripping out
    the 'autorestore/' namespace added by %store magic.
    """
    _db = _InteractiveShell.instance().db  # IPython's PickleStore
    _NAMESPACE = 'autorestore/'  # IPython StoreMagic's "namespace"

    root = _path_join(
        str(_db.root),
        _NAMESPACE)  # The root directory of the store, used for watching

    def _trim(self, key: str, _ns=_NAMESPACE):
        # _ns = self._NAMESPACE
        return key[len(_ns):] if key.startswith(_ns) else key

    def keys(self):
        return (self._trim(key) for key in self._db.keys())

    def items(self):
        for key in self._db.keys():
            try:
                yield self._trim(key), self._db[key]
            except KeyError:  # Object unpickleable in this env; skip
                pass

    def get(self, key: str, default=None):
        return self._db.get(self._NAMESPACE + key, self._db.get(key))

    def __getitem__(self, key):
        return self._db[self._NAMESPACE + key]

    def __delitem__(self, key):
        del self._db[self._NAMESPACE + key]

    def __contains__(self, key):
        return (self._NAMESPACE + key) in self._db

    def __iter__(self):
        return iter(self.keys())

    def __len__(self):
        return len(list(self.keys()))
Exemple #39
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 raw:
        for obj in objs:
            publish_display_data('display', obj, metadata)
    else:
        format = InteractiveShell.instance().display_formatter.format
        for obj in objs:
            format_dict, md_dict = format(obj,
                                          include=include,
                                          exclude=exclude)
            if metadata:
                # kwarg-specified metadata gets precedence
                _merge(md_dict, metadata)
            publish_display_data('display', format_dict, md_dict)
Exemple #40
0
 def _is_notebook(self):
     try:
         from IPython.core.interactiveshell import InteractiveShell
         from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell as notebook
         from IPython.terminal.interactiveshell import TerminalInteractiveShell as shell
         if InteractiveShell.initialized():
             ip = get_ipython()
             if isinstance(ip, notebook):
                 return True
             elif isinstance(ip, shell):
                 return False
             else:
                 raise Exception('Wrong Shell')
         else:
             return False
     except Exception as e:
         self.print_error(e)
         return False
Exemple #41
0
def enable_ipython_completer():
    import sys
    if 'IPython' in sys.modules:
        ip_running = False
        try:
            from IPython.core.interactiveshell import InteractiveShell
            ip_running = InteractiveShell.initialized()
        except ImportError:
            # support <ipython-0.11
            from IPython import ipapi as _ipapi
            ip_running = _ipapi.get() is not None
        except Exception:
            pass
        if ip_running:
            from . import ipy_completer
            return ipy_completer.load_ipython_extension()

    raise RuntimeError('completer must be enabled in active ipython session')
Exemple #42
0
def hlt(line, cell):
    if not line:
        raise ValueError("Please provide a title for the highlight")
    outputs = []

    def capture_display(msg):
        if msg['content']['data'] in outputs:  # display only once
            return None
        outputs.append(msg['content'])
        return msg

    cell = cell.strip()

    annotation = None

    if cell.endswith('"""'):
        *rest, annotation = cell.split('"""')[:-1]
        cell = '"""'.join(rest)

    shell = InteractiveShell.instance()
    # Capture any output that is displayed before output (like matplotlib plots)
    shell.display_pub.register_hook(capture_display)

    header = f'## {line}'

    try:
        publish_display_data({'text/markdown': header})

        result = shell.run_cell(cell).result
        outputs.append(
            dict(
                zip(('data', 'metadata'),
                    shell.display_formatter.format(result))))

        if annotation:
            publish_display_data({'text/markdown': annotation})
    finally:
        shell.display_pub.unregister_hook(capture_display)

    all_out = outputs

    env.o.add_highlight(line, all_out)

    return None
Exemple #43
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 #44
0
    def _ipython_display_(self, **kwargs):
        from IPython.core.interactiveshell import InteractiveShell
        import IPython

        # from IPython.Widget._ipython_display_
        if InteractiveShell.initialized():
            if self.widget._view_name is not None:

                plaintext = repr(self)
                data = {
                    'text/plain': plaintext,
                    'application/vnd.jupyter.widget-view+json': {
                        'version_major': 2,
                        'version_minor': 0,
                        'model_id': self.widget._model_id
                    }
                }
                IPython.display.display(data, raw=True)
                self.widget._handle_displayed(**kwargs)
Exemple #45
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)
Exemple #46
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))
Exemple #47
0
    def _update_plot(self):
        # Set max of 4 frames per second
        if time.time() - self._last_draw < 0.25:
            return

        self._last_draw = time.time()

        self.ax.relim()
        self.ax.cla()
        self.ax.plot(self.xdata, self.ydata)
        self.ax.legend(labels=[f"{self.ylabel} ({self.ydata[-1]:.4f})"])

        if InteractiveShell.initialized():
            # Support for notebook backend.
            print("ADLStream",
                  end="")  # Needed due to a bug (StackOverflow #66176016).
            display(self.fig)
            clear_output(wait=True)

        plt.pause(1e-9)
Exemple #48
0
def run_notebook(path, start=0, last=None):
    shell = InteractiveShell.instance()
    with io.open(path, 'r', encoding='utf-8') as f:
        nb = read(f, 4)

    result = []
    if last is None:
        last = len(nb.cells)

    for cell_num in range(start, last):
        if nb.cells[cell_num].cell_type == 'code':
            # transform the input to executable Python
            code = shell.input_transformer_manager.transform_cell(
                nb.cells[cell_num].source)
            # run the code in themodule
        else:
            code = nb.cells[cell_num].source
        result.append(shell.run_cell(code))

    return result
Exemple #49
0
def ipython2python(code):
    """Transform IPython syntax to pure Python syntax

    Parameters
    ----------

    code : str
        IPython code, to be transformed to pure Python
    """
    try:
        from IPython.core.interactiveshell import InteractiveShell
    except ImportError:
        warnings.warn(
            "IPython is needed to transform IPython syntax to pure Python."
            " Install ipython if you need this functionality."
        )
        return code
    else:
        shell = InteractiveShell.instance()
        return shell.input_transformer_manager.transform_cell(code)
Exemple #50
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()
     except RunInPixieDebugger:
         self.options.pop("handlerId")
         self.options['new_parent_prefix'] = False
         entity={
             "breakpoints": ["{}.{}".format(self.__pixieapp_class_name__, self.breakpoints)],
             "code": self.get_pd_controls()["command"]
         }
         self.breakpoints = None
         runPixieApp(
             "pixiedust.apps.debugger.PixieDebugger", 
             parent_pixieapp=self, 
             entity=entity,
             **{"options":self.options})
     finally:
         display_pub.unregister_hook(self.hook_msg)
Exemple #51
0
def cache(line, cell):
    key = str(hash(cell.strip()))
    cache = env.o.get_cached().get(key)

    if cache:
        print(f"Using cached version ('{key}')")
        for output in cache:
            publish_display_data(**output)
        return None

    outputs = []

    def capture_display(msg):
        if msg['content']['data'] in outputs:  # display only once
            return None
        outputs.append(msg['content'])
        return msg

    shell = InteractiveShell.instance()
    # Capture any output that is displayed before output (like matplotlib plots)
    shell.display_pub.register_hook(capture_display)

    try:
        output = shell.run_cell(cell)
        result = output.result
        if output.error_in_exec:
            raise RuntimeError("Error in cell output, not caching")
        outputs.append(
            dict(
                zip(('data', 'metadata'),
                    shell.display_formatter.format(result))))
    finally:
        shell.display_pub.unregister_hook(capture_display)

    all_out = outputs

    env.o.cache_result(key, all_out)

    return None
Exemple #52
0
def print_notebook_dependencies(notebook_name, 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)
            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))

    for module in modules:
        print(module)
Exemple #53
0
def print_callback(val):
    """
    Internal function.
    This function is called via a call back returning from IPC to Cython
    to Python. It tries to perform incremental printing to IPython Notebook or
    Jupyter Notebook and when all else fails, just prints locally.
    """
    success = False
    try:
        # for reasons I cannot fathom, regular printing, even directly
        # to io.stdout does not work.
        # I have to intrude rather deep into IPython to make it behave
        if have_ipython:
            if InteractiveShell.initialized():
                IPython.display.publish_display_data({'text/plain':val,'text/html':'<pre>' + val + '</pre>'})
                success = True
    except:
        pass

    if not success:
        print(val)
        sys.stdout.flush()
Exemple #54
0
def load_notebook(fullname: str):
    """Import a notebook as a module."""
    shell = InteractiveShell.instance()
    path = fullname

    # load the notebook object
    with open(path, 'r', encoding='utf-8') as f:
        notebook = read(f, 4)

    # create the module and add it to sys.modules
    mod = types.ModuleType(fullname)
    mod.__file__ = path
    # mod.__loader__ = self
    mod.__dict__['get_ipython'] = get_ipython
    sys.modules[fullname] = mod

    # extra work to ensure that magics that would affect the user_ns
    # actually affect the notebook module's ns
    save_user_ns = shell.user_ns
    shell.user_ns = mod.__dict__

    try:
        for cell in notebook.cells:
            if cell.cell_type == 'code':
                try:
                    # only run valid python code
                    ast.parse(cell.source)
                except SyntaxError:
                    continue
                try:
                    # pylint: disable=exec-used
                    exec(cell.source, mod.__dict__)
                except NameError:
                    print(cell.source)
                    raise
    finally:
        shell.user_ns = save_user_ns
    return mod
Exemple #55
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 = {'pdf', 'png', 'svg'}
        c.InlineBackend.quality = 90

    Parameters
    ----------
    *formats : list, tuple
        One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    quality : int
        A percentage for the quality of JPEG figures. Defaults to 90.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, quality=90)
Exemple #56
0
def create_notebook_with_imports(path, toplevel=True):
    """Create notebook that only has imports and return path"""
    try:
        with open(path) as fil:
            notebook = nbformat.read(fil, as_version=4)
        metadata = notebook["metadata"]
    except Exception as exc:  # pylint: disable=broad-except
        print("Failed to load notebook {}".format(exc))
        return (None, False)
    language_info = metadata.get("language_info", {})
    language = language_info.get("name", "unknown")
    if language == "python":
        shell = InteractiveShell.instance()
        for cell in notebook['cells']:
            if cell.get("cell_type") == "code":
                source = cell["source"] or ""
                cell["source"] = ""
                try:
                    source = shell.input_transformer_manager.transform_cell(
                        source)
                    if "\0" in source:
                        source = source.replace("\0", "\n")
                    tree = ast.parse(source)
                    visitor = ImportVisitor()
                    visitor.visit(tree)
                    cell["source"] = visitor.import_only_code(toplevel)
                except (IndentationError, SyntaxError):
                    pass
        try:
            with open(path + ".julimp", "w") as fil:
                nbformat.write(notebook, fil)
            return (path + ".julimp", True)
        except Exception as exc:  # pylint: disable=broad-except
            print("Failed to save notebook {}".format(exc))
            return (None, False)
    return (path, False)
Exemple #57
0
import os
from pathlib import Path
from IPython.core.alias import Alias
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.magic import MagicAlias
from IPython.utils.text import dedent, indent

shell = InteractiveShell.instance()
magics = shell.magics_manager.magics

def _strip_underline(line):
    chars = set(line.strip())
    if len(chars) == 1 and ('-' in chars or '=' in chars):
        return ""
    else:
        return line

def format_docstring(func):
    docstring = (func.__doc__ or "Undocumented").rstrip()
    docstring = indent(dedent(docstring))
    # Sphinx complains if indented bits have rst headings in, so strip out
    # any underlines in the docstring.
    lines = [_strip_underline(l) for l in docstring.splitlines()]
    return "\n".join(lines)

output = [
"Line magics",
"===========",
"",
]
 def __init__(self, path=None):
     self.shell = InteractiveShell.instance()
     self.path = path
Exemple #59
0
def create_inputhook_qt4(mgr, app=None):
    """Create an input hook for running the Qt4 application event loop.

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

    app : Qt Application, optional.
        Running application to use.  If not given, we probe Qt for an
        existing application object, and create a new one if none is found.

    Returns
    -------
    A pair consisting of a Qt Application (either the one given or the
    one found or created) and a inputhook.

    Notes
    -----
    We use a custom input hook instead of PyQt4's default one, as it
    interacts better with the readline packages (issue #481).

    The inputhook function works in tandem with a 'pre_prompt_hook'
    which automatically restores the hook as an inputhook in case the
    latter has been temporarily disabled after having intercepted a
    KeyboardInterrupt.
    """

    if app is None:
        app = QtCore.QCoreApplication.instance()
        if app is None:
            app = QtGui.QApplication([" "])

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

    # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of
    # hooks (they both share the got_kbdint flag)

    def inputhook_qt4():
        """PyOS_InputHook python hook for Qt4.

        Process pending Qt events and if there's no pending keyboard
        input, spend a short slice of time (50ms) running the Qt event
        loop.

        As a Python ctypes callback can't raise an exception, we catch
        the KeyboardInterrupt and temporarily deactivate the hook,
        which will let a *second* CTRL+C be processed normally and go
        back to a clean prompt line.
        """
        try:
            allow_CTRL_C()
            app = QtCore.QCoreApplication.instance()
            if not app:  # shouldn't happen, but safer if it happens anyway...
                return 0
            app.processEvents(QtCore.QEventLoop.AllEvents, 300)
            if not stdin_ready():
                # Generally a program would run QCoreApplication::exec()
                # from main() to enter and process the Qt event loop until
                # quit() or exit() is called and the program terminates.
                #
                # For our input hook integration, we need to repeatedly
                # enter and process the Qt event loop for only a short
                # amount of time (say 50ms) to ensure that Python stays
                # responsive to other user inputs.
                #
                # A naive approach would be to repeatedly call
                # QCoreApplication::exec(), using a timer to quit after a
                # short amount of time. Unfortunately, QCoreApplication
                # emits an aboutToQuit signal before stopping, which has
                # the undesirable effect of closing all modal windows.
                #
                # To work around this problem, we instead create a
                # QEventLoop and call QEventLoop::exec(). Other than
                # setting some state variables which do not seem to be
                # used anywhere, the only thing QCoreApplication adds is
                # the aboutToQuit signal which is precisely what we are
                # trying to avoid.
                timer = QtCore.QTimer()
                event_loop = QtCore.QEventLoop()
                timer.timeout.connect(event_loop.quit)
                while not stdin_ready():
                    timer.start(50)
                    event_loop.exec_()
                    timer.stop()
        except KeyboardInterrupt:
            global got_kbdint, sigint_timer

            ignore_CTRL_C()
            got_kbdint = True
            mgr.clear_inputhook()

            # This generates a second SIGINT so the user doesn't have to
            # press CTRL+C twice to get a clean prompt.
            #
            # Since we can't catch the resulting KeyboardInterrupt here
            # (because this is a ctypes callback), we use a timer to
            # generate the SIGINT after we leave this callback.
            #
            # Unfortunately this doesn't work on Windows (SIGINT kills
            # Python and CTRL_C_EVENT doesn't work).
            if os.name == "posix":
                pid = os.getpid()
                if not sigint_timer:
                    sigint_timer = threading.Timer(
                        0.01, os.kill, args=[pid, signal.SIGINT]
                    )
                    sigint_timer.start()
            else:
                print("\nKeyboardInterrupt - Ctrl-C again for new prompt")

        except:  # NO exceptions are allowed to escape from a ctypes callback
            ignore_CTRL_C()
            from traceback import print_exc

            print_exc()
            print("Got exception from inputhook_qt4, unregistering.")
            mgr.clear_inputhook()
        finally:
            allow_CTRL_C()
        return 0

    def preprompthook_qt4(ishell):
        """'pre_prompt_hook' used to restore the Qt4 input hook

        (in case the latter was temporarily deactivated after a
        CTRL+C)
        """
        global got_kbdint, sigint_timer

        if sigint_timer:
            sigint_timer.cancel()
            sigint_timer = None

        if got_kbdint:
            mgr.set_inputhook(inputhook_qt4)
        got_kbdint = False

    ip._inputhook_qt4 = inputhook_qt4
    ip.set_hook("pre_prompt_hook", preprompthook_qt4)

    return app, inputhook_qt4
Exemple #60
0
 def init_alias(self):
     # InteractiveShell defines alias's we want, but TerminalInteractiveShell defines
     # ones we don't. So don't use super and instead go right to InteractiveShell
     InteractiveShell.init_alias(self)