Python IPython.get_ipython() Examples

The following are 30 code examples of IPython.get_ipython(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module IPython , or try the search function .
Example #1
Source File: gcloud.py    From PySyft with Apache License 2.0 7 votes vote down vote up
def __init__(self, credentials, project_id, region):
        """
        args:
            credentials: Path to the credentials json file
            project_id: project_id of your project in GCP
            region: region of your GCP project
        """
        self.credentials = credentials
        self.project_id = project_id
        self.region = region
        self.provider = "google"
        self.config = terrascript.Terrascript()
        self.config += terrascript.provider.google(
            credentials=self.credentials, project=self.project_id, region=self.region
        )
        with open("main.tf.json", "w") as main_config:
            json.dump(self.config, main_config, indent=2, sort_keys=False)

        if IPython.get_ipython():
            terraform_notebook.init()
        else:
            terraform_script.init() 
Example #2
Source File: magics.py    From omniduct with MIT License 7 votes vote down vote up
def _process_line_arguments(line_arguments):
    from IPython import get_ipython
    args = []
    kwargs = {}
    reached_kwargs = False
    for arg in line_arguments.split():
        if '=' in arg:
            reached_kwargs = True
            key, value = arg.split('=')
            value = eval(value, get_ipython().user_ns)
            if key in kwargs:
                raise ValueError('Duplicate keyword argument `{}`.'.format(key))
            kwargs[key] = value
        else:
            if reached_kwargs:
                raise ValueError('Positional argument `{}` after keyword argument.'.format(arg))
            args.append(arg)
    return args, kwargs 
Example #3
Source File: pipeline_tests.py    From pydatalab with Apache License 2.0 6 votes vote down vote up
def test_create_cell_debug(self, mock_default_context, mock_notebook_environment):
    env = {}
    mock_default_context.return_value = TestCases._create_context()
    mock_notebook_environment.return_value = env
    IPython.get_ipython().user_ns = env

    # cell output is empty when debug is True
    output = google.datalab.contrib.pipeline.commands._pipeline._create_cell(
        {'name': 'foo_pipeline', 'debug': True}, self.sample_cell_body)
    self.assertTrue(len(output) > 0)

    output = google.datalab.contrib.pipeline.commands._pipeline._create_cell(
        {'name': 'foo_pipeline', 'debug': False}, self.sample_cell_body)
    self.assertTrue(output is None)

    output = google.datalab.contrib.pipeline.commands._pipeline._create_cell(
        {'name': 'foo_pipeline'}, self.sample_cell_body)
    self.assertTrue(output is None) 
Example #4
Source File: completerlib.py    From Computable with MIT License 6 votes vote down vote up
def quick_completer(cmd, completions):
    """ Easily create a trivial completer for a command.

    Takes either a list of completions, or all completions in string (that will
    be split on whitespace).

    Example::

        [d:\ipython]|1> import ipy_completers
        [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
        [d:\ipython]|3> foo b<TAB>
        bar baz
        [d:\ipython]|3> foo ba
    """

    if isinstance(completions, basestring):
        completions = completions.split()

    def do_complete(self, event):
        return completions

    get_ipython().set_hook('complete_command',do_complete, str_key = cmd) 
Example #5
Source File: ipython.py    From xcube with MIT License 6 votes vote down vote up
def register_json_formatter(cls: Type, to_dict_method_name: str = 'to_dict'):
    """
    TODO
    :param cls:
    :param to_dict_method_name:
    :return:
    """
    if not hasattr(cls, to_dict_method_name) or not callable(getattr(cls, to_dict_method_name)):
        raise ValueError(f'{cls} must define a {to_dict_method_name}() method')

    try:
        import IPython
        import IPython.display

        if IPython.get_ipython() is not None:
            def obj_to_dict(obj):
                return getattr(obj, to_dict_method_name)()

            ipy_formatter = IPython.get_ipython().display_formatter.formatters['application/json']
            ipy_formatter.for_type(cls, obj_to_dict)

    except ImportError:
        pass 
Example #6
Source File: gcloud.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def compute_instance(self, name, machine_type, zone, image_family, apply=True):
        """
        args:
            name: name of the compute instance
            machine_type: the type of machine
            zone: zone of your GCP project
            image_family: image of the OS
            apply: to call terraform apply at the end
        """
        self.config += terrascript.resource.google_compute_instance(
            name,
            name=name,
            machine_type=machine_type,
            zone=zone,
            boot_disk={"initialize_params": {"image": image_family}},
            network_interface={"network": "default", "access_config": {}},
        )
        with open("main.tf.json", "w") as main_config:
            json.dump(self.config, main_config, indent=2, sort_keys=False)

        if apply:
            if IPython.get_ipython():
                terraform_notebook.apply()
            else:
                terraform_script.apply() 
Example #7
Source File: _csv.py    From pydatalab with Apache License 2.0 6 votes vote down vote up
def _view(args, cell):
  csv = datalab.data.Csv(args['input'])
  num_lines = int(args['count'] or 5)
  headers = None
  if cell:
    ipy = IPython.get_ipython()
    config = _utils.parse_config(cell, ipy.user_ns)
    if 'columns' in config:
      headers = [e.strip() for e in config['columns'].split(',')]
  df = pd.DataFrame(csv.browse(num_lines, headers))
  if args['profile']:
    # TODO(gram): We need to generate a schema and type-convert the columns before this
    # will be useful for CSV
    return _utils.profile_df(df)
  else:
    return IPython.core.display.HTML(df.to_html(index=False)) 
Example #8
Source File: base.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_qt_app():
    app = None

    if in_ipython():
        from IPython import get_ipython
        ipython = get_ipython()
        ipython.magic('gui qt')

        from IPython.external.qt_for_kernel import QtGui
        app = QtGui.QApplication.instance()

    if app is None:
        from PyQt5.QtWidgets import QApplication
        app = QApplication.instance()
        if not app:
            app = QApplication([''])

    return app 
Example #9
Source File: __init__.py    From stackprinter with MIT License 6 votes vote down vote up
def _patch_ipython_excepthook(**kwargs):
    """ Replace ipython's built-in traceback printer, excellent though it is"""
    global ipy_tb

    blacklist = kwargs.get('suppressed_paths', [])
    blacklist.append('site-packages/IPython/')
    kwargs['suppressed_paths'] = blacklist

    if 'file' in kwargs:
        del kwargs['file']

    def format_tb(*exc_tuple, **__):
        unstructured_tb = format(exc_tuple, **kwargs)
        structured_tb = [unstructured_tb]  # \*coughs*
        return structured_tb

    import IPython
    shell = IPython.get_ipython()
    if ipy_tb is None:
        ipy_tb = shell.InteractiveTB.structured_traceback
    shell.InteractiveTB.structured_traceback = format_tb 
Example #10
Source File: ipy_completer.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def load_ipython_extension(ip=None):
    """ Load completer function into IPython """
    if ip is None:
        ip = get_ipython()
    ip.set_hook('complete_command', h5py_completer, re_key=r"(?:.*\=)?(.+?)\[") 
Example #11
Source File: shell.py    From insights-core with Apache License 2.0 5 votes vote down vote up
def reset_requested(self):
        """ Reset requested state so you can work on a new rule. """
        IPython.get_ipython().history_manager.reset()
        self._requested.clear() 
Example #12
Source File: views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def in_ipython_frontend():
    """
    Helper function which is variation of :meth:`pandas:pandas.io.formats.console.in_ipython_frontend` which
    checks to see if we are inside an IPython zmq frontend

    :return: `True` if D-Tale is being invoked within ipython notebook, `False` otherwise
    """
    try:
        from IPython import get_ipython

        ip = get_ipython()
        return "zmq" in str(type(ip)).lower()
    except BaseException:
        pass
    return False 
Example #13
Source File: __init__.py    From pySINDy with MIT License 5 votes vote down vote up
def _handle_ipython():
    """Register with the comm target at import if running in IPython"""
    ip = get_ipython()
    if ip is None:
        return
    load_ipython_extension(ip) 
Example #14
Source File: __init__.py    From pySINDy with MIT License 5 votes vote down vote up
def register_comm_target(kernel=None):
    """Register the jupyter.widget comm target"""
    if kernel is None:
        kernel = get_ipython().kernel
    kernel.comm_manager.register_target('jupyter.widget', Widget.handle_comm_opened)

# deprecated alias 
Example #15
Source File: __init__.py    From stackprinter with MIT License 5 votes vote down vote up
def _unpatch_ipython_excepthook():
    """ restore proper order in Ipython """
    import IPython
    shell = IPython.get_ipython()
    if ipy_tb is not None:
        shell.InteractiveTB.structured_traceback = ipy_tb 
Example #16
Source File: jupyter_magics.py    From qiskit-terra with Apache License 2.0 5 votes vote down vote up
def circuit_library_info(circuit: qiskit.QuantumCircuit) -> None:
        """Displays library information for a quantum circuit.

        Args:
            circuit: Input quantum circuit.
        """
        shell = get_ipython()
        circ = shell.ev(circuit)
        circuit_library_widget(circ) 
Example #17
Source File: widget_output.py    From pySINDy with MIT License 5 votes vote down vote up
def __enter__(self):
        """Called upon entering output widget context manager."""
        self._flush()
        ip = get_ipython()
        if ip and hasattr(ip, 'kernel') and hasattr(ip.kernel, '_parent_header'):
            self.msg_id = ip.kernel._parent_header['header']['msg_id'] 
Example #18
Source File: __init__.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def unload_ipython_extension(shell):
  _shell.InteractiveShell.run_cell_magic = _orig_run_cell_magic
  _shell.InteractiveShell.run_line_magic = _orig_run_line_magic
  _requests.Session.__init__ = _orig_init
  _httplib2.Http.request = _orig_request
  try:
    del _IPython.get_ipython().user_ns['project_id']
    del _IPython.get_ipython().user_ns['set_project_id']
  except Exception:
    pass  # We mock IPython for tests so we need this.
  # TODO(gram): unregister imports/magics/etc. 
Example #19
Source File: _commands.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def parse(self, line, namespace=None):
    """Parses a line into a dictionary of arguments, expanding meta-variables from a namespace. """
    try:
      if namespace is None:
        ipy = IPython.get_ipython()
        namespace = ipy.user_ns
      args = CommandParser.create_args(line, namespace)
      return self.parse_args(args)
    except Exception as e:
      print(str(e))
      return None 
Example #20
Source File: _chart.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _chart_cell(args, cell):
  source = args['data']
  ipy = IPython.get_ipython()
  chart_options = _utils.parse_config(cell, ipy.user_ns)
  if chart_options is None:
    chart_options = {}
  elif not isinstance(chart_options, dict):
    raise Exception("Could not parse chart options")
  chart_type = args['chart']
  fields = args['fields'] if args['fields'] else '*'
  return IPython.core.display.HTML(_utils.chart_html('gcharts', chart_type, source=source,
                                                     chart_options=chart_options, fields=fields)) 
Example #21
Source File: _bigquery.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _query_cell(args, cell_body):
  """Implements the BigQuery cell magic for used to build SQL objects.

  The supported syntax is:

      %%bq query <args>
      [<inline SQL>]

  Args:
    args: the optional arguments following '%%bql query'.
    cell_body: the contents of the cell
  """
  name = args['name']
  udfs = args['udfs']
  datasources = args['datasources']
  subqueries = args['subqueries']

  # Finally build the query object
  query = bigquery.Query(cell_body, env=IPython.get_ipython().user_ns, udfs=udfs,
                         data_sources=datasources, subqueries=subqueries)

  # if no name is specified, execute this query instead of defining it
  if name is None:
    return query.execute().result()
  else:
    google.datalab.utils.commands.notebook_environment()[name] = query 
Example #22
Source File: _utils.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def notebook_environment():
  """ Get the IPython user namespace. """
  ipy = IPython.get_ipython()
  return ipy.user_ns 
Example #23
Source File: _modules.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _pymodule_cell(args, cell):
  if cell is None:
      raise Exception('The code for the module must be included')

  name = args['name']
  module = _create_python_module(name, cell)

  # Automatically import the newly created module by assigning it to a variable
  # named the same name as the module name.
  ipy = IPython.get_ipython()
  ipy.push({name: module}) 
Example #24
Source File: _bigquery.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _register_html_formatters():
  try:
    # The full module paths need to be specified in the type name lookup
    ipy = IPython.get_ipython()
    html_formatter = ipy.display_formatter.formatters['text/html']

    html_formatter.for_type_by_name('google.datalab.bigquery._query', 'Query', _repr_html_query)
    html_formatter.for_type_by_name('google.datalab.bigquery._query_results_table',
                                    'QueryResultsTable', _repr_html_query_results_table)
    html_formatter.for_type_by_name('google.datalab.bigquery._table', 'Table', _repr_html_table)
    html_formatter.for_type_by_name('google.datalab.bigquery._schema', 'Schema',
                                    _repr_html_table_schema)
  except TypeError:
    # For when running unit tests
    pass 
Example #25
Source File: _utils.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def notebook_environment():
  """ Get the IPython user namespace. """
  ipy = IPython.get_ipython()
  return ipy.user_ns 
Example #26
Source File: _chart.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _chart_cell(args, cell):
  source = args['data']
  ipy = IPython.get_ipython()
  chart_options = _utils.parse_config(cell, ipy.user_ns)
  if chart_options is None:
    chart_options = {}
  elif not isinstance(chart_options, dict):
    raise Exception("Could not parse chart options")
  chart_type = args['chart']
  fields = args['fields'] if args['fields'] else '*'
  return IPython.core.display.HTML(_utils.chart_html('gcharts', chart_type, source=source,
                                                     chart_options=chart_options, fields=fields)) 
Example #27
Source File: _sql.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def sql_cell(args, cell):
  """Implements the SQL cell magic for ipython notebooks.

  The supported syntax is:

      %%sql [--module <modulename>]
      [<optional Python code for default argument values>]
      [<optional named queries>]
      [<optional unnamed query>]

  At least one query should be present. Named queries should start with:

      DEFINE QUERY <name>

  on a line by itself.

  Args:
    args: the optional arguments following '%%sql'.
    cell: the contents of the cell; Python code for arguments followed by SQL queries.
  """
  name = args['module'] if args['module'] else '_sql_cell'
  module = imp.new_module(name)
  query = _split_cell(cell, module)
  ipy = IPython.get_ipython()
  if not args['module']:
      # Execute now
      if query:
        return datalab.bigquery.Query(query, values=ipy.user_ns) \
          .execute(dialect=args['dialect'], billing_tier=args['billing']).results
  else:
    # Add it as a module
    sys.modules[name] = module
    exec('import %s' % name, ipy.user_ns) 
Example #28
Source File: __init__.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def unload_ipython_extension(shell):
  _shell.InteractiveShell.run_cell_magic = _orig_run_cell_magic
  _shell.InteractiveShell.run_line_magic = _orig_run_line_magic
  _requests.Session.__init__ = _orig_init
  _httplib2.Http.request = _orig_request
  try:
    del _IPython.get_ipython().user_ns['project_id']
    del _IPython.get_ipython().user_ns['set_project_id']
  except Exception:
    pass  # We mock IPython for tests so we need this.

  # TODO(gram): unregister imports/magics/etc. 
Example #29
Source File: _storage.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _gcs_read(args, _):
  contents = _get_object_contents(args['object'])
  ipy = IPython.get_ipython()
  ipy.push({args['variable']: contents}) 
Example #30
Source File: _bigquery.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def _register_html_formatters():
  try:
    ipy = IPython.get_ipython()
    html_formatter = ipy.display_formatter.formatters['text/html']

    html_formatter.for_type_by_name('datalab.bigquery._query', 'Query', _repr_html_query)
    html_formatter.for_type_by_name('datalab.bigquery._query_results_table', 'QueryResultsTable',
                                    _repr_html_query_results_table)
    html_formatter.for_type_by_name('datalab.bigquery._table', 'Table', _repr_html_table)
    html_formatter.for_type_by_name('datalab.bigquery._schema', 'Schema', _repr_html_table_schema)
  except TypeError:
    # For when running unit tests
    pass