How to fix the PyVis Knowledge Graph error: AttributeError: 'NoneType' object has no attribute 'render'

If you’re trying to build a knowledge graph, you might be using PyVis, which is a fast way to generate visual network graphs using Python. You can use PyVis in Jupyter Notebooks, locally, or in my case, generate the graphs as HTML files and then visualize those.

❌ The Problem

If you’re trying to run your code, you might run into a number of issues, such as AttributeError: 'NoneType' object has no attribute 'render' like what is shown below:

Code Snippet: PyVis: AttributeError: 'NoneType' object has no attribute 'render'

✅ The Solution

Because this code is being executed within a Jupyter Notebook, there are extra parameters that need to be passed into the Network module from PyVis to execute the data network environment, notably, these two values:

  • notebook: a boolean value, which given the use of Jupyter Notebook, has been set to True

  • cdn_resources: a string, which can be “in_line” or “remote”, but must be specified within the Jupyter Notebook environment.


This is the line of code I added to the code block:

nt = Network('500px', '500px', notebook = True, cdn_resources = 'in_line')


Fixing this line will only solve part of the issue, as this will throw another error: localhost refused to connect as shown below:

To fix this, we will need to utilize the display module from IPython.core to visualize this html with the following line of code:

display(HTML('nx.html'))

By importing and leveraging the HTML and display modules from IPython.core, we can then add this extra line to display the knowledge graph with: display(HTML(filename='example.html'))

Check out the code and the output below:

Code Snippet: PyVis: PyVis visualization



Looking for more resources?

Read more related content here:



Snag my 7 Days of Development ebook here:

I'll send you a FREE copy of my 7 Days of Development Ebook to get you started writing Python scripts, Node.js functions, Next.js apps, and building in the cloud!


7 Days of Development Ebook
Previous
Previous

How to fix this Next.js error: No HTTP methods exported in… Export a named export for each HTTP method instead.

Next
Next

How to fix the PyVis Knowledge Graph error: localhost refused to connect