Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,8 @@ import numpy as np
|
|
7 |
sidebar_option = st.sidebar.radio("Select an option",
|
8 |
["Select an option", "Basic: Properties",
|
9 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
10 |
-
"Basic: Simple graph Directed", "Drawing: Custom Node Position"
|
|
|
11 |
|
12 |
# Helper function to draw and display graph
|
13 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -124,6 +125,38 @@ def display_custom_node_position():
|
|
124 |
# Draw the graph
|
125 |
draw_graph(G, pos)
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
# Display Basic: Properties if selected
|
128 |
if sidebar_option == "Basic: Properties":
|
129 |
st.title("Basic: Properties")
|
@@ -228,3 +261,7 @@ elif sidebar_option == "Basic: Simple graph Directed":
|
|
228 |
# Display Drawing: Custom Node Position if selected
|
229 |
elif sidebar_option == "Drawing: Custom Node Position":
|
230 |
display_custom_node_position()
|
|
|
|
|
|
|
|
|
|
7 |
sidebar_option = st.sidebar.radio("Select an option",
|
8 |
["Select an option", "Basic: Properties",
|
9 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
10 |
+
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
11 |
+
"Drawing: Cluster Layout"])
|
12 |
|
13 |
# Helper function to draw and display graph
|
14 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
125 |
# Draw the graph
|
126 |
draw_graph(G, pos)
|
127 |
|
128 |
+
# Function to display Cluster Layout for Drawing: Cluster Layout
|
129 |
+
def display_cluster_layout():
|
130 |
+
st.title("Drawing: Cluster Layout")
|
131 |
+
|
132 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
133 |
+
|
134 |
+
if option == "Default Example":
|
135 |
+
G = nx.davis_southern_women_graph() # Example graph
|
136 |
+
communities = nx.community.greedy_modularity_communities(G)
|
137 |
+
|
138 |
+
# Compute positions for the node clusters as if they were themselves nodes in a supergraph using a larger scale factor
|
139 |
+
supergraph = nx.cycle_graph(len(communities))
|
140 |
+
superpos = nx.spring_layout(G, scale=50, seed=429)
|
141 |
+
|
142 |
+
# Use the "supernode" positions as the center of each node cluster
|
143 |
+
centers = list(superpos.values())
|
144 |
+
pos = {}
|
145 |
+
for center, comm in zip(centers, communities):
|
146 |
+
pos.update(nx.spring_layout(nx.subgraph(G, comm), center=center, seed=1430))
|
147 |
+
|
148 |
+
# Nodes colored by cluster
|
149 |
+
for nodes, clr in zip(communities, ("tab:blue", "tab:orange", "tab:green")):
|
150 |
+
nx.draw_networkx_nodes(G, pos=pos, nodelist=nodes, node_color=clr, node_size=100)
|
151 |
+
nx.draw_networkx_edges(G, pos=pos)
|
152 |
+
|
153 |
+
plt.tight_layout()
|
154 |
+
st.pyplot(plt)
|
155 |
+
|
156 |
+
elif option == "Create your own":
|
157 |
+
st.write("For custom graph creation, you can specify the number of nodes and edges.")
|
158 |
+
# Here you can add functionality for custom graph creation if required
|
159 |
+
|
160 |
# Display Basic: Properties if selected
|
161 |
if sidebar_option == "Basic: Properties":
|
162 |
st.title("Basic: Properties")
|
|
|
261 |
# Display Drawing: Custom Node Position if selected
|
262 |
elif sidebar_option == "Drawing: Custom Node Position":
|
263 |
display_custom_node_position()
|
264 |
+
|
265 |
+
# Display Drawing: Cluster Layout if selected
|
266 |
+
elif sidebar_option == "Drawing: Cluster Layout":
|
267 |
+
display_cluster_layout()
|