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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -86,7 +86,8 @@ def display_simple_graph(G, pos=None, directed=False):
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:
@@ -145,8 +146,22 @@ elif sidebar_option == "Basic: Simple graph":
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
@@ -159,8 +174,10 @@ elif sidebar_option == "Basic: Simple graph":
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":
166
  num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=5)
@@ -179,9 +196,9 @@ elif sidebar_option == "Basic: Simple graph":
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)
 
86
  "linewidths": 5,
87
  "width": 5,
88
  }
89
+
90
+ # Draw the network
91
  if directed:
92
  nx.draw_networkx(G, pos, **options, arrows=True) # For directed graph with arrows
93
  else:
 
146
  st.title("Basic: Simple graph")
147
  option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
148
 
149
+ # Default example: simple undirected graph
150
  if option == "Default Example":
151
+ G = nx.Graph()
152
+ G.add_edge(1, 2)
153
+ G.add_edge(1, 3)
154
+ G.add_edge(1, 5)
155
+ G.add_edge(2, 3)
156
+ G.add_edge(3, 4)
157
+ G.add_edge(4, 5)
158
+
159
+ # explicitly set positions for visualization
160
+ pos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}
161
+ display_simple_graph(G, pos)
162
+
163
+ # Default example: directed graph with custom positions (added the missing part)
164
+ if option == "Default Example" and st.session_state.graph_displayed == False:
165
  G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
166
 
167
  # Group nodes by column
 
174
  pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
175
  pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
176
 
177
+ # Visualize the directed graph
178
  display_simple_graph(G, pos, directed=True)
179
+ st.session_state.graph_displayed = True
180
+
181
  # Create your own graph
182
  elif option == "Create your own":
183
  num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=5)
 
196
 
197
  # Button to generate the graph
198
  if st.button("Generate"):
199
+ G_custom = nx.Graph()
200
  G_custom.add_edges_from(edges)
201
 
202
  # Set a basic layout (spring layout as default)
203
  pos = nx.spring_layout(G_custom, seed=42)
204
+ display_simple_graph(G_custom, pos)