Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import networkx as nx
|
|
| 5 |
# Sidebar for selecting an option
|
| 6 |
sidebar_option = st.sidebar.radio("Select an option",
|
| 7 |
["Select an option", "Basic: Properties",
|
| 8 |
-
"Basic: Read and write graphs", "Basic: Simple graph"])
|
| 9 |
|
| 10 |
# Function to display properties and graph for Basic: Properties
|
| 11 |
def display_graph_properties(G):
|
|
@@ -96,6 +96,25 @@ def display_simple_graph(G, pos=None):
|
|
| 96 |
plt.axis("off")
|
| 97 |
st.pyplot(plt)
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
# Display Basic: Properties if selected
|
| 100 |
if sidebar_option == "Basic: Properties":
|
| 101 |
st.title("Basic: Properties")
|
|
@@ -180,4 +199,51 @@ elif sidebar_option == "Basic: Simple graph":
|
|
| 180 |
|
| 181 |
# Set a basic layout (spring layout as default)
|
| 182 |
pos = nx.spring_layout(G_custom, seed=42)
|
| 183 |
-
display_simple_graph(G_custom, pos)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Sidebar for selecting an option
|
| 6 |
sidebar_option = st.sidebar.radio("Select an option",
|
| 7 |
["Select an option", "Basic: Properties",
|
| 8 |
+
"Basic: Read and write graphs", "Basic: Simple graph", "Basic: Simple graph Directed"])
|
| 9 |
|
| 10 |
# Function to display properties and graph for Basic: Properties
|
| 11 |
def display_graph_properties(G):
|
|
|
|
| 96 |
plt.axis("off")
|
| 97 |
st.pyplot(plt)
|
| 98 |
|
| 99 |
+
# Function to display Simple Directed Graphs for Basic: Simple graph Directed
|
| 100 |
+
def display_simple_directed_graph(G, pos=None):
|
| 101 |
+
options = {
|
| 102 |
+
"node_size": 500,
|
| 103 |
+
"node_color": "lightblue",
|
| 104 |
+
"arrowsize": 20,
|
| 105 |
+
"width": 2,
|
| 106 |
+
"edge_color": "gray",
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
# Draw the directed graph with the given positions and options
|
| 110 |
+
nx.draw_networkx(G, pos, **options)
|
| 111 |
+
|
| 112 |
+
# Set margins for the axes so that nodes aren't clipped
|
| 113 |
+
ax = plt.gca()
|
| 114 |
+
ax.margins(0.20)
|
| 115 |
+
plt.axis("off")
|
| 116 |
+
st.pyplot(plt)
|
| 117 |
+
|
| 118 |
# Display Basic: Properties if selected
|
| 119 |
if sidebar_option == "Basic: Properties":
|
| 120 |
st.title("Basic: Properties")
|
|
|
|
| 199 |
|
| 200 |
# Set a basic layout (spring layout as default)
|
| 201 |
pos = nx.spring_layout(G_custom, seed=42)
|
| 202 |
+
display_simple_graph(G_custom, pos)
|
| 203 |
+
|
| 204 |
+
# Display Basic: Simple Directed Graph if selected
|
| 205 |
+
elif sidebar_option == "Basic: Simple graph Directed":
|
| 206 |
+
st.title("Basic: Simple graph Directed")
|
| 207 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 208 |
+
|
| 209 |
+
# Default example: simple directed graph
|
| 210 |
+
if option == "Default Example":
|
| 211 |
+
G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
|
| 212 |
+
|
| 213 |
+
# Group nodes by column
|
| 214 |
+
left_nodes = [0, 1, 2]
|
| 215 |
+
middle_nodes = [3, 4]
|
| 216 |
+
right_nodes = [5, 6]
|
| 217 |
+
|
| 218 |
+
# Set the position according to column (x-coord)
|
| 219 |
+
pos = {n: (0, i) for i, n in enumerate(left_nodes)}
|
| 220 |
+
pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
|
| 221 |
+
pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
|
| 222 |
+
|
| 223 |
+
# Draw the directed graph
|
| 224 |
+
display_simple_directed_graph(G, pos)
|
| 225 |
+
|
| 226 |
+
# Create your own directed graph
|
| 227 |
+
elif option == "Create your own":
|
| 228 |
+
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=5)
|
| 229 |
+
edges = []
|
| 230 |
+
|
| 231 |
+
# Let the user define directed edges
|
| 232 |
+
st.write("Enter the directed edges (as pairs of nodes) separated by commas. For example, 1,2 or 3,4.")
|
| 233 |
+
edge_input = st.text_area("Edges:", value="1,2\n1,3\n2,3")
|
| 234 |
+
|
| 235 |
+
# Parse the edges
|
| 236 |
+
if edge_input:
|
| 237 |
+
edge_list = edge_input.split("\n")
|
| 238 |
+
for edge in edge_list:
|
| 239 |
+
u, v = map(int, edge.split(","))
|
| 240 |
+
edges.append((u, v))
|
| 241 |
+
|
| 242 |
+
# Button to generate the directed graph
|
| 243 |
+
if st.button("Generate"):
|
| 244 |
+
G_custom = nx.DiGraph()
|
| 245 |
+
G_custom.add_edges_from(edges)
|
| 246 |
+
|
| 247 |
+
# Set a basic layout (spring layout as default)
|
| 248 |
+
pos = nx.spring_layout(G_custom, seed=42)
|
| 249 |
+
display_simple_directed_graph(G_custom, pos)
|