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
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 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 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 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( 'graphlab.callback', { 'text/plain': val, 'text/html': '<pre>' + val + '</pre>' }) success = True except: pass if not success: print val sys.stdout.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()
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)
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)
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 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)
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)
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)
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()
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()
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)
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')
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')
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
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
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)
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 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('graphlab.callback', {'text/plain':val,'text/html':'<pre>' + val + '</pre>'}) success = True except: pass if not success: print val sys.stdout.flush()
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. """ global __LAZY_HAVE_IPYTHON if __LAZY_HAVE_IPYTHON == 2: try: import IPython from IPython.core.interactiveshell import InteractiveShell __LAZY_HAVE_IPYTHON = 1 except ImportError: __LAZY_HAVE_IPYTHON = 0 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 __LAZY_HAVE_IPYTHON == 1: 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()
def show(self): if InteractiveShell.initialized(): clear_output() display(self.fig) else: plt.show()
def display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **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. In terminal IPython this will be similar to using :func:`print`, for use in richer frontends see Jupyter notebook examples with rich display logic. 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, tuple or set, 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, tuple or set, 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. transient : dict, optional A dictionary of transient data to associate with the output. Data in this dict should not be persisted to files (e.g. notebooks). display_id : str, bool optional Set an id for the display. This id can be used for updating this display area later via update_display. If given as `True`, generate a new `display_id` kwargs: additional keyword-args, optional Additional keyword-arguments are passed through to the display publisher. Returns ------- handle: DisplayHandle Returns a handle on updatable displays for use with :func:`update_display`, if `display_id` is given. Returns :any:`None` if no `display_id` is given (default). Examples -------- >>> class Json(object): ... def __init__(self, json): ... self.json = json ... def _repr_pretty_(self, pp, cycle): ... import json ... pp.text(json.dumps(self.json, indent=2)) ... def __repr__(self): ... return str(self.json) ... >>> d = Json({1:2, 3: {4:5}}) >>> print(d) {1: 2, 3: {4: 5}} >>> display(d) { "1": 2, "3": { "4": 5 } } >>> def int_formatter(integer, pp, cycle): ... pp.text('I'*integer) >>> plain = get_ipython().display_formatter.formatters['text/plain'] >>> plain.for_type(int, int_formatter) <function _repr_pprint at 0x...> >>> display(7-5) II >>> del plain.type_printers[int] >>> display(7-5) 2 See Also -------- :func:`update_display` Notes ----- In Python, objects can declare their textual representation using the `__repr__` method. IPython expands on this idea and allows objects to declare other, rich representations including: - HTML - JSON - PNG - JPEG - SVG - LaTeX A single object can declare some or all of these representations; all are handled by IPython's display system. The main idea of the first approach is that you have to implement special display methods when you define your class, one for each representation you want to use. Here is a list of the names of the special methods and the values they must return: - `_repr_html_`: return raw HTML as a string - `_repr_json_`: return a JSONable dict - `_repr_jpeg_`: return raw JPEG data - `_repr_png_`: return raw PNG data - `_repr_svg_`: return raw SVG data as a string - `_repr_latex_`: return LaTeX commands in a string surrounded by "$". - `_repr_mimebundle_`: return a full mimebundle containing the mapping from all mimetypes to data. Use this for any mime-type not listed above. When you are directly writing your own classes, you can adapt them for display in IPython by following the above approach. But in practice, you often need to work with existing classes that you can't easily modify. You can refer to the documentation on IPython display formatters in order to register custom formatters for already existing types. .. versionadded:: 5.4 display available without import .. versionadded:: 6.1 display available without import Since IPython 5.4 and 6.1 :func:`display` is automatically made available to the user without import. If you are using display in a document that might be used in a pure python context or with older version of IPython, use the following import at the top of your file:: from IPython.display import display """ from IPython.core.interactiveshell import InteractiveShell if not InteractiveShell.initialized(): # Directly print objects. print(*objs) return raw = kwargs.pop('raw', False) if transient is None: transient = {} if metadata is None: metadata={} if display_id: if display_id is True: display_id = _new_id() transient['display_id'] = display_id if kwargs.get('update') and 'display_id' not in transient: raise TypeError('display_id required for update_display') if transient: kwargs['transient'] = transient if not raw: format = InteractiveShell.instance().display_formatter.format for obj in objs: if raw: publish_display_data(data=obj, metadata=metadata, **kwargs) else: format_dict, md_dict = format(obj, include=include, exclude=exclude) if not format_dict: # nothing to display (e.g. _ipython_display_ took over) continue if metadata: # kwarg-specified metadata gets precedence _merge(md_dict, metadata) publish_display_data(data=format_dict, metadata=md_dict, **kwargs) if display_id: return DisplayHandle(display_id)
import matplotlib as mpl from IPython.core.interactiveshell import InteractiveShell from IPython import get_ipython from pathlib import Path # ------------------------------------------------------------------ # Check the environment for plotting # ------------------------------------------------------------------ # Do we run in IPython ? IN_IPYTHON = False KERNEL = None IP = None if InteractiveShell.initialized(): # pragma: no cover IN_IPYTHON = True IP = get_ipython() KERNEL = getattr(IP, "kernel", None) NO_DISPLAY = False NO_DIALOG = False # Are we buildings the docs ? if Path(sys.argv[0]).name in ["make.py", "validate_docstrings.py"]: # pragma: no cover # if we are building the documentation, in principle it should be done # using the make.py located at the root of the spectrochempy package. NO_DISPLAY = True NO_DIALOG = True mpl.use("agg", force=True)
# Software is furnished to do so, subject to the following conditions: # # # # The above copyright notice and this permission notice shall be included in # # all copies or substantial portions of the Software. # # # # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # # DEALINGS IN THE SOFTWARE. # # # ############################################################################### """ This module contains only a display() function, which, if you are running in the IPython --pylab mode, will render things inline in pretty SVG or PNG graphics. If you are NOT running in IPython --pylab mode, it will do nothing. """ try: from IPython.core.interactiveshell import InteractiveShell except ImportError: def display(obj): pass else: if InteractiveShell.initialized(): from IPython.core.display import display else: def display(obj): pass
# all copies or substantial portions of the Software. # # # # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # # DEALINGS IN THE SOFTWARE. # # # ############################################################################### """ This module contains only a display() function, which, if you are running in the IPython --pylab mode, will render things inline in pretty SVG or PNG graphics. If you are NOT running in IPython --pylab mode, it will do nothing. """ try: from IPython.core.interactiveshell import InteractiveShell except ImportError: def display(obj): pass else: if InteractiveShell.initialized(): from IPython.core.display import display else: def display(obj): pass
def initialize(self, argv=None): """ Initialisation function for the API applications Parameters ---------- argv : List, [optional]. List of configuration parameters. """ # parse the argv # -------------------------------------------------------------------- # if we are running this under ipython and jupyter notebooks # deactivate potential command line arguments # (such that those from jupyter which cause problems here) IN_IPYTHON = False if InteractiveShell.initialized(): IN_IPYTHON = True if not IN_IPYTHON: # remove argument not known by spectrochempy if 'make.py' in sys.argv[0] or 'pytest' in sys.argv[ 0]: # building docs options = [] for item in sys.argv[:]: for k in list(self.flags.keys()): if item.startswith("--" + k) or k in ['--help', '--help-all']: options.append(item) continue for k in list(self.aliases.keys()): if item.startswith("-" + k) or k in [ 'h', ]: options.append(item) self.parse_command_line(options) else: self.parse_command_line(sys.argv) # Get preferences from the config file and init everything # --------------------------------------------------------------------- self._init_all_preferences() # we catch warnings and error for a ligther display to the end-user. # except if we are in debugging mode # warning handler # -------------------------------------------------------------------- def send_warnings_to_log(message, category): self.logs.warning(f'{category.__name__} - {message}') return warnings.showwarning = send_warnings_to_log # exception handler # -------------------------------------------------------------------- if IN_IPYTHON: ip = get_ipython() def _custom_exc(shell, etype, evalue, tb, tb_offset=None): if self.log_level == logging.DEBUG: shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset) else: self.logs.error(f"{etype.__name__}: {evalue}") ip.set_custom_exc((Exception, ), _custom_exc) # load our custom magic extensions # -------------------------------------------------------------------- if ip is not None: ip.register_magics(SpectroChemPyMagics)
def display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **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. In terminal IPython this will be similar to using :func:`print`, for use in richer frontends see Jupyter notebook examples with rich display logic. 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, tuple or set, 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, tuple or set, 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. transient : dict, optional A dictionary of transient data to associate with the output. Data in this dict should not be persisted to files (e.g. notebooks). display_id : str, bool optional Set an id for the display. This id can be used for updating this display area later via update_display. If given as `True`, generate a new `display_id` kwargs: additional keyword-args, optional Additional keyword-arguments are passed through to the display publisher. Returns ------- handle: DisplayHandle Returns a handle on updatable displays for use with :func:`update_display`, if `display_id` is given. Returns :any:`None` if no `display_id` is given (default). Examples -------- >>> class Json(object): ... def __init__(self, json): ... self.json = json ... def _repr_pretty_(self, pp, cycle): ... import json ... pp.text(json.dumps(self.json, indent=2)) ... def __repr__(self): ... return str(self.json) ... >>> d = Json({1:2, 3: {4:5}}) >>> print(d) {1: 2, 3: {4: 5}} >>> display(d) { "1": 2, "3": { "4": 5 } } >>> def int_formatter(integer, pp, cycle): ... pp.text('I'*integer) >>> plain = get_ipython().display_formatter.formatters['text/plain'] >>> plain.for_type(int, int_formatter) <function _repr_pprint at 0x...> >>> display(7-5) II >>> del plain.type_printers[int] >>> display(7-5) 2 See Also -------- :func:`update_display` Notes ----- In Python, objects can declare their textual representation using the `__repr__` method. IPython expands on this idea and allows objects to declare other, rich representations including: - HTML - JSON - PNG - JPEG - SVG - LaTeX A single object can declare some or all of these representations; all are handled by IPython's display system. The main idea of the first approach is that you have to implement special display methods when you define your class, one for each representation you want to use. Here is a list of the names of the special methods and the values they must return: - `_repr_html_`: return raw HTML as a string, or a tuple (see below). - `_repr_json_`: return a JSONable dict, or a tuple (see below). - `_repr_jpeg_`: return raw JPEG data, or a tuple (see below). - `_repr_png_`: return raw PNG data, or a tuple (see below). - `_repr_svg_`: return raw SVG data as a string, or a tuple (see below). - `_repr_latex_`: return LaTeX commands in a string surrounded by "$", or a tuple (see below). - `_repr_mimebundle_`: return a full mimebundle containing the mapping from all mimetypes to data. Use this for any mime-type not listed above. The above functions may also return the object's metadata alonside the data. If the metadata is available, the functions will return a tuple containing the data and metadata, in that order. If there is no metadata available, then the functions will return the data only. When you are directly writing your own classes, you can adapt them for display in IPython by following the above approach. But in practice, you often need to work with existing classes that you can't easily modify. You can refer to the documentation on integrating with the display system in order to register custom formatters for already existing types (:ref:`integrating_rich_display`). .. versionadded:: 5.4 display available without import .. versionadded:: 6.1 display available without import Since IPython 5.4 and 6.1 :func:`display` is automatically made available to the user without import. If you are using display in a document that might be used in a pure python context or with older version of IPython, use the following import at the top of your file:: from IPython.display import display """ from IPython.core.interactiveshell import InteractiveShell if not InteractiveShell.initialized(): # Directly print objects. print(*objs) return raw = kwargs.pop('raw', False) if transient is None: transient = {} if metadata is None: metadata={} if display_id: if display_id is True: display_id = _new_id() transient['display_id'] = display_id if kwargs.get('update') and 'display_id' not in transient: raise TypeError('display_id required for update_display') if transient: kwargs['transient'] = transient if not raw: format = InteractiveShell.instance().display_formatter.format for obj in objs: if raw: publish_display_data(data=obj, metadata=metadata, **kwargs) else: format_dict, md_dict = format(obj, include=include, exclude=exclude) if not format_dict: # nothing to display (e.g. _ipython_display_ took over) continue if metadata: # kwarg-specified metadata gets precedence _merge(md_dict, metadata) publish_display_data(data=format_dict, metadata=md_dict, **kwargs) if display_id: return DisplayHandle(display_id)