Update app.py
Browse files
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,91 @@ 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: Lanl Routes"])
|
| 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 |
+
# Default example: Lanl Routes graph
|
| 33 |
+
def lanl_graph():
|
| 34 |
+
"""Return the LANL internet view graph from lanl.edges"""
|
| 35 |
+
try:
|
| 36 |
+
fh = open("lanl_routes.edgelist")
|
| 37 |
+
except OSError:
|
| 38 |
+
print("lanl.edges not found")
|
| 39 |
+
raise
|
| 40 |
+
|
| 41 |
+
G = nx.Graph()
|
| 42 |
+
|
| 43 |
+
time = {}
|
| 44 |
+
time[0] = 0 # assign 0 to center node
|
| 45 |
+
for line in fh.readlines():
|
| 46 |
+
(head, tail, rtt) = line.split()
|
| 47 |
+
G.add_edge(int(head), int(tail))
|
| 48 |
+
time[int(head)] = float(rtt)
|
| 49 |
+
|
| 50 |
+
# get largest component and assign ping times to G0time dictionary
|
| 51 |
+
Gcc = sorted(nx.connected_components(G), key=len, reverse=True)[0]
|
| 52 |
+
G0 = G.subgraph(Gcc)
|
| 53 |
+
G0.rtt = {}
|
| 54 |
+
for n in G0:
|
| 55 |
+
G0.rtt[n] = time[n]
|
| 56 |
+
|
| 57 |
+
return G0
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Streamlit Layout for Graphviz Layout: Lanl Routes
|
| 61 |
+
def graphviz_layout_lanl_routes():
|
| 62 |
+
st.title("Graphviz Layout: Lanl Routes")
|
| 63 |
+
|
| 64 |
+
# Sidebar selection for Default Example or Custom Graph
|
| 65 |
+
graph_mode = st.radio(
|
| 66 |
+
"Choose a Mode:",
|
| 67 |
+
("Default Example", "Create Your Own"),
|
| 68 |
+
help="Default example shows LANL routes, or you can create your own custom graph."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if graph_mode == "Default Example":
|
| 72 |
+
# Load LANL graph and visualize
|
| 73 |
+
G = lanl_graph()
|
| 74 |
+
|
| 75 |
+
st.write("Graph Summary:")
|
| 76 |
+
st.write(G)
|
| 77 |
+
st.write(f"{nx.number_connected_components(G)} connected components")
|
| 78 |
+
|
| 79 |
+
# Create and display the graph
|
| 80 |
+
plt.figure(figsize=(8, 8))
|
| 81 |
+
# Use graphviz to find radial layout
|
| 82 |
+
pos = nx.nx_agraph.graphviz_layout(G, prog="twopi", root=0)
|
| 83 |
+
|
| 84 |
+
# Draw nodes, coloring by rtt ping time
|
| 85 |
+
options = {"with_labels": False, "alpha": 0.5, "node_size": 15}
|
| 86 |
+
nx.draw(G, pos, node_color=[G.rtt[v] for v in G], **options)
|
| 87 |
+
|
| 88 |
+
# Adjust the plot limits
|
| 89 |
+
xmax = 1.02 * max(xx for xx, yy in pos.values())
|
| 90 |
+
ymax = 1.02 * max(yy for xx, yy in pos.values())
|
| 91 |
+
plt.xlim(0, xmax)
|
| 92 |
+
plt.ylim(0, ymax)
|
| 93 |
+
st.pyplot(plt)
|
| 94 |
+
|
| 95 |
+
elif graph_mode == "Create Your Own":
|
| 96 |
+
st.write("### Custom Graph Creation")
|
| 97 |
+
|
| 98 |
+
# Input form for creating custom graph
|
| 99 |
+
num_nodes = st.slider("Number of Nodes", min_value=2, max_value=50, value=10)
|
| 100 |
+
num_edges = st.slider("Number of Edges", min_value=1, max_value=num_nodes*(num_nodes-1)//2, value=10)
|
| 101 |
+
|
| 102 |
+
# Generate custom graph
|
| 103 |
+
G_custom = nx.gnm_random_graph(num_nodes, num_edges)
|
| 104 |
+
|
| 105 |
+
st.write("Generated Custom Graph:")
|
| 106 |
+
st.write(G_custom)
|
| 107 |
+
|
| 108 |
+
# Use graphviz layout
|
| 109 |
+
pos = nx.nx_agraph.graphviz_layout(G_custom, prog="twopi")
|
| 110 |
+
nx.draw(G_custom, pos, with_labels=True, node_size=500, node_color="lightblue", font_size=10)
|
| 111 |
+
st.pyplot(plt)
|
| 112 |
+
|
| 113 |
+
# Display the corresponding page based on sidebar option
|
| 114 |
+
if sidebar_option == "Graphviz Layout: Lanl Routes":
|
| 115 |
+
graphviz_layout_lanl_routes()
|
| 116 |
+
|
| 117 |
if sidebar_option == "3D Drawing: Animations of 3D Rotation":
|
| 118 |
st.title("3D Drawing: Animations of 3D Rotation")
|
| 119 |
|