pyvis icon indicating copy to clipboard operation
pyvis copied to clipboard

set_options() not working

Open yutxie opened this issue 4 years ago • 16 comments

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!

yutxie avatar Feb 01 '21 13:02 yutxie

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

jonschull avatar Mar 30 '21 00:03 jonschull

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.

boludo00 avatar Apr 29 '21 17:04 boludo00

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.

pavelb97 avatar May 27 '21 09:05 pavelb97

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.

Marcin-Radecki avatar Jun 16 '21 08:06 Marcin-Radecki

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

jhunpingco avatar Oct 22 '21 19:10 jhunpingco

This is still an issue - if you add the "configure" statement, it renders fine, but then show_buttons(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.

tyrin avatar Feb 25 '22 17:02 tyrin

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

AlexandreRozier avatar Apr 05 '22 08:04 AlexandreRozier

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)

pbouill avatar Apr 27 '23 16:04 pbouill

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)

drAbreu avatar Dec 08 '23 09:12 drAbreu

@.*** 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: @.***>

jonschull avatar Dec 08 '23 20:12 jonschull

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}})

stefanzero avatar Apr 02 '24 17:04 stefanzero