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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -2
app.py CHANGED
@@ -2,9 +2,10 @@ import streamlit as st
2
  import matplotlib.pyplot as plt
3
  import networkx as nx
4
 
5
- # Add a sidebar with options
6
  sidebar_option = st.sidebar.radio("Select an option",
7
- ["Select an option", "Basic: Properties", "Basic: Read and write graphs"])
 
8
 
9
  # Function to display properties and graph for Basic: Properties
10
  def display_graph_properties(G):
@@ -75,6 +76,26 @@ def display_read_write_graph(G):
75
  nx.draw(H, pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
76
  st.pyplot(plt)
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  # Display Basic: Properties if selected
79
  if sidebar_option == "Basic: Properties":
80
  st.title("Basic: Properties")
@@ -116,3 +137,47 @@ elif sidebar_option == "Basic: Read and write graphs":
116
  if rows >= 2 and cols >= 2:
117
  G_custom = nx.grid_2d_graph(rows, cols)
118
  display_read_write_graph(G_custom)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import matplotlib.pyplot as plt
3
  import networkx as nx
4
 
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):
 
76
  nx.draw(H, pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
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,
84
+ "node_color": "white",
85
+ "edgecolors": "black",
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()
95
+ ax.margins(0.20)
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")
 
137
  if rows >= 2 and cols >= 2:
138
  G_custom = nx.grid_2d_graph(rows, cols)
139
  display_read_write_graph(G_custom)
140
+
141
+ # Display Basic: Simple Graph if selected
142
+ 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":
162
+ num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=5)
163
+ edges = []
164
+
165
+ # Let the user define edges
166
+ st.write("Enter the edges (as pairs of nodes) separated by commas. For example, 1,2 or 3,4.")
167
+ edge_input = st.text_area("Edges:", value="1,2\n1,3\n2,3")
168
+
169
+ # Parse the edges
170
+ if edge_input:
171
+ edge_list = edge_input.split("\n")
172
+ for edge in edge_list:
173
+ u, v = map(int, edge.split(","))
174
+ edges.append((u, v))
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)