shukdevdatta123 commited on
Commit
23bdcd8
·
verified ·
1 Parent(s): d8baf10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -1
app.py CHANGED
@@ -20,7 +20,8 @@ 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", "3D Drawing: Basic Matplotlib"])
 
24
 
25
  # Helper function to draw and display graph
26
  def draw_graph(G, pos=None, title="Graph Visualization"):
@@ -28,6 +29,76 @@ 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
 
 
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", "3D Drawing: Basic Matplotlib",
24
+ "Graphviz Layout: Attributes"])
25
 
26
  # Helper function to draw and display graph
27
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
29
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
30
  st.pyplot(plt)
31
 
32
+ def graphviz_layout_attributes():
33
+ st.title("Graphviz Layout: Attributes")
34
+
35
+ # Sidebar selection for Default Example or Custom Graph
36
+ graph_mode = st.radio(
37
+ "Choose a Mode:",
38
+ ("Default Example", "Create Your Own"),
39
+ help="Default example shows graph with attributes, or you can create your own custom graph."
40
+ )
41
+
42
+ if graph_mode == "Default Example":
43
+ # Create a graph and add edges and nodes with attributes
44
+ G = nx.Graph()
45
+ G.add_edge(1, 2, color="red")
46
+ G.add_edge(2, 3, color="red")
47
+ G.add_node(3)
48
+ G.add_node(4)
49
+
50
+ # Convert NetworkX graph to Graphviz graph
51
+ A = nx.nx_agraph.to_agraph(G) # convert to a graphviz graph
52
+ A.draw("attributes.png", prog="neato") # Draw with pygraphviz
53
+
54
+ # Display the graph image
55
+ st.image("attributes.png", caption="Graph with Attributes")
56
+
57
+ # Convert back to NetworkX graph and display attributes
58
+ X = nx.nx_agraph.from_agraph(A)
59
+
60
+ st.write("### Edge Data:")
61
+ st.write(list(X.edges(data=True))) # Display edges with data
62
+
63
+ st.write("### Default Graph Attributes:")
64
+ st.write(X.graph) # Display default graph attributes
65
+
66
+ st.write("### Node Data:")
67
+ st.write(X.nodes.data(True)) # Display node data with attributes
68
+
69
+ elif graph_mode == "Create Your Own":
70
+ st.write("### Custom Graph Creation")
71
+
72
+ # Input for custom graph (create a random graph)
73
+ num_nodes = st.slider("Number of Nodes", min_value=2, max_value=50, value=10)
74
+ num_edges = st.slider("Number of Edges", min_value=1, max_value=num_nodes*(num_nodes-1)//2, value=10)
75
+
76
+ # Generate a custom graph
77
+ G_custom = nx.gnm_random_graph(num_nodes, num_edges)
78
+
79
+ # Convert to Graphviz and draw
80
+ A_custom = nx.nx_agraph.to_agraph(G_custom)
81
+ A_custom.draw("custom_attributes.png", prog="neato")
82
+
83
+ # Display the custom graph image
84
+ st.image("custom_attributes.png", caption="Custom Graph with Attributes")
85
+
86
+ # Convert back to NetworkX graph and display custom attributes
87
+ X_custom = nx.nx_agraph.from_agraph(A_custom)
88
+
89
+ st.write("### Edge Data:")
90
+ st.write(list(X_custom.edges(data=True))) # Display edges with data
91
+
92
+ st.write("### Default Graph Attributes:")
93
+ st.write(X_custom.graph) # Display default graph attributes
94
+
95
+ st.write("### Node Data:")
96
+ st.write(X_custom.nodes.data(True)) # Display node data with attributes
97
+
98
+ # Display the corresponding page based on sidebar option
99
+ if sidebar_option == "Graphviz Layout: Attributes":
100
+ graphviz_layout_attributes()
101
+
102
  if sidebar_option == "3D Drawing: Animations of 3D Rotation":
103
  st.title("3D Drawing: Animations of 3D Rotation")
104