Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ddenz opened this issue Nov 20, 2023 · 1 comment
Open

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

ddenz opened this issue Nov 20, 2023 · 1 comment

Comments

@ddenz
Copy link

ddenz commented Nov 20, 2023

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"}}
)
@ddenz ddenz changed the title Displacy HTML object generation failing Displacy HTML render failing Nov 20, 2023
@ddenz ddenz changed the title Displacy HTML render failing No HTML object returned by displacy.render() Nov 20, 2023
@adrianeboyd
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants