Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
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", "Graph: Erdos Renyi"])
|
25 |
|
26 |
# Helper function to draw and display graph
|
27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -29,6 +29,58 @@ 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 erdos_renyi_graph():
|
33 |
st.title("Graph: Erdos Renyi")
|
34 |
|
|
|
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", "Graph: Erdos Renyi", "Graph: Karate Club"])
|
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 karate_club_graph():
|
33 |
+
st.title("Graph: Karate Club")
|
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 the Karate Club graph, or you can create your own graph."
|
40 |
+
)
|
41 |
+
|
42 |
+
if graph_mode == "Default Example":
|
43 |
+
# Load the Karate Club graph
|
44 |
+
G = nx.karate_club_graph()
|
45 |
+
|
46 |
+
# Display node degree
|
47 |
+
st.write("### Node Degree")
|
48 |
+
for v in G:
|
49 |
+
st.write(f"Node {v:4}: Degree = {G.degree(v)}")
|
50 |
+
|
51 |
+
# Visualize the graph using circular layout
|
52 |
+
st.write("### Graph Visualization")
|
53 |
+
fig, ax = plt.subplots()
|
54 |
+
nx.draw_circular(G, with_labels=True, ax=ax, node_color="skyblue", edge_color="gray")
|
55 |
+
ax.set_title("Karate Club Graph")
|
56 |
+
st.pyplot(fig)
|
57 |
+
|
58 |
+
elif graph_mode == "Create Your Own":
|
59 |
+
st.write("### Create Your Own Graph")
|
60 |
+
|
61 |
+
# Allow user to input the number of nodes and edges for custom graph
|
62 |
+
num_nodes = st.number_input("Number of nodes", min_value=2, value=10)
|
63 |
+
num_edges = st.number_input("Number of edges", min_value=1, value=15)
|
64 |
+
|
65 |
+
# Create random graph with user input
|
66 |
+
G = nx.gnm_random_graph(num_nodes, num_edges)
|
67 |
+
|
68 |
+
# Display node degree
|
69 |
+
st.write("### Node Degree")
|
70 |
+
for v in G:
|
71 |
+
st.write(f"Node {v:4}: Degree = {G.degree(v)}")
|
72 |
+
|
73 |
+
# Visualize the graph using circular layout
|
74 |
+
st.write("### Graph Visualization")
|
75 |
+
fig, ax = plt.subplots()
|
76 |
+
nx.draw_circular(G, with_labels=True, ax=ax, node_color="lightgreen", edge_color="gray")
|
77 |
+
ax.set_title("Custom Graph")
|
78 |
+
st.pyplot(fig)
|
79 |
+
|
80 |
+
# Display the corresponding page based on sidebar option
|
81 |
+
if sidebar_option == "Graph: Karate Club":
|
82 |
+
karate_club_graph()
|
83 |
+
|
84 |
def erdos_renyi_graph():
|
85 |
st.title("Graph: Erdos Renyi")
|
86 |
|