shukdevdatta123 commited on
Commit
0dbc58f
·
verified ·
1 Parent(s): 50d794a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -1
app.py CHANGED
@@ -11,7 +11,8 @@ sidebar_option = st.sidebar.radio("Select an option",
11
  "Basic: Simple graph Directed", "Drawing: Custom Node Position",
12
  "Drawing: Cluster Layout", "Drawing: Degree Analysis",
13
  "Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
14
- "Drawing: House With Colors", "Drawing: Labels And Colors"])
 
15
 
16
  # Helper function to draw and display graph
17
  def draw_graph(G, pos=None, title="Graph Visualization"):
@@ -19,6 +20,83 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
19
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
20
  st.pyplot(plt)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Function to display Drawing: Labels And Colors
23
  def display_labels_and_colors():
24
  st.title("Drawing: Labels And Colors")
 
11
  "Basic: Simple graph Directed", "Drawing: Custom Node Position",
12
  "Drawing: Cluster Layout", "Drawing: Degree Analysis",
13
  "Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
14
+ "Drawing: House With Colors", "Drawing: Labels And Colors",
15
+ "Drawing: Plotting MultiDiGraph Edges and Labels"])
16
 
17
  # Helper function to draw and display graph
18
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
20
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
21
  st.pyplot(plt)
22
 
23
+ # Function to display Drawing: Plotting MultiDiGraph Edges and Labels
24
+ def draw_labeled_multigraph(G, attr_name, ax=None):
25
+ """
26
+ Length of connectionstyle must be at least that of a maximum number of edges
27
+ between pair of nodes. This number is maximum one-sided connections
28
+ for directed graph and maximum total connections for undirected graph.
29
+ """
30
+ # Works with arc3 and angle3 connectionstyles
31
+ connectionstyle = [f"arc3,rad={r}" for r in it.accumulate([0.15] * 4)]
32
+ pos = nx.shell_layout(G)
33
+ nx.draw_networkx_nodes(G, pos, ax=ax)
34
+ nx.draw_networkx_labels(G, pos, font_size=20, ax=ax)
35
+ nx.draw_networkx_edges(
36
+ G, pos, edge_color="grey", connectionstyle=connectionstyle, ax=ax
37
+ )
38
+
39
+ labels = {
40
+ tuple(edge): f"{attr_name}={attrs[attr_name]}"
41
+ for *edge, attrs in G.edges(keys=True, data=True)
42
+ }
43
+ nx.draw_networkx_edge_labels(
44
+ G,
45
+ pos,
46
+ labels,
47
+ connectionstyle=connectionstyle,
48
+ label_pos=0.3,
49
+ font_color="blue",
50
+ bbox={"alpha": 0},
51
+ ax=ax,
52
+ )
53
+
54
+ # Function to display Drawing: Plotting MultiDiGraph Edges and Labels
55
+ def display_multidigraph_edges_labels():
56
+ st.title("Drawing: Plotting MultiDiGraph Edges and Labels")
57
+
58
+ option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
59
+
60
+ if option == "Default Example":
61
+ # Generate a multi directed graph as an example
62
+ nodes = "ABC"
63
+ prod = list(it.product(nodes, repeat=2))
64
+ pair_dict = {f"Product x {i}": prod * i for i in range(1, 5)}
65
+
66
+ fig, axes = plt.subplots(2, 2)
67
+ for (name, pairs), ax in zip(pair_dict.items(), np.ravel(axes)):
68
+ G = nx.MultiDiGraph()
69
+ for i, (u, v) in enumerate(pairs):
70
+ G.add_edge(u, v, w=round(i / 3, 2))
71
+ draw_labeled_multigraph(G, "w", ax)
72
+ ax.set_title(name)
73
+ fig.tight_layout()
74
+ st.pyplot(plt)
75
+
76
+ elif option == "Create your own":
77
+ # Let the user input the nodes and edges of the graph
78
+ st.write("Enter the nodes and edges to create your own labeled MultiDiGraph.")
79
+
80
+ nodes = st.text_area("Enter node labels (comma-separated, e.g., a,b,c,d):", value="a,b,c,d")
81
+ node_labels = nodes.split(',')
82
+
83
+ edges = st.text_area("Enter edges (format: node1-node2, comma-separated, e.g., a-b,b-c):", value="a-b,b-c,c-d")
84
+ edge_list = [tuple(edge.split('-')) for edge in edges.split(',')]
85
+
86
+ # Generate graph based on user input
87
+ G_custom = nx.MultiDiGraph()
88
+ for i, (u, v) in enumerate(edge_list):
89
+ G_custom.add_edge(u, v, w=round(i / 3, 2))
90
+
91
+ # Draw the graph
92
+ fig, ax = plt.subplots()
93
+ draw_labeled_multigraph(G_custom, "w", ax)
94
+ st.pyplot(plt)
95
+
96
+ # Display Drawing: Plotting MultiDiGraph Edges and Labels if selected
97
+ if sidebar_option == "Drawing: Plotting MultiDiGraph Edges and Labels":
98
+ display_multidigraph_edges_labels()
99
+
100
  # Function to display Drawing: Labels And Colors
101
  def display_labels_and_colors():
102
  st.title("Drawing: Labels And Colors")