shukdevdatta123 commited on
Commit
8f75d16
·
verified ·
1 Parent(s): 67e7fee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -10
app.py CHANGED
@@ -3,9 +3,10 @@ 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", ["Select an option", "Basic: Properties"])
 
7
 
8
- # Function to display properties and graph
9
  def display_graph_properties(G):
10
  # Initialize a list for path lengths
11
  pathlengths = []
@@ -50,25 +51,64 @@ def display_graph_properties(G):
50
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
51
  st.pyplot(plt)
52
 
53
- # Only display Basic: Properties when it is selected from the sidebar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  if sidebar_option == "Basic: Properties":
55
- # Set the title for the page
56
  st.title("Basic: Properties")
57
-
58
- # Add a radio button for selecting the graph type
59
  option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
60
 
61
- # Default example
62
  if option == "Default Example":
63
  G = nx.lollipop_graph(4, 6)
64
  display_graph_properties(G)
65
 
66
  # Create your own graph
67
  elif option == "Create your own":
68
- # Let the user input number of nodes and edges
69
  num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
70
  num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
71
 
72
  if num_nodes >= 2 and num_edges >= 1:
73
- G = nx.lollipop_graph(num_nodes, num_edges)
74
- display_graph_properties(G)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
11
  # Initialize a list for path lengths
12
  pathlengths = []
 
51
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
52
  st.pyplot(plt)
53
 
54
+ # Function to display graph for Basic: Read and write graphs
55
+ def display_read_write_graph(G):
56
+ # Print the adjacency list of the graph
57
+ st.write("### Adjacency List:")
58
+ for line in nx.generate_adjlist(G):
59
+ st.write(line)
60
+
61
+ # Write the graph's edge list to a file
62
+ st.write("### Writing Edge List to 'grid.edgelist' file:")
63
+ nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
64
+ st.write("Edge list written to 'grid.edgelist'")
65
+
66
+ # Read the graph from the edge list
67
+ st.write("### Reading Edge List from 'grid.edgelist' file:")
68
+ H = nx.read_edgelist(path="grid.edgelist", delimiter=":")
69
+ st.write("Edge list read into graph H")
70
+
71
+ # Visualize the graph
72
+ st.write("### Graph Visualization:")
73
+ pos = nx.spring_layout(H, seed=200) # Seed for reproducibility
74
+ plt.figure(figsize=(8, 6))
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")
 
 
81
  option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
82
 
83
+ # Default example: 5x5 grid graph
84
  if option == "Default Example":
85
  G = nx.lollipop_graph(4, 6)
86
  display_graph_properties(G)
87
 
88
  # Create your own graph
89
  elif option == "Create your own":
 
90
  num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
91
  num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
92
 
93
  if num_nodes >= 2 and num_edges >= 1:
94
+ G_custom = nx.lollipop_graph(num_nodes, num_edges)
95
+ display_graph_properties(G_custom)
96
+
97
+ # Display Basic: Read and write graphs if selected
98
+ elif sidebar_option == "Basic: Read and write graphs":
99
+ st.title("Basic: Read and write graphs")
100
+ option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
101
+
102
+ # Default example: 5x5 grid graph
103
+ if option == "Default Example":
104
+ G = nx.grid_2d_graph(5, 5) # 5x5 grid
105
+ display_read_write_graph(G)
106
+
107
+ # Create your own graph
108
+ elif option == "Create your own":
109
+ rows = st.number_input("Number of rows:", min_value=2, max_value=20, value=5)
110
+ cols = st.number_input("Number of columns:", min_value=2, max_value=20, value=5)
111
+
112
+ if rows >= 2 and cols >= 2:
113
+ G_custom = nx.grid_2d_graph(rows, cols)
114
+ display_read_write_graph(G_custom)