1

I am using Spacy to visualize NERs in a notebook as follows:

import spacy
from spacy import displacy

text = "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously."

nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
displacy.render(doc, style="ent")

(This is the example in the spacy documentation)

result: enter image description here

So far so good.

The question is if it is possible to access the HTML object itself, not just rendering it, in order to add functionality via HTML to it. More precisely what I am looking for is a way to fire a function when clicking everyone of those "buttons". this work around I am looking for is meant to work in a jupyter hub, i.e. Jupyter lab, so solutions using any webframe like streamlit or flask or whatever that can be run in a local server are out of scope.

thanks.

EDIT 1: If you use the following code in a Jupyter lab you can get the plain HTML:

displacy.render(doc, style="ent", jupyter'False'))

you would get:

'<div class="entities" style="line-height: 2.5; direction: ltr">When \n<mark class="entity" style="background: ....... etc....</div>'

The idea is transforming the buttons:

enter image description here

...into clickable buttons.

I guess the way to go is parse the HTML add clickable button and do "something" with ipywidgets jslink or so.

JFerro
  • 3,203
  • 7
  • 35
  • 88

1 Answers1

1

You can use ipywidgets:

from ipywidgets import interact_manual
  
@interact_manual  
def html():
    path="file_name.html"
    Html_file= open(path,"w")
    Html_file.write(displacy.render(doc, style="ent"))
    Html_file.close()

This will save the output as a html file when you press the button.

NicolasPeruchot
  • 439
  • 5
  • 9
  • Thanks @NicolasPeruchot, but I was meaning something else. I would like to convert any of the NER markups in the text displayed into a button. – JFerro Sep 30 '21 at 18:22