Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
| 11 |
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
| 12 |
"Drawing: Cluster Layout", "Drawing: Degree Analysis",
|
| 13 |
"Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
|
| 14 |
-
"Drawing: House With Colors"])
|
| 15 |
|
| 16 |
# Helper function to draw and display graph
|
| 17 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
@@ -19,6 +19,87 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
| 19 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
| 20 |
st.pyplot(plt)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Function to display Drawing: House With Colors
|
| 23 |
def display_house_with_colors():
|
| 24 |
st.title("Drawing: House With Colors")
|
|
|
|
| 11 |
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
| 12 |
"Drawing: Cluster Layout", "Drawing: Degree Analysis",
|
| 13 |
"Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
|
| 14 |
+
"Drawing: House With Colors", "Drawing: Labels And Colors"])
|
| 15 |
|
| 16 |
# Helper function to draw and display graph
|
| 17 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
|
| 19 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
| 20 |
st.pyplot(plt)
|
| 21 |
|
| 22 |
+
# Function to display Drawing: Labels And Colors
|
| 23 |
+
def display_labels_and_colors():
|
| 24 |
+
st.title("Drawing: Labels And Colors")
|
| 25 |
+
|
| 26 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 27 |
+
|
| 28 |
+
if option == "Default Example":
|
| 29 |
+
# Create a cubical graph
|
| 30 |
+
G = nx.cubical_graph()
|
| 31 |
+
pos = nx.spring_layout(G, seed=3113794652) # positions for all nodes
|
| 32 |
+
|
| 33 |
+
# Draw nodes with different colors
|
| 34 |
+
options = {"edgecolors": "tab:gray", "node_size": 800, "alpha": 0.9}
|
| 35 |
+
nx.draw_networkx_nodes(G, pos, nodelist=[0, 1, 2, 3], node_color="tab:red", **options)
|
| 36 |
+
nx.draw_networkx_nodes(G, pos, nodelist=[4, 5, 6, 7], node_color="tab:blue", **options)
|
| 37 |
+
|
| 38 |
+
# Draw edges
|
| 39 |
+
nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
|
| 40 |
+
nx.draw_networkx_edges(
|
| 41 |
+
G,
|
| 42 |
+
pos,
|
| 43 |
+
edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
|
| 44 |
+
width=8,
|
| 45 |
+
alpha=0.5,
|
| 46 |
+
edge_color="tab:red",
|
| 47 |
+
)
|
| 48 |
+
nx.draw_networkx_edges(
|
| 49 |
+
G,
|
| 50 |
+
pos,
|
| 51 |
+
edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
|
| 52 |
+
width=8,
|
| 53 |
+
alpha=0.5,
|
| 54 |
+
edge_color="tab:blue",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Add labels for nodes
|
| 58 |
+
labels = {0: r"$a$", 1: r"$b$", 2: r"$c$", 3: r"$d$", 4: r"$\alpha$", 5: r"$\beta$", 6: r"$\gamma$", 7: r"$\delta$"}
|
| 59 |
+
nx.draw_networkx_labels(G, pos, labels, font_size=22, font_color="whitesmoke")
|
| 60 |
+
|
| 61 |
+
plt.tight_layout()
|
| 62 |
+
plt.axis("off")
|
| 63 |
+
st.pyplot(plt)
|
| 64 |
+
|
| 65 |
+
elif option == "Create your own":
|
| 66 |
+
# Let the user input the nodes and edges of the graph
|
| 67 |
+
st.write("Enter the nodes and edges to create your own labeled graph.")
|
| 68 |
+
|
| 69 |
+
nodes = st.text_area("Enter node labels (comma-separated, e.g., a,b,c,d):", value="a,b,c,d")
|
| 70 |
+
node_labels = nodes.split(',')
|
| 71 |
+
|
| 72 |
+
edges = st.text_area("Enter edges (format: node1-node2, comma-separated, e.g., a-b,b-c):", value="a-b,b-c,c-d")
|
| 73 |
+
edge_list = [tuple(edge.split('-')) for edge in edges.split(',')]
|
| 74 |
+
|
| 75 |
+
# Generate graph based on user input
|
| 76 |
+
G_custom = nx.Graph()
|
| 77 |
+
G_custom.add_nodes_from(node_labels)
|
| 78 |
+
G_custom.add_edges_from(edge_list)
|
| 79 |
+
|
| 80 |
+
# Let user choose colors for nodes and edges
|
| 81 |
+
node_color = st.color_picker("Pick a color for nodes:", "#FF6347")
|
| 82 |
+
edge_color = st.color_picker("Pick a color for edges:", "#4682B4")
|
| 83 |
+
|
| 84 |
+
# Generate layout for the nodes
|
| 85 |
+
pos_custom = nx.spring_layout(G_custom)
|
| 86 |
+
|
| 87 |
+
# Draw the graph
|
| 88 |
+
nx.draw_networkx_nodes(G_custom, pos_custom, node_color=node_color, node_size=800, edgecolors="gray", alpha=0.9)
|
| 89 |
+
nx.draw_networkx_edges(G_custom, pos_custom, edge_color=edge_color, width=2, alpha=0.7)
|
| 90 |
+
|
| 91 |
+
# Create custom labels
|
| 92 |
+
custom_labels = {node: f"${node}$" for node in node_labels}
|
| 93 |
+
nx.draw_networkx_labels(G_custom, pos_custom, labels=custom_labels, font_size=22, font_color="whitesmoke")
|
| 94 |
+
|
| 95 |
+
plt.tight_layout()
|
| 96 |
+
plt.axis("off")
|
| 97 |
+
st.pyplot(plt)
|
| 98 |
+
|
| 99 |
+
# Display Drawing: Labels And Colors if selected
|
| 100 |
+
if sidebar_option == "Drawing: Labels And Colors":
|
| 101 |
+
display_labels_and_colors()
|
| 102 |
+
|
| 103 |
# Function to display Drawing: House With Colors
|
| 104 |
def display_house_with_colors():
|
| 105 |
st.title("Drawing: House With Colors")
|