explosion / spacy-streamlit

đź‘‘ spaCy building blocks and visualizers for Streamlit apps
https://share.streamlit.io/ines/spacy-streamlit-demo/master/app.py
MIT License
804 stars 115 forks source link

No HTML object returned by displacy.render() #52

Open ddenz opened 11 months ago

ddenz commented 11 months ago

Running the set examples in Jupyter notebook causes:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[47], line 2
      1 doc = nlp('The tenants have mobility issues and cannot see well.')
----> 2 spacy_streamlit.visualize_spans(doc, 
      3                                 spans_key='sc', 
      4                                 displacy_options={'colors': {"MOBILITY":'#09a3d5'}}, 
      5                                 show_table=False)

File ~\.conda\envs\prodigy\lib\site-packages\spacy_streamlit\visualizer.py:326, in visualize_spans(doc, spans_key, attrs, show_table, title, manual, displacy_options)
    317         st.warning(
    318             "When the parameter 'manual' is set to True, the parameter 'doc' must be of type 'Dict', not 'spacy.tokens.Doc'."
    319         )
    320 html = displacy.render(
    321     doc,
    322     style="span",
    323     options=displacy_options,
    324     manual=manual,
    325 )
--> 326 st.write(f"{get_html(html)}", unsafe_allow_html=True)
    328 if show_table:
    329     data = [
    330         [str(getattr(span, attr)) for attr in attrs]
    331         for span in doc.spans[spans_key]
    332     ]

File ~\.conda\envs\prodigy\lib\site-packages\spacy_streamlit\util.py:30, in get_html(html)
     28 WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
     29 # Newlines seem to mess with the rendering
---> 30 html = html.replace("\n", " ")
     31 return WRAPPER.format(html)

AttributeError: 'NoneType' object has no attribute 'replace'

Setup: Windows Server 2019 Standard Jupyter 1.0.0 spaCy version 3.6.1 spacy-streamlit 1.0.6

Code:

import spacy_streamlit
import streamlit as st

import spacy
from spacy_streamlit import visualize_spans

nlp = spacy.load("en_core_web_sm")
doc = nlp("Sundar Pichai is the CEO of Google.")
span = doc[4:7]  # CEO of Google
span.label_ = "CEO"
doc.spans["job_role"] = [span]
visualize_spans(
    doc, spans_key="job_role", displacy_options={"colors": {"CEO": "#09a3d5"}}
)
adrianeboyd commented 11 months ago

The spacy-streamlit examples are intended to be run outside a notebook with

streamlit run ...

The concrete problem above is that displacy.render auto-detects that you're in a jupyter notebook and displays the output directly instead of returning HTML. However if you turn the auto-detection off, the result is no output because this code just isn't intended to be run within a notebook.

I don't know how stable it is, but you might try https://github.com/ddobrinskiy/streamlit-jupyter or look for similar libraries?