shukdevdatta123 commited on
Commit
34f9627
·
verified ·
1 Parent(s): dea80b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -1
app.py CHANGED
@@ -10,7 +10,8 @@ sidebar_option = st.sidebar.radio("Select an option",
10
  "Basic: Read and write graphs", "Basic: Simple graph",
11
  "Basic: Simple graph Directed", "Drawing: Custom Node Position",
12
  "Drawing: Cluster Layout", "Drawing: Degree Analysis",
13
- "Drawing: Ego Graph", "Drawing: Eigenvalues"])
 
14
 
15
  # Helper function to draw and display graph
16
  def draw_graph(G, pos=None, title="Graph Visualization"):
@@ -18,6 +19,103 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
18
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
19
  st.pyplot(plt)
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Function to display Eigenvalue analysis for Drawing: Eigenvalues
22
  def display_eigenvalue_analysis():
23
  st.title("Drawing: Eigenvalues")
 
10
  "Basic: Read and write graphs", "Basic: Simple graph",
11
  "Basic: Simple graph Directed", "Drawing: Custom Node Position",
12
  "Drawing: Cluster Layout", "Drawing: Degree Analysis",
13
+ "Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids"])
14
+
15
 
16
  # Helper function to draw and display graph
17
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
19
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
20
  st.pyplot(plt)
21
 
22
+ # Function to display Four Grids visualization for Drawing: Four Grids
23
+ def display_four_grids():
24
+ st.title("Drawing: Four Grids")
25
+
26
+ option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
27
+
28
+ if option == "Default Example":
29
+ # Generate a 4x4 grid graph
30
+ G = nx.grid_2d_graph(4, 4) # 4x4 grid
31
+ pos = nx.spring_layout(G, iterations=100, seed=39775)
32
+
33
+ # Create a 2x2 subplot
34
+ fig, all_axes = plt.subplots(2, 2)
35
+ ax = all_axes.flat
36
+
37
+ # Draw graphs in 4 different styles
38
+ nx.draw(G, pos, ax=ax[0], font_size=8)
39
+ nx.draw(G, pos, ax=ax[1], node_size=0, with_labels=False)
40
+ nx.draw(
41
+ G,
42
+ pos,
43
+ ax=ax[2],
44
+ node_color="tab:green",
45
+ edgecolors="tab:gray", # Node surface color
46
+ edge_color="tab:gray", # Color of graph edges
47
+ node_size=250,
48
+ with_labels=False,
49
+ width=6,
50
+ )
51
+ H = G.to_directed()
52
+ nx.draw(
53
+ H,
54
+ pos,
55
+ ax=ax[3],
56
+ node_color="tab:orange",
57
+ node_size=20,
58
+ with_labels=False,
59
+ arrowsize=10,
60
+ width=2,
61
+ )
62
+
63
+ # Set margins for the axes so that nodes aren't clipped
64
+ for a in ax:
65
+ a.margins(0.10)
66
+ fig.tight_layout()
67
+ st.pyplot(fig)
68
+
69
+ elif option == "Create your own":
70
+ # Allow the user to customize the grid dimensions
71
+ rows = st.number_input("Number of rows:", min_value=2, max_value=20, value=4)
72
+ cols = st.number_input("Number of columns:", min_value=2, max_value=20, value=4)
73
+
74
+ if st.button("Generate"):
75
+ # Generate a custom grid graph
76
+ G_custom = nx.grid_2d_graph(rows, cols) # Create the grid graph
77
+ pos = nx.spring_layout(G_custom, iterations=100, seed=39775)
78
+
79
+ # Create a 2x2 subplot
80
+ fig, all_axes = plt.subplots(2, 2)
81
+ ax = all_axes.flat
82
+
83
+ # Draw graphs in 4 different styles
84
+ nx.draw(G_custom, pos, ax=ax[0], font_size=8)
85
+ nx.draw(G_custom, pos, ax=ax[1], node_size=0, with_labels=False)
86
+ nx.draw(
87
+ G_custom,
88
+ pos,
89
+ ax=ax[2],
90
+ node_color="tab:green",
91
+ edgecolors="tab:gray", # Node surface color
92
+ edge_color="tab:gray", # Color of graph edges
93
+ node_size=250,
94
+ with_labels=False,
95
+ width=6,
96
+ )
97
+ H = G_custom.to_directed()
98
+ nx.draw(
99
+ H,
100
+ pos,
101
+ ax=ax[3],
102
+ node_color="tab:orange",
103
+ node_size=20,
104
+ with_labels=False,
105
+ arrowsize=10,
106
+ width=2,
107
+ )
108
+
109
+ # Set margins for the axes so that nodes aren't clipped
110
+ for a in ax:
111
+ a.margins(0.10)
112
+ fig.tight_layout()
113
+ st.pyplot(fig)
114
+
115
+ # Display Drawing: Four Grids if selected
116
+ if sidebar_option == "Drawing: Four Grids":
117
+ display_four_grids()
118
+
119
  # Function to display Eigenvalue analysis for Drawing: Eigenvalues
120
  def display_eigenvalue_analysis():
121
  st.title("Drawing: Eigenvalues")