Ivo
Ivo

Reputation: 4200

Use pre-saved physics options for pyvis network

I have a pyvis network. Consider this example

from pyvis import Network
G = Network(notebook=True)
G.add_nodes([1, 2, 3], label=['Peter', 'Paul', Mary])
G.add_nodes([4, 5, 6, 7], 
            label=['Michael', 'Ben', 'Oliver', 'Olivia'],
            color=['#3da831', '#9a31a8', '#3155a8', '#eb4034'])

When I visualized my actual network, the points kept moving. As per this answer, I turn on physics controls using G.show_buttons(filter_=['physics']). I then show the network in an HTML (G.show('tmp.html')) and open the HTML.

There, I modify the options and click on 'generate options' which gives me this options summary:

const options = {
  "physics": {
    "enabled": false,
    "forceAtlas2Based": {
      "springLength": 100
    },
    "minVelocity": 0.75,
    "solver": "forceAtlas2Based"
  }
}

I would now like to set these options to a graph and not display the physics options every time anew.

How do I set pre-saved physics options to the pyvis object?

Thanks for any and all pointers.

Upvotes: 2

Views: 775

Answers (1)

Djanger
Djanger

Reputation: 143

You can set pre-saved physics options to the pyvis Network object using the set_options method, like this:

    G.set_options("""
const options = {
  "physics": {
    "enabled": false,
    "forceAtlas2Based": {
      "springLength": 100
    },
    "minVelocity": 0.75,
    "solver": "forceAtlas2Based"
  }
}""")

Upvotes: 3

Related Questions