shukdevdatta123 commited on
Commit
98c04d6
·
verified ·
1 Parent(s): 13911ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -45
app.py CHANGED
@@ -5,48 +5,65 @@ import networkx as nx
5
  # Add a headline
6
  st.title("Basic: Properties")
7
 
8
- # Create lollipop graph
9
- G = nx.lollipop_graph(4, 6)
10
-
11
- # Initialize a list for path lengths
12
- pathlengths = []
13
-
14
- # Display the source-target shortest path lengths
15
- st.write("### Source vertex {target:length, }")
16
- for v in G.nodes():
17
- spl = dict(nx.single_source_shortest_path_length(G, v))
18
- st.write(f"Vertex {v}: {spl}")
19
- for p in spl:
20
- pathlengths.append(spl[p])
21
-
22
- # Calculate and display the average shortest path length
23
- avg_path_length = sum(pathlengths) / len(pathlengths)
24
- st.write(f"### Average shortest path length: {avg_path_length}")
25
-
26
- # Calculate and display the distribution of path lengths
27
- dist = {}
28
- for p in pathlengths:
29
- if p in dist:
30
- dist[p] += 1
31
- else:
32
- dist[p] = 1
33
-
34
- st.write("### Length #paths")
35
- for d in sorted(dist.keys()):
36
- st.write(f"Length {d}: {dist[d]} paths")
37
-
38
- # Display the graph metrics with a "Properties" heading
39
- st.write("### Properties")
40
- st.write(f"Radius: {nx.radius(G)}")
41
- st.write(f"Diameter: {nx.diameter(G)}")
42
- st.write(f"Eccentricity: {nx.eccentricity(G)}")
43
- st.write(f"Center: {nx.center(G)}")
44
- st.write(f"Periphery: {nx.periphery(G)}")
45
- st.write(f"Density: {nx.density(G)}")
46
-
47
- # Visualize the graph
48
- st.write("### Graph Visualization")
49
- pos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility
50
- plt.figure(figsize=(8, 6))
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # Add a headline
6
  st.title("Basic: Properties")
7
 
8
+ # Add a radio button for selecting the graph type
9
+ option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
10
+
11
+ # Function to display properties and graph
12
+ def display_graph_properties(G):
13
+ # Initialize a list for path lengths
14
+ pathlengths = []
15
+
16
+ # Display the source-target shortest path lengths
17
+ st.write("### Source vertex {target:length, }")
18
+ for v in G.nodes():
19
+ spl = dict(nx.single_source_shortest_path_length(G, v))
20
+ st.write(f"Vertex {v}: {spl}")
21
+ for p in spl:
22
+ pathlengths.append(spl[p])
23
+
24
+ # Calculate and display the average shortest path length
25
+ avg_path_length = sum(pathlengths) / len(pathlengths)
26
+ st.write(f"### Average shortest path length: {avg_path_length}")
27
+
28
+ # Calculate and display the distribution of path lengths
29
+ dist = {}
30
+ for p in pathlengths:
31
+ if p in dist:
32
+ dist[p] += 1
33
+ else:
34
+ dist[p] = 1
35
+
36
+ st.write("### Length #paths")
37
+ for d in sorted(dist.keys()):
38
+ st.write(f"Length {d}: {dist[d]} paths")
39
+
40
+ # Display the graph metrics with a "Properties" heading
41
+ st.write("### Properties")
42
+ st.write(f"Radius: {nx.radius(G)}")
43
+ st.write(f"Diameter: {nx.diameter(G)}")
44
+ st.write(f"Eccentricity: {nx.eccentricity(G)}")
45
+ st.write(f"Center: {nx.center(G)}")
46
+ st.write(f"Periphery: {nx.periphery(G)}")
47
+ st.write(f"Density: {nx.density(G)}")
48
+
49
+ # Visualize the graph
50
+ st.write("### Graph Visualization")
51
+ pos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility
52
+ plt.figure(figsize=(8, 6))
53
+ nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
54
+ st.pyplot(plt)
55
+
56
+ # Default example
57
+ if option == "Default Example":
58
+ G = nx.lollipop_graph(4, 6)
59
+ display_graph_properties(G)
60
+
61
+ # Create your own graph
62
+ elif option == "Create your own":
63
+ # Let the user input number of nodes and edges
64
+ num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
65
+ num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
66
+
67
+ if num_nodes >= 2 and num_edges >= 1:
68
+ G = nx.lollipop_graph(num_nodes, num_edges)
69
+ display_graph_properties(G)