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:
✅ 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 toTrue
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:
Looking for more resources?
Read more related content here:
- Automating Code Generation with OpenAI GPT Agents: A Developer's Journey to Efficiency
- Thankful for the AWS Community Builders Program & Building + AWS Step Functions 101
- AWS: Predicting Stock Prices Using AI & NO CODE!
- Want to Learn How to Code? Discover the Top 4 Platforms to Kickstart Your Software Engineer Journey of Coding
- Turn Your AI Model into a Real Product (Amazon SageMaker, API Gateway, AWS Lambda, Next.js, Python)
- Launch your own LLM (Deploy LLaMA 2 on Amazon SageMaker with Hugging Face Deep Learning Containers)
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!