2

In pyvis, Why I get this error when I am trying to visualise a simple graph (3 lines of codes only)?

net=Network(notebook=True, cdn_resources='in_line')
net.from_nx(nx.davis_southern_women_graph())
net.show('example.html')

This leads to the error:

Traceback (most recent call last):
  File "g:\My Drive\....\graph_v.py", line 270, in <module>
    net.show('example.html')
  File "C:\Users\ziton\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvis\network.py", line 546, in show
    self.write_html(name, open_browser=False,notebook=True)
  File "C:\Users\ziton\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvis\network.py", line 530, in write_html
    out.write(self.html)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 19, in encode 
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 263607-263621: character maps to <undefined>

Some research yielded that I need to update the encoding to UTF-8 but when calling which method and how?

Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83

1 Answers1

1

One possibility could be

from pyvis.network import Network
import networkx as nx
from IPython.display import display, HTML

net=Network(notebook=True, cdn_resources='in_line')
net.from_nx(nx.davis_southern_women_graph())
# net.show('example.html')

html = net.generate_html()
with open("example.html", mode='w', encoding='utf-8') as fp:
        fp.write(html)
display(HTML(html))

that generates the image below. It is not necessary to store the html file, but you may wish to do so.

enter image description here

karpan
  • 421
  • 1
  • 5
  • 13