Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,8 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
23 |
-
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib"
|
|
|
24 |
|
25 |
# Helper function to draw and display graph
|
26 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -28,6 +29,82 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
28 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
29 |
st.pyplot(plt)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
if sidebar_option == "3D Drawing: Animations of 3D Rotation":
|
32 |
st.title("3D Drawing: Animations of 3D Rotation")
|
33 |
|
|
|
20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
23 |
+
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
24 |
+
"Graph: DAG - Topological Layout"])
|
25 |
|
26 |
# Helper function to draw and display graph
|
27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
29 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
30 |
st.pyplot(plt)
|
31 |
|
32 |
+
def dag_topological_layout():
|
33 |
+
st.title("Graph: DAG - Topological Layout")
|
34 |
+
|
35 |
+
# Sidebar selection for Default Example or Custom Graph
|
36 |
+
graph_mode = st.radio(
|
37 |
+
"Choose a Mode:",
|
38 |
+
("Default Example", "Create Your Own"),
|
39 |
+
help="Default example shows DAG layout in topological order, or you can create your own DAG."
|
40 |
+
)
|
41 |
+
|
42 |
+
if graph_mode == "Default Example":
|
43 |
+
# Create a Directed Acyclic Graph (DAG)
|
44 |
+
G = nx.DiGraph(
|
45 |
+
[
|
46 |
+
("f", "a"),
|
47 |
+
("a", "b"),
|
48 |
+
("a", "e"),
|
49 |
+
("b", "c"),
|
50 |
+
("b", "d"),
|
51 |
+
("d", "e"),
|
52 |
+
("f", "c"),
|
53 |
+
("f", "g"),
|
54 |
+
("h", "f"),
|
55 |
+
]
|
56 |
+
)
|
57 |
+
|
58 |
+
# Add layer attribute for multipartite_layout
|
59 |
+
for layer, nodes in enumerate(nx.topological_generations(G)):
|
60 |
+
for node in nodes:
|
61 |
+
G.nodes[node]["layer"] = layer
|
62 |
+
|
63 |
+
# Compute the multipartite_layout using the "layer" node attribute
|
64 |
+
pos = nx.multipartite_layout(G, subset_key="layer")
|
65 |
+
|
66 |
+
# Draw the graph
|
67 |
+
fig, ax = plt.subplots()
|
68 |
+
nx.draw_networkx(G, pos=pos, ax=ax)
|
69 |
+
ax.set_title("DAG layout in topological order")
|
70 |
+
fig.tight_layout()
|
71 |
+
st.pyplot(fig)
|
72 |
+
|
73 |
+
elif graph_mode == "Create Your Own":
|
74 |
+
st.write("### Custom DAG Creation")
|
75 |
+
|
76 |
+
# Inputs for custom DAG creation (nodes and edges)
|
77 |
+
num_nodes = st.slider("Number of Nodes", min_value=2, max_value=10, value=5)
|
78 |
+
edges = []
|
79 |
+
|
80 |
+
for i in range(num_nodes):
|
81 |
+
for j in range(i + 1, num_nodes):
|
82 |
+
if st.checkbox(f"Add edge from {i} to {j}", value=False):
|
83 |
+
edges.append((i, j))
|
84 |
+
|
85 |
+
# Create the custom DAG
|
86 |
+
G_custom = nx.DiGraph()
|
87 |
+
G_custom.add_edges_from(edges)
|
88 |
+
|
89 |
+
# Add layer attribute for multipartite_layout
|
90 |
+
for layer, nodes in enumerate(nx.topological_generations(G_custom)):
|
91 |
+
for node in nodes:
|
92 |
+
G_custom.nodes[node]["layer"] = layer
|
93 |
+
|
94 |
+
# Compute the multipartite_layout using the "layer" node attribute
|
95 |
+
pos_custom = nx.multipartite_layout(G_custom, subset_key="layer")
|
96 |
+
|
97 |
+
# Draw the custom DAG
|
98 |
+
fig_custom, ax_custom = plt.subplots()
|
99 |
+
nx.draw_networkx(G_custom, pos=pos_custom, ax=ax_custom)
|
100 |
+
ax_custom.set_title("Custom DAG layout in topological order")
|
101 |
+
fig_custom.tight_layout()
|
102 |
+
st.pyplot(fig_custom)
|
103 |
+
|
104 |
+
# Display the corresponding page based on sidebar option
|
105 |
+
if sidebar_option == "Graph: DAG - Topological Layout":
|
106 |
+
dag_topological_layout()
|
107 |
+
|
108 |
if sidebar_option == "3D Drawing: Animations of 3D Rotation":
|
109 |
st.title("3D Drawing: Animations of 3D Rotation")
|
110 |
|