Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
14 |
"Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
|
15 |
"Drawing: House With Colors", "Drawing: Labels And Colors",
|
16 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
17 |
-
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph"])
|
18 |
|
19 |
|
20 |
|
@@ -24,6 +24,57 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
24 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
25 |
st.pyplot(plt)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Function to display Drawing: Random Geometric Graph
|
28 |
def display_random_geometric_graph():
|
29 |
st.title("Drawing: Random Geometric Graph")
|
|
|
14 |
"Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
|
15 |
"Drawing: House With Colors", "Drawing: Labels And Colors",
|
16 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
17 |
+
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops"])
|
18 |
|
19 |
|
20 |
|
|
|
24 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
25 |
st.pyplot(plt)
|
26 |
|
27 |
+
# Function to display Drawing: Self-loops
|
28 |
+
def display_self_loops():
|
29 |
+
st.title("Drawing: Self-loops")
|
30 |
+
|
31 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
32 |
+
|
33 |
+
if option == "Default Example":
|
34 |
+
# Default example of a graph with self-loops
|
35 |
+
G = nx.complete_graph(3, create_using=nx.DiGraph)
|
36 |
+
G.add_edge(0, 0) # Add a self-loop to node 0
|
37 |
+
pos = nx.circular_layout(G)
|
38 |
+
|
39 |
+
# Draw the graph
|
40 |
+
nx.draw(G, pos, with_labels=True)
|
41 |
+
|
42 |
+
# Add self-loops to the remaining nodes
|
43 |
+
edgelist = [(1, 1), (2, 2)]
|
44 |
+
G.add_edges_from(edgelist)
|
45 |
+
|
46 |
+
# Draw the newly added self-loops with different formatting
|
47 |
+
nx.draw_networkx_edges(G, pos, edgelist=edgelist, arrowstyle="<|-", style="dashed")
|
48 |
+
st.pyplot(plt)
|
49 |
+
|
50 |
+
elif option == "Create your own":
|
51 |
+
# User can create their own graph with self-loops
|
52 |
+
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=3)
|
53 |
+
add_self_loops = st.checkbox("Add self-loops to all nodes?", value=True)
|
54 |
+
|
55 |
+
if st.button("Generate Graph"):
|
56 |
+
# Generate a complete graph
|
57 |
+
G = nx.complete_graph(num_nodes, create_using=nx.DiGraph)
|
58 |
+
|
59 |
+
# Optionally add self-loops to all nodes
|
60 |
+
if add_self_loops:
|
61 |
+
for node in G.nodes():
|
62 |
+
G.add_edge(node, node)
|
63 |
+
|
64 |
+
pos = nx.circular_layout(G)
|
65 |
+
|
66 |
+
# Draw the graph with self-loops
|
67 |
+
nx.draw(G, pos, with_labels=True)
|
68 |
+
|
69 |
+
# Style self-loops differently
|
70 |
+
edgelist = [(node, node) for node in G.nodes()]
|
71 |
+
nx.draw_networkx_edges(G, pos, edgelist=edgelist, arrowstyle="<|-", style="dashed")
|
72 |
+
st.pyplot(plt)
|
73 |
+
|
74 |
+
# Display Drawing: Self-loops if selected
|
75 |
+
if sidebar_option == "Drawing: Self-loops":
|
76 |
+
display_self_loops()
|
77 |
+
|
78 |
# Function to display Drawing: Random Geometric Graph
|
79 |
def display_random_geometric_graph():
|
80 |
st.title("Drawing: Random Geometric Graph")
|