Add is_in_jupyter() helper for displaCy (see #1058)

This commit is contained in:
ines 2017-05-18 14:13:14 +02:00
parent abf0188b0a
commit 489d2fb4ba
2 changed files with 17 additions and 2 deletions

View File

@ -3,13 +3,14 @@ from __future__ import unicode_literals
from .render import DependencyRenderer, EntityRenderer
from ..tokens import Doc
from ..util import prints
from ..util import prints, is_in_jupyter
_html = {}
IS_JUPYTER = is_in_jupyter()
def render(docs, style='dep', page=False, minify=False, jupyter=False, options={}):
def render(docs, style='dep', page=False, minify=False, jupyter=IS_JUPYTER, options={}):
"""Render displaCy visualisation.
docs (list or Doc): Document(s) to visualise.

View File

@ -142,6 +142,20 @@ def parse_package_meta(package_path, require=True):
return None
def is_in_jupyter():
"""Check if user is in a Jupyter notebook. Mainly used for displaCy.
RETURNS (bool): True if in Jupyter, False if not.
"""
try:
cfg = get_ipython().config
if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook':
return True
except NameError:
return False
return False
def get_cuda_stream(require=False):
# TODO: Error and tell to install chainer if not found
# Requires GPU