本文整理汇总了Python中IPython.core.interactiveshell.InteractiveShell.instance方法的典型用法代码示例。如果您正苦于以下问题:Python InteractiveShell.instance方法的具体用法?Python InteractiveShell.instance怎么用?Python InteractiveShell.instance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPython.core.interactiveshell.InteractiveShell
的用法示例。
在下文中一共展示了InteractiveShell.instance方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: append_display_data
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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
},
)
示例2: save_n_explore
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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))
示例3: setup_class
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
def setup_class(cls):
pytest.importorskip('IPython')
from IPython.core.interactiveshell import InteractiveShell
cls.display_formatter = InteractiveShell.instance().display_formatter
示例4: __init__
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
def __init__(self, path=None):
self.shell = InteractiveShell.instance()
self.path = path
示例5: ipython2python
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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)
示例6: clear_output
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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()
示例7: get_ipython
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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()
示例8: test_no_widget_view
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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)
示例9: shell
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
def shell():
from IPython.core.interactiveshell import InteractiveShell
shell = InteractiveShell.instance()
shell.magic("load_ext pylada")
return shell
示例10: load_notebook
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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
示例11: format_display_data
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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/latex
* application/json
* application/javascript
* 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
)
示例12: display
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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)
示例13: publish_display_data
# 需要导入模块: from IPython.core.interactiveshell import InteractiveShell [as 别名]
# 或者: from IPython.core.interactiveshell.InteractiveShell import instance [as 别名]
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/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
)