WestHealth / pyvis

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

set_options() not working #81

Open yutxie opened 3 years ago

yutxie commented 3 years ago

Hi! I'm trying to use the set_options(...) function to set default configurations. However, the visualization fails to be loaded after this function is called.

Below is my testing code.

import networkx as nx
from pyvis.network import Network

net = Network()
net.from_nx(nx.complete_graph(5))
net.show_buttons()
net.set_options('''
var options = {
  "edges": {
    "color": {
      "inherit": true
    },
    "smooth": false
  },
  "physics": {
    "minVelocity": 0.75
  }
}
''')
net.show("ex.html")

I have also tried to remove the set_options(...) part as below, and found the desired figure can be shown immediately after net.show("ex.html").

import networkx as nx
from pyvis.network import Network

net = Network()
net.from_nx(nx.complete_graph(5))
net.show_buttons()
net.show("ex.html")

I printed out the default net.options. It goes like this:

{'interaction': {'hideEdgesOnDrag': False, 'hideNodesOnDrag': False, 'dragNodes': True}, 'configure': {'enabled': True}, 'physics': {'enabled': True, 'stabilization': <pyvis.physics.Physics.Stabilization object at 0x000001548026A280>}, 'edges': {'smooth': {'enabled': False, 'type': 'continuous'}, 'color': {'inherit': True}}}

I was wondering whether some essential elements (e.g., pyvis.physics.Physics.Stabilization object) were lost during modifying net.options, so that the figure could not be loaded as expected.

Hope you could help me with this! Thank you so much!

jonschull commented 3 years ago

I am having the same problem. In the following code, the OPTIONS were copied from Generate Options output. Yet as you can see the hierarchical option (among others) do not "take).


edges = [
('1', '1.1'),
('1', '1.2'),
('1.2', '1.2.1'),
('1.2', '1.2.2'),
('2', '2.1')
]

OPTIONS = """{
  "edges": {
    "color": {
      "inherit": true
    },
    "smooth": false
  },
  "layout": {
    "hierarchical": {
      "enabled": true
    }
  },
  "physics": {
    "hierarchicalRepulsion": {
      "centralGravity": 0
    },
    "minVelocity": 0.75,
    "solver": "hierarchicalRepulsion"
  }
}"""
g=net.Network(notebook=True)
g.options.set(OPTIONS)  ##This is not working
names=[]
for parent, name in edges:
    g.add_node(parent,shape='box')
    g.add_node(name, shape='box')
    print(name)
    if parent:
        g.add_edge(parent, name)
        print(f"('{parent}', '{name}'),")
g.show_buttons()
g.show('fromraw.html')

Pasted_Image_3_29_21__8_38_PM

boludo00 commented 3 years ago

Hi guys, I have been playing around with this as of visjs version 9.0.4 and it is giving me really buggy results. So I am not for certain what could be the issue, I am thinking something in the JS layer as there are several errors thrown in the browser debugger when making UI changes.

pavelb97 commented 3 years ago

Has anyone found a solution for this? I'm running into an AttributeError: 'dict' object has no attribute 'configure' when I try and pass JSON options in the same format as OP.

Marcin-Radecki commented 3 years ago

Hi, I think this is because when you use Network.show_buttons(), then if you check webpage source then you can see a framework adds this key 'configure' to json, which is not suggested when you copy it from the GUI:

    var options = {
    "configure": {
        "enabled": true
    },
    "edges": {
        "color": {
            "inherit": true
        },
        "smooth": {
            "enabled": false,
            "type": "continuous"
        }
    },
    "interaction": {
        "dragNodes": true,
        "hideEdgesOnDrag": false,
        "hideNodesOnDrag": false
    },
    "physics": {
        "enabled": true,
        "stabilization": {
            "enabled": true,
            "fit": true,
            "iterations": 1000,
            "onlyDynamicEdges": false,
            "updateInterval": 50
        }
    }
};

So the workaround is to always include this section in e.g.

   options = """
var options = {
   "configure": {
        "enabled": true
   },
  "edges": {
    "color": {
      "inherit": true
    },
    "smooth": false
  },
  "physics": {
    "barnesHut": {
      "gravitationalConstant": -12050
    },
    "minVelocity": 0.75
  }
}
"""
        net.set_options(options)

But I think this is an issue, either this should be managed by Pyvis already.

jhunpingco commented 2 years ago

@Marcin-Radecki Can you put together a pull request for this? I just merged a bunch of them.

tyrin commented 2 years ago

This is still an issue - if you add the "configure" statement, it renders fine, but then showbuttons(filter=['physics']) throws an error - AttributeError: 'dict' object has no attribute 'configure' So I think that show_buttons is constructing a version of the dict without the configure and that's what's causing the problem. Here's a quick vid of the issue. Let me know if you can't get to it - I can move it to a personal account, rather than a company one.

AlexandreRozier commented 2 years ago

Having the same problem, no "configure" object is recreated in set_options

pbouill commented 1 year ago

yeah -- I also had to include my filters in the "configure" key -- otherwise was getting that "AttributeError: 'dict' object has no attribute 'configure'"

options = '''
var options = {
  "configure": {
    "enabled": true,
    "filter": ["physics"]
  },
#other content from the "generate options" output
...
'''
net.set_options(options)
net.write_html(file)
drAbreu commented 9 months ago

I can confirm this is a good workaround for me.

yeah -- I also had to include my filters in the "configure" key -- otherwise was getting that "AttributeError: 'dict' object has no attribute 'configure'"

options = '''
var options = {
  "configure": {
    "enabled": true,
    "filter": ["physics"]
  },
#other content from the "generate options" output
...
'''
net.set_options(options)
net.write_html(file)
jonschull commented 9 months ago

@.*** cell: 585-738-6696 Co-Founder, e-NABLE: volunteers worldwide making free, 3D printed prosthetics Innovation Fellow, JMK Innovation Fund

Sent from iPhone: beware tyops.

On Fri, Dec 8, 2023 at 9:33 AM Dr. Jorge Abreu Vicente < @.***> wrote:

I can confirm this is a good workaround for me.

yeah -- I also had to include my filters in the "configure" key -- otherwise was getting that "AttributeError: 'dict' object has no attribute 'configure'"

options = '''var options = { "configure": { "enabled": true, "filter": ["physics"] },#other content from the "generate options" output...'''net.set_options(options)net.write_html(file)

— Reply to this email directly, view it on GitHub https://github.com/WestHealth/pyvis/issues/81#issuecomment-1846854891, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAANDKMKLGKJU2W4GORKK53YILNF5AVCNFSM4W4Y7O4KU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBUGY4DKNBYHEYQ . You are receiving this because you commented.Message ID: @.***>

stefanzero commented 5 months ago

In the source code listed in the documentation generate_html, it shows that the option physics has a sub-key enabled.

In the Network constructor, add this dictionary to physics:

Network(options={"physics": {"enabled": False}})
papoteur-mga commented 3 months ago

In the source code listed in the documentation generate_html, it shows that the option physics has a sub-key enabled.

In the Network constructor, add this dictionary to physics:

Network(options={"physics": {"enabled": False}})

Hmm

    newgraph =  Network('800px', width="100%", cdn_resources="local", options={"layout": {"hierarchical": {"direction": "UD"}}})

    TypeError: Network.__init__() got an unexpected keyword argument 'options'
BecayeCIMA commented 1 month ago

Getting the same issue in July 2024? Any fix?