Skip to content

Commit

Permalink
Merge pull request #40 from WestHealth/feature/30-options-as-dictionary
Browse files Browse the repository at this point in the history
Feature/30 options as dictionary
  • Loading branch information
Giancarlo Perrone committed Jul 8, 2019
2 parents 41cf502 + 91f1491 commit 8773ed9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# binaries
*.pyc

# package details
pyvis.egg-info/

# vscode specific
.vscode/
9 changes: 8 additions & 1 deletion pyvis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,11 @@ def get_network_data(self):
>>> nodes, edges, height, width, options = net.get_network_data()
"""
return (self.nodes, self.edges, self.height,
if isinstance(self.options, dict):
return (self.nodes, self.edges, self.height,
self.width, json.dumps(self.options))
else:
return (self.nodes, self.edges, self.height,
self.width, self.options.to_json())

def save_graph(self, name):
Expand Down Expand Up @@ -835,3 +839,6 @@ def toggle_stabilization(self, status):
:type status: bool
"""
self.options.physics.toggle_stabilization(status)

def set_options(self, options):
self.options = self.options.set(options)
34 changes: 28 additions & 6 deletions pyvis/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ def __repr__(self):
return str(self.__dict__)

def __init__(self, randomSeed=None, improvedLayout=True):
if not randomSeed:
self.randomSeed = 0
else:
self.radnomSeed = randomSeed
self.improvedLayout = improvedLayout
self.hierarchical = self.Hierarchical(enabled=True)
if not randomSeed:
self.randomSeed = 0
else:
self.radnomSeed = randomSeed
self.improvedLayout = improvedLayout
self.hierarchical = self.Hierarchical(enabled=True)

def set_separation(self, distance):
"""
Expand Down Expand Up @@ -203,6 +203,28 @@ def __init__(self, layout=None):
self.physics = Physics()
self.edges = EdgeOptions()

def set(self, new_options):
"""
This method should accept a JSON string and replace its internal
options structure with the given argument after parsing it.
In practice, this method should be called after using the browser
to experiment with different physics and layout options, using
the generated JSON options structure that is spit out from the
front end to serve as input to this method as a string.
:param new_options: The JSON like string of the options that will
override.
:type new_options: str
"""

options = new_options.replace("\n", "").replace(" ", "")
first_bracket = options.find("{")
options = options[first_bracket:]
options = json.loads(options)
return options


def to_json(self):
return json.dumps(
self, default=lambda o: o.__dict__,
Expand Down
4 changes: 0 additions & 4 deletions pyvis/templates/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@

{% endif %}

// default to using dot shape for nodes
options.nodes = {
shape: "dot"
}
{% if conf %}
// if this network requires displaying the configure window,
// put it in its div
Expand Down

0 comments on commit 8773ed9

Please sign in to comment.