Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add constructor to main widget #258

Merged
merged 8 commits into from
Nov 27, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions ipycytoscape/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

# Copyright (c) Mariana Meireles.
# Distributed under the terms of the Modified BSD License.

from os import path
import copy
import json

from spectate import mvc
from traitlets import TraitType, TraitError

from pandas import DataFrame
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently don't require pandas so this need to either be an optional import or the check below needs to check for pandas in the graph.__class__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a change to fix this, but I am not sure I passed the kwargs correctly to the add_graph_from_df correctly ... cold you please let me know what the correct way to do that would be? :)


from ipywidgets import DOMWidget, Widget, widget_serialization, CallbackDispatcher
from traitlets import (
Unicode,
Expand Down Expand Up @@ -504,11 +507,15 @@ def add_graph_from_json(self, json_file, directed=False, multiple_edges=False):

Parameters
----------
json_file : dict
json_file : dict, string
vaniisgh marked this conversation as resolved.
Show resolved Hide resolved
directed : bool
If True all edges will be given 'directed' as a class if
they do not already have it.
"""
if path.isfile(str(json_file)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fully protect us if the user passes in a dict?

with open(json_file) as f:
json_file = json.load(f)

node_list = list()
for node in json_file["nodes"]:
node_instance = Node()
Expand Down Expand Up @@ -811,12 +818,21 @@ class CytoscapeWidget(DOMWidget):

graph = Instance(Graph, args=tuple()).tag(sync=True, **widget_serialization)

def __init__(self, **kwargs):
def __init__(self, graph_file=None, **kwargs):
vaniisgh marked this conversation as resolved.
Show resolved Hide resolved
super(CytoscapeWidget, self).__init__(**kwargs)

self.on_msg(self._handle_interaction)
self.graph = Graph()

if isinstance(graph_file, nx.Graph):
self.graph.add_graph_from_networkx(graph_file)
if isinstance(graph_file, dict):
self.graph.add_graph_from_json(graph_file)
if isinstance(graph_file, DataFrame):
self.graph.add_graph_from_df(graph_file)
if isinstance(graph_file, str):
self.graph.add_graph_from_json(graph_file)

vaniisgh marked this conversation as resolved.
Show resolved Hide resolved
# Make sure we have a callback dispatcher for this widget and event type;
# since _interaction_handlers is synced with the frontend and changes to
# mutable values don't automatically propagate, we need to explicitly set
Expand Down