WestHealth / pyvis

Python package for creating and visualizing interactive network graphs.
http://pyvis.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
940 stars 162 forks source link

Network.set_options node color is broken. #289

Open YouGuessedMyName opened 1 week ago

YouGuessedMyName commented 1 week ago

To reproduce, run the following (not in a notebook):

from pyvis.network import Network
import networkx as nx

g = Network()

g.from_nx(nx.florentine_families_graph())
#g.show_buttons(filter_=['nodes'])
g.set_options("""
var options = {
    "nodes": {
        "color": {
            "background": "rgba(252,39,99,1)"
        }
    }
}""")

html = g.generate_html("example2.html")
with open("example2.html", mode='w', encoding='utf-8') as fp:
        fp.write(html)

Expected behavior: Florence family graph with purple background nodes. Actual behavior: Florence family graph with default blue background nodes.

linusheck commented 1 week ago

I think the reason is that g.from_nx calls g.add_node with no argument for color, which leads to the nodes getting the default argument color="#97c2fc", which overrides anything you set in set_options. I think the library should not behave like this, the default argument for color being "#97c2fc" is dumb.

YouGuessedMyName commented 1 week ago

Thank you Linus, if anyone else is also facing this issue, you can edit the node colors as expected if you generate a graph using g.add_node(1, color=None). The 'None' makes sure that the default color is not overwritten and VisJs actually applies the layout specified in set_options . The default value of the color argument of add_node should definitely be changed to 'None' in the source code.