Unable to use displacy(spacy lib) in VS Code

244 Views Asked by At

Nothing shows up on terminal while using displacy in V.S Code I have created a on virtual env in VS code without jupyter

I have tried both the .serve and .render methods .serve method redirects me to a external browser Site not available msg keeps showing code for .serve

import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a sentence.")
displacy.serve(doc, style="dep")

while render method just doesn't display anything

(Is there a way to do this without creating a env in/using jupyter notebook)

1

There are 1 best solutions below

2
Kartoos On BEST ANSWER

Your code sample works perfectly in my local machine. Couple of points that could help you:

  • Check the console if you are able to run it properly, After running your code, In my case my console/terminal was was showing:
Using the 'dep' visualizer
Serving on http://0.0.0.0:3000 ...
  • If you run into issues like port is already in use, then you can explicitly specify the port. For example I specified 3000 port to use:
import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a sentence.")
displacy.serve(doc, style="dep", port=3000) # port=3000 specifies the port
  • You can access via browser by visiting http://0.0.0.0:3000 or http://localhost:3000 (sometimes localhost works while 0.0.0.0 may not)