shukdevdatta123 commited on
Commit
d51fc42
·
verified ·
1 Parent(s): ba7867f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -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,81 @@ 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 Drawing: Conversion"])
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_drawing_conversion():
33
+ st.title("Graphviz Drawing: Conversion")
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 conversions, or you can create your own custom graph."
40
+ )
41
+
42
+ if graph_mode == "Default Example":
43
+ # Create a complete graph (K5)
44
+ G = nx.complete_graph(5)
45
+
46
+ # Convert NetworkX graph to Graphviz graph
47
+ A = nx.nx_agraph.to_agraph(G) # convert to a graphviz graph
48
+
49
+ # Convert back to NetworkX graph using different methods
50
+ X1 = nx.nx_agraph.from_agraph(A) # Convert back to NetworkX as Graph
51
+ X2 = nx.Graph(A) # Another method to convert
52
+ G1 = nx.Graph(X1) # Another Graph conversion
53
+
54
+ # Write the graph to a DOT file
55
+ A.write("k5.dot")
56
+
57
+ # Read the graph from the DOT file
58
+ X3 = nx.nx_agraph.read_dot("k5.dot")
59
+
60
+ # Display the Graphviz file and the conversions
61
+ st.write("### Graphviz to NetworkX Conversion Examples:")
62
+ st.write("X1 (from AGraph to NetworkX):", list(X1.edges()))
63
+ st.write("X2 (from AGraph to NetworkX):", list(X2.edges()))
64
+ st.write("G1 (from NetworkX Graph to Graph):", list(G1.edges()))
65
+ st.write("X3 (from DOT file to NetworkX):", list(X3.edges()))
66
+
67
+ # Draw the graph and save as PNG
68
+ A.draw("k5.png", prog="neato") # Save the graph as a PNG file
69
+
70
+ # Display the graph image
71
+ st.image("k5.png", caption="Graphviz Drawing of K5")
72
+
73
+ elif graph_mode == "Create Your Own":
74
+ st.write("### Custom Graph Creation")
75
+
76
+ # Input for custom graph (create a random graph)
77
+ num_nodes = st.slider("Number of Nodes", min_value=2, max_value=50, value=10)
78
+ num_edges = st.slider("Number of Edges", min_value=1, max_value=num_nodes*(num_nodes-1)//2, value=10)
79
+
80
+ # Generate a custom graph
81
+ G_custom = nx.gnm_random_graph(num_nodes, num_edges)
82
+
83
+ # Convert to Graphviz and draw
84
+ A_custom = nx.nx_agraph.to_agraph(G_custom)
85
+
86
+ # Write custom graph to DOT file
87
+ A_custom.write("custom_graph.dot")
88
+
89
+ # Read the graph from the DOT file
90
+ X_custom = nx.nx_agraph.read_dot("custom_graph.dot")
91
+
92
+ # Display custom graph conversion details
93
+ st.write("### Custom Graph Conversion:")
94
+ st.write("Custom Graph Edges (from NetworkX to Graphviz):", list(G_custom.edges()))
95
+ st.write("Custom Graph Edges (from DOT to NetworkX):", list(X_custom.edges()))
96
+
97
+ # Draw the custom graph and save as PNG
98
+ A_custom.draw("custom_graph.png", prog="neato")
99
+
100
+ # Display the custom graph image
101
+ st.image("custom_graph.png", caption="Custom Graph Drawing")
102
+
103
+ # Display the corresponding page based on sidebar option
104
+ if sidebar_option == "Graphviz Drawing: Conversion":
105
+ graphviz_drawing_conversion()
106
+
107
  if sidebar_option == "3D Drawing: Animations of 3D Rotation":
108
  st.title("3D Drawing: Animations of 3D Rotation")
109