Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -77,7 +77,7 @@ def display_read_write_graph(G):
|
|
77 |
st.pyplot(plt)
|
78 |
|
79 |
# Function to display Simple Graphs for Basic: Simple graph
|
80 |
-
def display_simple_graph(G, pos=None):
|
81 |
options = {
|
82 |
"font_size": 36,
|
83 |
"node_size": 3000,
|
@@ -86,9 +86,11 @@ def display_simple_graph(G, pos=None):
|
|
86 |
"linewidths": 5,
|
87 |
"width": 5,
|
88 |
}
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
92 |
|
93 |
# Set margins for the axes so that nodes aren't clipped
|
94 |
ax = plt.gca()
|
@@ -143,19 +145,21 @@ elif sidebar_option == "Basic: Simple graph":
|
|
143 |
st.title("Basic: Simple graph")
|
144 |
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
145 |
|
146 |
-
# Default example:
|
147 |
if option == "Default Example":
|
148 |
-
G = nx.
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
pos
|
158 |
-
|
|
|
|
|
159 |
|
160 |
# Create your own graph
|
161 |
elif option == "Create your own":
|
@@ -175,9 +179,9 @@ elif sidebar_option == "Basic: Simple graph":
|
|
175 |
|
176 |
# Button to generate the graph
|
177 |
if st.button("Generate"):
|
178 |
-
G_custom = nx.
|
179 |
G_custom.add_edges_from(edges)
|
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)
|
|
|
77 |
st.pyplot(plt)
|
78 |
|
79 |
# Function to display Simple Graphs for Basic: Simple graph
|
80 |
+
def display_simple_graph(G, pos=None, directed=False):
|
81 |
options = {
|
82 |
"font_size": 36,
|
83 |
"node_size": 3000,
|
|
|
86 |
"linewidths": 5,
|
87 |
"width": 5,
|
88 |
}
|
89 |
+
|
90 |
+
if directed:
|
91 |
+
nx.draw_networkx(G, pos, **options, arrows=True) # For directed graph with arrows
|
92 |
+
else:
|
93 |
+
nx.draw_networkx(G, pos, **options)
|
94 |
|
95 |
# Set margins for the axes so that nodes aren't clipped
|
96 |
ax = plt.gca()
|
|
|
145 |
st.title("Basic: Simple graph")
|
146 |
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
147 |
|
148 |
+
# Default example: directed graph with custom positions
|
149 |
if option == "Default Example":
|
150 |
+
G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
|
151 |
+
|
152 |
+
# Group nodes by column
|
153 |
+
left_nodes = [0, 1, 2]
|
154 |
+
middle_nodes = [3, 4]
|
155 |
+
right_nodes = [5, 6]
|
156 |
+
|
157 |
+
# Set the position according to column (x-coordinates)
|
158 |
+
pos = {n: (0, i) for i, n in enumerate(left_nodes)}
|
159 |
+
pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
|
160 |
+
pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
|
161 |
+
|
162 |
+
display_simple_graph(G, pos, directed=True)
|
163 |
|
164 |
# Create your own graph
|
165 |
elif option == "Create your own":
|
|
|
179 |
|
180 |
# Button to generate the graph
|
181 |
if st.button("Generate"):
|
182 |
+
G_custom = nx.DiGraph() # Directed graph
|
183 |
G_custom.add_edges_from(edges)
|
184 |
|
185 |
# Set a basic layout (spring layout as default)
|
186 |
pos = nx.spring_layout(G_custom, seed=42)
|
187 |
+
display_simple_graph(G_custom, pos, directed=True)
|