shukdevdatta123 commited on
Commit
3ec7372
·
verified ·
1 Parent(s): 4c95ee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -1
app.py CHANGED
@@ -5,6 +5,9 @@ import networkx as nx
5
  import numpy as np
6
  from operator import itemgetter
7
  import math # Import the math module
 
 
 
8
 
9
  # Sidebar for selecting an option
10
  sidebar_option = st.sidebar.radio("Select an option",
@@ -17,7 +20,7 @@ sidebar_option = st.sidebar.radio("Select an option",
17
  "Drawing: Multipartite Layout", "Drawing: Node Colormap",
18
  "Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
19
  "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
20
- "Drawing: Weighted Graph"])
21
 
22
  # Helper function to draw and display graph
23
  def draw_graph(G, pos=None, title="Graph Visualization"):
@@ -25,6 +28,70 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
25
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
26
  st.pyplot(plt)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Function to display Weighted Graph
29
  def display_weighted_graph():
30
  st.title("Drawing: Weighted Graph")
 
5
  import numpy as np
6
  from operator import itemgetter
7
  import math # Import the math module
8
+ from matplotlib import animation
9
+ from mpl_toolkits.mplot3d import Axes3D
10
+ from streamlit.components.v1 import html
11
 
12
  # Sidebar for selecting an option
13
  sidebar_option = st.sidebar.radio("Select an option",
 
20
  "Drawing: Multipartite Layout", "Drawing: Node Colormap",
21
  "Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
22
  "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
23
+ "Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation"])
24
 
25
  # Helper function to draw and display graph
26
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
28
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
29
  st.pyplot(plt)
30
 
31
+ if sidebar_option == "3D Drawing: Animations of 3D Rotation":
32
+ st.title("3D Drawing: Animations of 3D Rotation")
33
+
34
+ # Provide options for Default Example or Custom Graph
35
+ graph_mode = st.radio(
36
+ "Choose a Mode:",
37
+ ("Default Example", "Create Your Own"),
38
+ help="Default example shows a dodecahedral graph, or you can create your own custom graph."
39
+ )
40
+
41
+ # Define the function to create animation
42
+ def generate_animation(G, pos, frames=100):
43
+ nodes = np.array([pos[v] for v in G])
44
+ edges = np.array([(pos[u], pos[v]) for u, v in G.edges()])
45
+
46
+ fig = plt.figure()
47
+ ax = fig.add_subplot(111, projection="3d")
48
+
49
+ def init():
50
+ ax.scatter(*nodes.T, alpha=0.2, s=100, color="blue")
51
+ for vizedge in edges:
52
+ ax.plot(*vizedge.T, color="gray")
53
+ ax.grid(False)
54
+ ax.set_axis_off()
55
+ plt.tight_layout()
56
+ return
57
+
58
+ def _frame_update(index):
59
+ ax.view_init(index * 0.2, index * 0.5)
60
+ return
61
+
62
+ ani = animation.FuncAnimation(
63
+ fig,
64
+ _frame_update,
65
+ init_func=init,
66
+ interval=50,
67
+ cache_frame_data=False,
68
+ frames=frames,
69
+ )
70
+
71
+ return ani
72
+
73
+ # Default Example
74
+ if graph_mode == "Default Example":
75
+ G = nx.dodecahedral_graph()
76
+ pos = nx.spectral_layout(G, dim=3)
77
+ ani = generate_animation(G, pos)
78
+
79
+ # Create Your Own Example
80
+ else:
81
+ st.write("### Customize Your Graph")
82
+ num_nodes = st.slider("Number of Nodes", min_value=5, max_value=50, value=20)
83
+ edge_prob = st.slider("Edge Probability", min_value=0.1, max_value=1.0, value=0.3)
84
+
85
+ # Generate custom graph
86
+ G = nx.erdos_renyi_graph(num_nodes, edge_prob)
87
+ pos = nx.spectral_layout(G, dim=3)
88
+ ani = generate_animation(G, pos)
89
+
90
+ # Display animation in Streamlit
91
+ with st.spinner("Rendering animation..."):
92
+ ani.save("animation.gif", writer="imagemagick")
93
+ st.image("animation.gif", caption="3D Graph Rotation", use_column_width=True)
94
+
95
  # Function to display Weighted Graph
96
  def display_weighted_graph():
97
  st.title("Drawing: Weighted Graph")