From a04550605a5dc4aa8126cf468e179392e322a6e5 Mon Sep 17 00:00:00 2001 From: ines Date: Sun, 14 May 2017 18:39:01 +0200 Subject: [PATCH] Add Jupyter notebook support (see #1058) --- spacy/displacy/__init__.py | 8 ++++++-- spacy/displacy/templates.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spacy/displacy/__init__.py b/spacy/displacy/__init__.py index f5216ccfc..e84ba376d 100644 --- a/spacy/displacy/__init__.py +++ b/spacy/displacy/__init__.py @@ -28,8 +28,12 @@ def render(docs, style='dep', page=False, minify=False, jupyter=False, options={ elif style is 'ent': renderer = EntityRenderer(options=options) parsed = [parse_ents(doc, options) for doc in docs] - _html['parsed'] = renderer.render(parsed, page=page, minify=minify) - return _html['parsed'] + _html['parsed'] = renderer.render(parsed, page=page, minify=minify).strip() + html = _html['parsed'] + if jupyter: # return HTML rendered by IPython display() + from IPython.core.display import display, HTML + return display(HTML(html)) + return html def serve(docs, style='dep', page=True, minify=False, options={}, port=5000): diff --git a/spacy/displacy/templates.py b/spacy/displacy/templates.py index 5cbcba98d..bdbdee459 100644 --- a/spacy/displacy/templates.py +++ b/spacy/displacy/templates.py @@ -2,8 +2,11 @@ from __future__ import unicode_literals +# setting explicit height and max-width: none on the SVG is required for +# Jupyter to render it properly in a cell + TPL_DEP_SVG = """ -{content} +{content} """