def app(): nodes = [] edges = [] nodes.append(Node(id="Spiderman", label="Spid", size=400)) nodes.append(Node(id="Captain_Marvel", label="marvel", size=400)) edges.append( Edge(source="Captain_Marvel", target="Spiderman", label="friend_of")) # nodes = [Node(id=n['id'], label=n['name']) for n in json_nodes] # add value to payload # edges = [Edge(source=e['sourceId'], target=e['targetId']) for e in json_edges] config = Config( width=1600, height=1000, directed=True, nodeHighlightBehavior=True, highlightColor="#F7A7A6", collapsible=True, node={'labelProperty': 'label'}, # config labelProperty link={ 'labelProperty': 'label', 'renderLabel': True }) return_value = agraph(nodes=nodes, edges=edges, config=config)
def plot_graph2(graph): nodes = [Node(id=i, label=str(i), size=200) for i in range(len(graph.nodes))] edges = [Edge(source=i, target=j, type="CURVE_SMOOTH") for (i, j) in graph.edges] config = Config(width=500, height=500, directed=True, nodeHighlightBehavior=True, highlightColor="#F7A7A6", collapsible=True, node={'labelProperty': 'label'}, link={'labelProperty': 'label', 'renderLabel': True} ) return_value = agraph(nodes=nodes, edges=edges, config=config)
columns=('col %d' % i for i in range(20))) st.dataframe(dataframe.style.highlight_max(axis=0)) dataframe1 = pd.DataFrame(np.random.randn(10, 20), columns=('col %d' % i for i in range(20))) st.table(dataframe1) elif pag_selec == 'Graph': st.title('Testes com Grafos') nodes = [] edges = [] nodes.append( Node( id="Spiderman", label="Peter Parker", size=400, svg= "http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_spiderman.png" )) # includes **kwargs nodes.append( Node( id="Captain_Marvel", size=400, svg= "http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_captainmarvel.png" )) nodes.append( Node( id="Thor", size=400, svg=
with st.spinner("Train decision tree classifier"): clf = tree.DecisionTreeClassifier() clf = clf.fit(iris.data, iris.target) dot_data = tree.export_graphviz(clf, out_file=None, feature_names=iris.feature_names) G = pgv.AGraph().from_string(dot_data) nodes = [] edges = [] for node in G.nodes(): nodes.append(Node(id=node, label=node.attr['label'].split('\\n')[0], symbolType='square')) for edge in G.edges(): edges.append( Edge(source=edge[0], target=edge[1], type="STRAIGHT") ) layout = st.sidebar.selectbox('layout',['dot', 'neato', 'circo', 'fdp', 'sfdp']) rankdir = st.sidebar.selectbox("rankdir", ['BT', 'TB', 'LR', 'RL'])
# First create a graph using the Barabasi-Albert model n = 2000 m = 2 G = nx.generators.barabasi_albert_graph(n, m) # Then find the node with the largest degree; # This node's egonet will be the focus of this example. node_and_degree = G.degree() most_connected_node = sorted(G.degree, key=lambda x: x[1], reverse=True)[0] degree = G.degree(most_connected_node) # Create egonet for the focal node hub_ego = nx.ego_graph(G, most_connected_node) # Now create the equivalent Node and Edge lists nodes = [Node(id=i, label=str(i), size=200) for i in hub_ego.nodes] edges = [ Edge(source=i, target=j, type="CURVE_SMOOTH") for (i, j) in G.edges if i in hub_ego.nodes and j in hub_ego.nodes ] config = Config( width=500, height=500, directed=True, nodeHighlightBehavior=False, highlightColor="#F7A7A6", # or "blue" collapsible=False, node={'labelProperty': 'label'}, # **kwargs e.g. node_size=1000 or node_color="blue" )
def app(): # Set title and user input elements st.title("Recipe Recommender Network") sidebar = st.sidebar middle, rsidebar = st.beta_columns([3, 1]) sidebar.title("Choose meal type and ingredients") userIngredients = sidebar.text_input( "Input ingredients that should be included in returned recipes:") userInput = sidebar.text_input("Input recipe to use as cluster seed: ") # create empty lists for holding nodes and edges for network #net = Network() # place holder for transitioning to pyvis nodes = [] edges = [] nodeSize = 500 # Bring in the initial data tmp = pd.read_csv( r'C:\Users\jdnun\OneDrive\Documents\GT\Spring2021\Project\code\cse6242_project-main\src\cluster_test.csv' ) # Here is where we would access the data from the backend print('length before filter: ', len(tmp)) # filter data for only recipes that contain the user inputs if userIngredients != "": ingredients = userIngredients.split(',') ingredients = [i.strip() for i in ingredients] contains = [ tmp["RecipeIngredientParts"].str.contains(i) for i in ingredients ] #print(ingredients) tmp = tmp[np.all(contains, axis=0)] print('length of filtered: ', len(tmp)) if len(tmp) != 0: edgeList = [] ### Used for loading the data directly from the dictionary output from Orion's clustering script # for i in range(0, len(tmp["c1"]["nodes"])): # nodes.extend([Node(id=int(tmp["c1"]["nodes"].iloc[i]['RecipeId']), label=tmp["c1"]["nodes"].iloc[i]["Name"], # size=nodeSize)]) # for j in range(0, len(tmp["c1"]["nodes"])): # if tmp["c1"]["nodes"].iloc[i]['RecipeId'] != tmp["c1"]["nodes"].iloc[j]['RecipeId'] and int(tmp["c1"]["nodes"].iloc[i]["Ingredient Difference"]) == int(tmp["c1"]["nodes"].iloc[j]["Ingredient Difference"]) \ # and [int(tmp["c1"]["nodes"].iloc[j]['RecipeId']),int(tmp["c1"]["nodes"].iloc[i]['RecipeId'])] not in edgeList: # edgeList.append([int(tmp["c1"]["nodes"].iloc[i]['RecipeId']),int(tmp["c1"]["nodes"].iloc[j]['RecipeId'])]) # edges.extend([Edge(source=int(tmp["c1"]["nodes"].iloc[i]['RecipeId']), label=int(tmp["c1"]["nodes"].iloc[j]["Ingredient Difference"]), # target=int(tmp["c1"]["nodes"].iloc[j]["RecipeId"]), type="CURVE_SMOOTH")]) for i in range(0, len(tmp["nodes"])): nodes.extend([ Node(id=int(tmp.iloc[i]['RecipeId']), label=tmp.iloc[i]["Name"], size=nodeSize) ]) #net.add_node(n_id=int(tmp.iloc[i]['RecipeId']), value=nodeSize, label=tmp.iloc[i]["Name"]) # place holder for transitioning to pyvis for j in range(0, len(tmp["nodes"])): if tmp.iloc[i]['RecipeId'] != tmp.iloc[j]['RecipeId'] and int(tmp.iloc[i]["Ingredient Difference"]) == int(tmp.iloc[j]["Ingredient Difference"]) \ and [int(tmp.iloc[j]['RecipeId']),int(tmp.iloc[i]['RecipeId'])] not in edgeList: edgeList.append([ int(tmp.iloc[i]['RecipeId']), int(tmp.iloc[j]['RecipeId']) ]) edges.extend([ Edge(source=int(tmp.iloc[i]['RecipeId']), label=int(tmp.iloc[j]["Ingredient Difference"]), target=int(tmp.iloc[j]["RecipeId"]), type="CURVE_SMOOTH") ]) #net.add_edge(int(tmp.iloc[i]['RecipeId']),int(tmp.iloc[j]["RecipeId"]),title=int(tmp.iloc[j]["Ingredient Difference"])) # # place holder for transitioning to pyvis # diffList = [str(i) for i in set(tmp["c1"]["nodes"]["Ingredient Difference"])] # diffList.append('All') # diffList.sort(reverse=True) # display_category = sidebar.selectbox("Ingredient difference: ",index=0, options = diffList) # could add more stuff here later on or add other endpoints in the sidebar. # if display_category == "all": viewEdges = edges # else: # viewEdges = [edge for edge in edges if edge.label == display_category] #mealType = sidebar.selectbox("Meal Type: ", index=0, options = ["Breakfast", "Lunch", "Dinner"]) # Just a place holder could be 'soup', 'salad', or 'italian', 'indian', etc # set network configuration config = Config(height=500, width=700, nodeHighlightBehavior=True, highlightColor="#F7A7A6", directed=False, collapsible=True, node={'labelProperty': 'label'}, link={ 'labelProperty': 'label', 'renderLabel': False }) # add network to middle column of streamlit canvas with middle: #st.text("Displaying {} cuisine types".format(display_category)) return_value = agraph(nodes=nodes, edges=viewEdges, config=config) #net.show('testgraph.html') # place holder for transitioning to pyvis st.text("Showing recipes based on the following ingredients: {}".format( userIngredients))
# karate_club_graph.py # An example of basic and common interoperability between # streamlit-agraph and networkx. # # Use the following command to launch the app # streamlit run <path-to-script>.py import networkx as nx from streamlit_agraph import agraph, Node, Edge, Config # Generate the networkx implementation of Zachary's Karate Club graph # (https://en.wikipedia.org/wiki/Zachary%27s_karate_club) G = nx.karate_club_graph() # Create the equivalent Node and Edge lists nodes = [Node(id=i, label=str(i), size=200) for i in range(len(G.nodes))] edges = [Edge(source=i, target=j, type="CURVE_SMOOTH") for (i, j) in G.edges] config = Config(width=500, height=500, directed=True, nodeHighlightBehavior=True, highlightColor="#F7A7A6", collapsible=True, node={'labelProperty': 'label'}, link={ 'labelProperty': 'label', 'renderLabel': True }) return_value = agraph(nodes=nodes, edges=edges, config=config)
subgraph = nx.subgraph(G, subset) node_and_degree = subgraph.degree() nx.set_node_attributes(subgraph, values=dict(subgraph.degree), name='degree') if ego_: most_connected_node = sorted(subgraph.degree, key=lambda x: x[1], reverse=True)[0][0] subgraph = nx.ego_graph(subgraph, most_connected_node) # Now create the equivalent Node and Edge lists nodes = [ Node(id=n[0], label=str(n[1]['label']), size=(2 + n[1]['degree'])**3) for n in subgraph.nodes(data=True) ] edges = [ Edge(source=e[0], target=e[1], labelProperty="", label=e[2]['relation'], type=edge_style) for e in subgraph.edges(data=True) if e[0] in subgraph.nodes and e[1] in subgraph.nodes ] config = Config( width=600, height=600, #graphviz_layout=layout,
f = open('patientInfection.json') print('check') trp_data = f.read() print(type(trp_data)) temp_data = json.loads(trp_data) temp_data = temp_data[0] print(type(temp_data)) print(temp_data["@@edgeSet"][0]) nodes = [] edges = [] for val in temp_data["@@edgeSet"]: nodes.append(Node(id=val['from_id'])) nodes.append(Node(id=val['to_id'])) edges.append( Edge(source=val['from_id'], target=val['to_id'], labelProperty=val['e_type'], renderLabel=True)) config = Config(height=500, width=700, nodeHighlightBehavior=True, highlightColor="#F7A7A6", directed=True, collapsible=True) agraph(nodes, edges, config)