Skip to content

Commit

Permalink
viewer.py: now able to use different styles file. #64 @dotasek
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeboucas committed Aug 7, 2018
1 parent 79afa40 commit 47a518f
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions py2cytoscape/cytoscapejs/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

HTML_TEMPLATE_FILE = 'template.html'
STYLE_FILE = 'default_style.json'
STYLE_FILE=os.path.abspath(os.path.dirname(__file__)) + '/' + STYLE_FILE


# Default network
DEF_NODES = [
Expand All @@ -37,19 +39,30 @@


# Init styles
style_file = open(os.path.abspath(os.path.dirname(__file__)) + '/' + STYLE_FILE, 'r')
style_list = json.load(style_file)
STYLES = {}
for style in style_list:
STYLES[style['title']] = style['style']
#style_file = open(os.path.abspath(os.path.dirname(__file__)) + '/' + STYLE_FILE, 'r')
#style_list = json.load(style_file)
#STYLES = {}
#for style in style_list:
# STYLES[style['title']] = style['style']


def set_styles(style_file=STYLE_FILE):
style_file = open(style_file, 'r')
style_list = json.load(style_file)
STYLES = {}
for style in style_list:
STYLES[style['title']] = style['style']
return STYLES

def render(network,
style=DEF_STYLE,
layout_algorithm=DEF_LAYOUT,
background=DEF_BACKGROUND_COLOR,
height=DEF_HEIGHT,
width=DEF_WIDTH):
width=DEF_WIDTH,
style_file=STYLE_FILE,
def_nodes=DEF_NODES,
def_edges=DEF_EDGES):
"""Render network data with embedded Cytoscape.js widget.
:param network: dict (required)
Expand All @@ -65,19 +78,26 @@ def render(network,
Height of the widget.
:param width: int
Width of the widget.
:param style_file: a styles file. [default: 'default_style.json']
:param def_nodes: default: [
{'data': {'id': 'Network Data'}},
{'data': {'id': 'Empty'}} ]
:param def_edges: default: [ {'data': {'id': 'is', 'source': 'Network Data', 'target': 'Empty'}} ]
"""

from jinja2 import Template
from IPython.core.display import display, HTML

STYLES=set_styles(style_file)

# Load style file if none available
if isinstance(style, str):
# Specified by name
style = STYLES[style]

if network is None:
nodes = DEF_NODES
edges = DEF_EDGES
nodes = def_nodes
edges = def_edges
else:
nodes = network['elements']['nodes']
edges = network['elements']['edges']
Expand All @@ -102,12 +122,12 @@ def render(network,
def get_layouts():
return PRESET_LAYOUTS


def get_style_names():
def get_style_names(style_file=STYLE_FILE):
STYLES=set_styles(style_file)
return list(STYLES.keys())


def get_style(name):
def get_style(name,style_file=STYLE_FILE):
STYLES=set_styles(style_file)
if name in STYLES.keys():
return STYLES[name]
else:
Expand Down

0 comments on commit 47a518f

Please sign in to comment.