shukdevdatta123 commited on
Commit
15edc3f
·
verified ·
1 Parent(s): f2eec95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
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
- # Draw the network
91
- nx.draw_networkx(G, pos, **options)
 
 
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: simple undirected graph
147
  if option == "Default Example":
148
- G = nx.Graph()
149
- G.add_edge(1, 2)
150
- G.add_edge(1, 3)
151
- G.add_edge(1, 5)
152
- G.add_edge(2, 3)
153
- G.add_edge(3, 4)
154
- G.add_edge(4, 5)
155
-
156
- # explicitly set positions for visualization
157
- pos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}
158
- display_simple_graph(G, pos)
 
 
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.Graph()
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)