pyvis: set_options() not working

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!

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 17 (1 by maintainers)

Most upvoted comments

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.

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)

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)

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.