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