Created
February 21, 2021 22:54
-
-
Save joseberlines/1462cdb193e8152b3f0366fd799bc3c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def transform_into_ipycytoscape(nodes_df,edges_df): | |
| nodes_dict = nodes_df.to_dict('records') | |
| edges_dict = edges_df.to_dict('records') | |
| # building nodes | |
| data_keys = ['id','label','classes'] | |
| position_keys = ['position_x','position_y'] | |
| rest_keys = ['score','idInt','name','score','group','removed','selected','selectable','locked','grabbed' | |
| 'grabbable'] | |
| nodes_graph_list=[] | |
| for node in nodes_dict: | |
| dict_node = {} | |
| data_sub_dict = {'data':{el:node[el] for el in data_keys}} | |
| rest_sub_dict = {el:node[el] for el in node.keys() if el in rest_keys} | |
| posi_sub_dict = {} | |
| if 'position_x' in node.keys() and 'position_y' in node.keys(): | |
| #print(node.keys()) | |
| posi_sub_dict = {'position':{el:node[el] for el in node.keys() if el in position_keys}} | |
| dict_node = {**data_sub_dict,**rest_sub_dict,**posi_sub_dict} | |
| nodes_graph_list.append(dict_node) | |
| # building edges | |
| data_keys = ['id','source','target'] | |
| data_keys2 = ['label','classes'] | |
| rest_keys = ['score','weight','group','networkId','networkGroupId','intn','rIntnId','group','removed','selected','selectable','locked','grabbed','grabbable','classes'] | |
| position_keys = ['position_x','position_y'] | |
| edges_graph_list = [] | |
| for edge in edges_dict: | |
| dict_edge = {} | |
| data_sub_dict = {el:edge[el] for el in data_keys} | |
| data_sub_dict2 = {el:edge[el] for el in edge.keys() if el in data_keys2} | |
| rest_sub_dict = {el:edge[el] for el in edge.keys() if el in rest_keys} | |
| dict_edge = {'data':{**data_sub_dict,**data_sub_dict},**rest_sub_dict} | |
| edges_graph_list.append(dict_edge) | |
| total_graph_dict = {'nodes': nodes_graph_list, 'edges':edges_graph_list} | |
| # building the style | |
| all_node_style = ['background-color','background-opacity', | |
| 'font-family','font-size','label','width', | |
| 'shape','height','width','text-valign','text-halign'] | |
| all_edge_style = ['background-color','background-opacity', | |
| 'font-family','font-size','label','width','line-color', | |
| ] | |
| total_style_dict = {} | |
| style_elements=[] | |
| for node in nodes_dict: | |
| node_dict = {'selector': f'node[id = \"{node["id"]}\"]'} | |
| style_dict ={"style": { el:node[el] for el in node.keys() if el in all_node_style}} | |
| node_dict.update(style_dict) | |
| style_elements.append(node_dict) | |
| for edge in edges_dict: | |
| edge_dict = {'selector': f'edge[id = \"{edge["id"]}\"]'} | |
| style_dict ={"style": { el:edge[el] for el in edge.keys() if el in all_edge_style}} | |
| edge_dict.update(style_dict) | |
| style_elements.append(edge_dict) | |
| # the graph | |
| data_graph = json.dumps(total_graph_dict) | |
| json_to_python = json.loads(data_graph) | |
| result_cyto = ipycytoscape.CytoscapeWidget() | |
| result_cyto.graph.add_graph_from_json(json_to_python) | |
| result_cyto.set_style(style_elements) | |
| return result_cyto | |
| G=transform_into_ipycytoscape(stations_df,rails_df) | |
| display(G) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment