Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,8 @@ import numpy as np
|
|
7 |
sidebar_option = st.sidebar.radio("Select an option",
|
8 |
["Select an option", "Basic: Properties",
|
9 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
10 |
-
"Basic: Simple graph Directed", "Drawing: Custom Node Position"
|
|
|
11 |
|
12 |
# Helper function to draw and display graph
|
13 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -56,7 +57,7 @@ def display_read_write_graph(G):
|
|
56 |
|
57 |
# Write the graph's edge list to a file
|
58 |
st.write("### Writing Edge List to 'grid.edgelist' file:")
|
59 |
-
nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
|
60 |
st.write("Edge list written to 'grid.edgelist'")
|
61 |
|
62 |
# Read the graph from the edge list
|
@@ -124,6 +125,45 @@ def display_custom_node_position():
|
|
124 |
# Draw the graph
|
125 |
draw_graph(G, pos)
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
# Display Basic: Properties if selected
|
128 |
if sidebar_option == "Basic: Properties":
|
129 |
st.title("Basic: Properties")
|
@@ -227,4 +267,8 @@ elif sidebar_option == "Basic: Simple graph Directed":
|
|
227 |
|
228 |
# Display Drawing: Custom Node Position if selected
|
229 |
elif sidebar_option == "Drawing: Custom Node Position":
|
230 |
-
display_custom_node_position()
|
|
|
|
|
|
|
|
|
|
7 |
sidebar_option = st.sidebar.radio("Select an option",
|
8 |
["Select an option", "Basic: Properties",
|
9 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
10 |
+
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
11 |
+
"Drawing: Chess Masters"])
|
12 |
|
13 |
# Helper function to draw and display graph
|
14 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
57 |
|
58 |
# Write the graph's edge list to a file
|
59 |
st.write("### Writing Edge List to 'grid.edgelist' file:")
|
60 |
+
nx.write_edgelist(G, path="grid.edgelist", delimiter=":") # Save edge list
|
61 |
st.write("Edge list written to 'grid.edgelist'")
|
62 |
|
63 |
# Read the graph from the edge list
|
|
|
125 |
# Draw the graph
|
126 |
draw_graph(G, pos)
|
127 |
|
128 |
+
# Function to display Chess Masters Graphs for Drawing: Chess Masters
|
129 |
+
def display_chess_masters():
|
130 |
+
st.title("Drawing: Chess Masters")
|
131 |
+
|
132 |
+
# Define the directed graph
|
133 |
+
G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
|
134 |
+
|
135 |
+
# Group nodes by column
|
136 |
+
left_nodes = [0, 1, 2]
|
137 |
+
middle_nodes = [3, 4]
|
138 |
+
right_nodes = [5, 6]
|
139 |
+
|
140 |
+
# Set the position according to column (x-coord)
|
141 |
+
pos = {n: (0, i) for i, n in enumerate(left_nodes)}
|
142 |
+
pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
|
143 |
+
pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
|
144 |
+
|
145 |
+
# Define plot options
|
146 |
+
options = {
|
147 |
+
"node_size": 500,
|
148 |
+
"node_color": "lightblue",
|
149 |
+
"arrowsize": 20,
|
150 |
+
"width": 2,
|
151 |
+
"edge_color": "gray",
|
152 |
+
}
|
153 |
+
|
154 |
+
# Draw the graph with the specified positions and options
|
155 |
+
nx.draw_networkx(G, pos, **options)
|
156 |
+
|
157 |
+
# Set margins for the axes so that nodes aren't clipped
|
158 |
+
ax = plt.gca()
|
159 |
+
ax.margins(0.20)
|
160 |
+
|
161 |
+
# Turn off the axis for better visualization
|
162 |
+
plt.axis("off")
|
163 |
+
|
164 |
+
# Show the plot
|
165 |
+
st.pyplot(plt)
|
166 |
+
|
167 |
# Display Basic: Properties if selected
|
168 |
if sidebar_option == "Basic: Properties":
|
169 |
st.title("Basic: Properties")
|
|
|
267 |
|
268 |
# Display Drawing: Custom Node Position if selected
|
269 |
elif sidebar_option == "Drawing: Custom Node Position":
|
270 |
+
display_custom_node_position()
|
271 |
+
|
272 |
+
# Display Drawing: Chess Masters if selected
|
273 |
+
elif sidebar_option == "Drawing: Chess Masters":
|
274 |
+
display_chess_masters()
|