Update app.py
Browse files
app.py
CHANGED
|
@@ -74,35 +74,40 @@ def display_weighted_graph():
|
|
| 74 |
nodes = st.text_input("Enter nodes (comma-separated):", "a,b,c,d,e,f").split(',')
|
| 75 |
nodes = [node.strip() for node in nodes]
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
weight = st.number_input(f"Weight between {nodes[i]} and {nodes[j]}:", min_value=0.0, value=1.0)
|
| 82 |
-
G_custom.add_edge(nodes[i], nodes[j], weight=weight)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
esmall = [(u, v) for (u, v, d) in G_custom.edges(data=True) if d["weight"] <= 0.5]
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
nx.draw_networkx_edges(G_custom, pos, edgelist=elarge, width=6)
|
| 94 |
-
nx.draw_networkx_edges(
|
| 95 |
-
G_custom, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
|
| 96 |
-
)
|
| 97 |
-
nx.draw_networkx_labels(G_custom, pos, font_size=20, font_family="sans-serif")
|
| 98 |
-
edge_labels = nx.get_edge_attributes(G_custom, "weight")
|
| 99 |
-
nx.draw_networkx_edge_labels(G_custom, pos, edge_labels)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
# Display Drawing: Weighted Graph if selected
|
| 108 |
if sidebar_option == "Drawing: Weighted Graph":
|
|
|
|
| 74 |
nodes = st.text_input("Enter nodes (comma-separated):", "a,b,c,d,e,f").split(',')
|
| 75 |
nodes = [node.strip() for node in nodes]
|
| 76 |
|
| 77 |
+
# Add a button to generate a new graph
|
| 78 |
+
generate_button = st.button("Generate Graph")
|
| 79 |
|
| 80 |
+
if generate_button:
|
| 81 |
+
G_custom = nx.Graph()
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Allow user to input weights for the edges between nodes
|
| 84 |
+
for i in range(len(nodes)):
|
| 85 |
+
for j in range(i + 1, len(nodes)):
|
| 86 |
+
weight = st.number_input(f"Weight between {nodes[i]} and {nodes[j]}:", min_value=0.0, value=1.0)
|
| 87 |
+
G_custom.add_edge(nodes[i], nodes[j], weight=weight)
|
|
|
|
| 88 |
|
| 89 |
+
# Create layout for visualization
|
| 90 |
+
pos = nx.spring_layout(G_custom, seed=7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Determine edges based on weight
|
| 93 |
+
elarge = [(u, v) for (u, v, d) in G_custom.edges(data=True) if d["weight"] > 0.5]
|
| 94 |
+
esmall = [(u, v) for (u, v, d) in G_custom.edges(data=True) if d["weight"] <= 0.5]
|
| 95 |
+
|
| 96 |
+
# Draw the graph
|
| 97 |
+
nx.draw_networkx_nodes(G_custom, pos, node_size=700)
|
| 98 |
+
nx.draw_networkx_edges(G_custom, pos, edgelist=elarge, width=6)
|
| 99 |
+
nx.draw_networkx_edges(
|
| 100 |
+
G_custom, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
|
| 101 |
+
)
|
| 102 |
+
nx.draw_networkx_labels(G_custom, pos, font_size=20, font_family="sans-serif")
|
| 103 |
+
edge_labels = nx.get_edge_attributes(G_custom, "weight")
|
| 104 |
+
nx.draw_networkx_edge_labels(G_custom, pos, edge_labels)
|
| 105 |
+
|
| 106 |
+
ax = plt.gca()
|
| 107 |
+
ax.margins(0.08)
|
| 108 |
+
plt.axis("off")
|
| 109 |
+
plt.tight_layout()
|
| 110 |
+
st.pyplot(plt)
|
| 111 |
|
| 112 |
# Display Drawing: Weighted Graph if selected
|
| 113 |
if sidebar_option == "Drawing: Weighted Graph":
|