Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,175 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import networkx as nx
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Sidebar for selecting an option
|
| 7 |
sidebar_option = st.sidebar.radio("Select an option",
|
| 8 |
["Select an option", "Basic: Properties",
|
| 9 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
| 10 |
-
"Basic: Simple graph Directed", "Drawing: Custom Node Position"
|
|
|
|
| 11 |
|
| 12 |
# Helper function to draw and display graph
|
| 13 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
| 14 |
-
plt.figure(figsize=(
|
| 15 |
-
nx.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
st.pyplot(plt)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def display_graph_properties(G):
|
| 20 |
pathlengths = []
|
| 21 |
st.write("### Source vertex {target:length, }")
|
|
@@ -30,7 +184,11 @@ def display_graph_properties(G):
|
|
| 30 |
|
| 31 |
dist = {}
|
| 32 |
for p in pathlengths:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
st.write("### Length #paths")
|
| 35 |
for d in sorted(dist.keys()):
|
| 36 |
st.write(f"Length {d}: {dist[d]} paths")
|
|
@@ -43,188 +201,59 @@ def display_graph_properties(G):
|
|
| 43 |
st.write(f"Periphery: {nx.periphery(G)}")
|
| 44 |
st.write(f"Density: {nx.density(G)}")
|
| 45 |
|
| 46 |
-
# Visualize the graph
|
| 47 |
st.write("### Graph Visualization")
|
| 48 |
-
pos = nx.spring_layout(G, seed=3068)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def display_read_write_graph(G):
|
| 53 |
-
st.write("### Adjacency List:")
|
| 54 |
-
for line in nx.generate_adjlist(G):
|
| 55 |
-
st.write(line)
|
| 56 |
-
|
| 57 |
-
# Write the graph's edge list to a file
|
| 58 |
-
st.write("### Writing Edge List to 'grid.edgelist' file:")
|
| 59 |
-
nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
|
| 60 |
-
st.write("Edge list written to 'grid.edgelist'")
|
| 61 |
-
|
| 62 |
-
# Read the graph from the edge list
|
| 63 |
-
st.write("### Reading Edge List from 'grid.edgelist' file:")
|
| 64 |
-
H = nx.read_edgelist(path="grid.edgelist", delimiter=":")
|
| 65 |
-
st.write("Edge list read into graph H")
|
| 66 |
-
|
| 67 |
-
# Visualize the graph
|
| 68 |
-
st.write("### Graph Visualization:")
|
| 69 |
-
pos = nx.spring_layout(H, seed=200) # Seed for reproducibility
|
| 70 |
-
draw_graph(H, pos)
|
| 71 |
-
|
| 72 |
-
# Function to display Simple Graphs for Basic: Simple graph
|
| 73 |
-
def display_simple_graph(G, pos=None):
|
| 74 |
-
options = {
|
| 75 |
-
"font_size": 36,
|
| 76 |
-
"node_size": 3000,
|
| 77 |
-
"node_color": "white",
|
| 78 |
-
"edgecolors": "black",
|
| 79 |
-
"linewidths": 5,
|
| 80 |
-
"width": 5,
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
# Draw the network
|
| 84 |
-
nx.draw_networkx(G, pos, **options)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
st.pyplot(plt)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
"arrowsize": 20,
|
| 98 |
-
"width": 2,
|
| 99 |
-
"edge_color": "gray",
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
# Draw the directed graph with the given positions and options
|
| 103 |
-
nx.draw_networkx(G, pos, **options)
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
st.pyplot(plt)
|
| 110 |
|
| 111 |
-
# Function to display Custom Node Position Graphs for Drawing: Custom Node Position
|
| 112 |
def display_custom_node_position():
|
| 113 |
st.title("Drawing: Custom Node Position")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
G =
|
| 117 |
-
|
| 118 |
-
edge_nodes = set(G) - {center_node}
|
| 119 |
-
|
| 120 |
-
# Ensure the nodes around the circle are evenly distributed
|
| 121 |
-
pos = nx.circular_layout(G.subgraph(edge_nodes))
|
| 122 |
-
pos[center_node] = np.array([0, 0]) # Manually specify node position
|
| 123 |
-
|
| 124 |
-
# Draw the graph
|
| 125 |
-
draw_graph(G, pos)
|
| 126 |
-
|
| 127 |
-
# Display Basic: Properties if selected
|
| 128 |
-
if sidebar_option == "Basic: Properties":
|
| 129 |
-
st.title("Basic: Properties")
|
| 130 |
-
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 131 |
-
|
| 132 |
-
if option == "Default Example":
|
| 133 |
-
G = nx.lollipop_graph(4, 6)
|
| 134 |
-
display_graph_properties(G)
|
| 135 |
|
| 136 |
-
elif option == "Create your own":
|
| 137 |
-
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
|
| 138 |
-
num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
display_graph_properties(G_custom)
|
| 144 |
-
|
| 145 |
-
# Display Basic: Read and write graphs if selected
|
| 146 |
elif sidebar_option == "Basic: Read and write graphs":
|
| 147 |
-
|
| 148 |
-
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 149 |
-
|
| 150 |
-
if option == "Default Example":
|
| 151 |
-
G = nx.grid_2d_graph(5, 5)
|
| 152 |
-
display_read_write_graph(G)
|
| 153 |
-
|
| 154 |
-
elif option == "Create your own":
|
| 155 |
-
rows = st.number_input("Number of rows:", min_value=2, max_value=20, value=5)
|
| 156 |
-
cols = st.number_input("Number of columns:", min_value=2, max_value=20, value=5)
|
| 157 |
-
|
| 158 |
-
if st.button("Generate"):
|
| 159 |
-
if rows >= 2 and cols >= 2:
|
| 160 |
-
G_custom = nx.grid_2d_graph(rows, cols)
|
| 161 |
-
display_read_write_graph(G_custom)
|
| 162 |
-
|
| 163 |
-
# Display Basic: Simple Graph if selected
|
| 164 |
elif sidebar_option == "Basic: Simple graph":
|
| 165 |
-
|
| 166 |
-
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 167 |
-
|
| 168 |
-
if option == "Default Example":
|
| 169 |
-
G = nx.Graph()
|
| 170 |
-
G.add_edge(1, 2)
|
| 171 |
-
G.add_edge(1, 3)
|
| 172 |
-
G.add_edge(1, 5)
|
| 173 |
-
G.add_edge(2, 3)
|
| 174 |
-
G.add_edge(3, 4)
|
| 175 |
-
G.add_edge(4, 5)
|
| 176 |
-
|
| 177 |
-
pos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}
|
| 178 |
-
display_simple_graph(G, pos)
|
| 179 |
-
|
| 180 |
-
elif option == "Create your own":
|
| 181 |
-
edges = []
|
| 182 |
-
edge_input = st.text_area("Edges:", value="1,2\n1,3\n2,3")
|
| 183 |
-
if edge_input:
|
| 184 |
-
edge_list = edge_input.split("\n")
|
| 185 |
-
for edge in edge_list:
|
| 186 |
-
u, v = map(int, edge.split(","))
|
| 187 |
-
edges.append((u, v))
|
| 188 |
-
|
| 189 |
-
if st.button("Generate"):
|
| 190 |
-
G_custom = nx.Graph()
|
| 191 |
-
G_custom.add_edges_from(edges)
|
| 192 |
-
pos = nx.spring_layout(G_custom, seed=42)
|
| 193 |
-
display_simple_graph(G_custom, pos)
|
| 194 |
-
|
| 195 |
-
# Display Basic: Simple Directed Graph if selected
|
| 196 |
elif sidebar_option == "Basic: Simple graph Directed":
|
| 197 |
-
|
| 198 |
-
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 199 |
-
|
| 200 |
-
if option == "Default Example":
|
| 201 |
-
G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
|
| 202 |
-
|
| 203 |
-
left_nodes = [0, 1, 2]
|
| 204 |
-
middle_nodes = [3, 4]
|
| 205 |
-
right_nodes = [5, 6]
|
| 206 |
-
|
| 207 |
-
pos = {n: (0, i) for i, n in enumerate(left_nodes)}
|
| 208 |
-
pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
|
| 209 |
-
pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
|
| 210 |
-
|
| 211 |
-
display_simple_directed_graph(G, pos)
|
| 212 |
-
|
| 213 |
-
elif option == "Create your own":
|
| 214 |
-
edges = []
|
| 215 |
-
edge_input = st.text_area("Edges:", value="1,2\n1,3\n2,3")
|
| 216 |
-
if edge_input:
|
| 217 |
-
edge_list = edge_input.split("\n")
|
| 218 |
-
for edge in edge_list:
|
| 219 |
-
u, v = map(int, edge.split(","))
|
| 220 |
-
edges.append((u, v))
|
| 221 |
-
|
| 222 |
-
if st.button("Generate"):
|
| 223 |
-
G_custom = nx.DiGraph()
|
| 224 |
-
G_custom.add_edges_from(edges)
|
| 225 |
-
pos = nx.spring_layout(G_custom, seed=42)
|
| 226 |
-
display_simple_directed_graph(G_custom, pos)
|
| 227 |
-
|
| 228 |
-
# Display Drawing: Custom Node Position if selected
|
| 229 |
elif sidebar_option == "Drawing: Custom Node Position":
|
| 230 |
display_custom_node_position()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import networkx as nx
|
| 4 |
+
import bz2
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
# Sidebar for selecting an option
|
| 8 |
sidebar_option = st.sidebar.radio("Select an option",
|
| 9 |
["Select an option", "Basic: Properties",
|
| 10 |
"Basic: Read and write graphs", "Basic: Simple graph",
|
| 11 |
+
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
| 12 |
+
"Drawing: Chess Masters"])
|
| 13 |
|
| 14 |
# Helper function to draw and display graph
|
| 15 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
| 16 |
+
plt.figure(figsize=(12, 12))
|
| 17 |
+
nx.draw_networkx_edges(G, pos, alpha=0.3, width=edgewidth, edge_color="m")
|
| 18 |
+
nx.draw_networkx_nodes(G, pos, node_size=nodesize, node_color="#210070", alpha=0.9)
|
| 19 |
+
label_options = {"ec": "k", "fc": "white", "alpha": 0.7}
|
| 20 |
+
nx.draw_networkx_labels(G, pos, font_size=14, bbox=label_options)
|
| 21 |
+
|
| 22 |
+
# Title/legend
|
| 23 |
+
font = {"fontname": "Helvetica", "color": "k", "fontweight": "bold", "fontsize": 14}
|
| 24 |
+
ax = plt.gca()
|
| 25 |
+
ax.set_title(title, font)
|
| 26 |
+
ax.text(
|
| 27 |
+
0.80,
|
| 28 |
+
0.10,
|
| 29 |
+
"edge width = # games played",
|
| 30 |
+
horizontalalignment="center",
|
| 31 |
+
transform=ax.transAxes,
|
| 32 |
+
fontdict=font,
|
| 33 |
+
)
|
| 34 |
+
ax.text(
|
| 35 |
+
0.80,
|
| 36 |
+
0.06,
|
| 37 |
+
"node size = # games won",
|
| 38 |
+
horizontalalignment="center",
|
| 39 |
+
transform=ax.transAxes,
|
| 40 |
+
fontdict=font,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Resize figure for label readability
|
| 44 |
+
ax.margins(0.1, 0.05)
|
| 45 |
+
plt.axis("off")
|
| 46 |
st.pyplot(plt)
|
| 47 |
|
| 48 |
+
|
| 49 |
+
def chess_pgn_graph(pgn_file="chess_masters_WCC.pgn.bz2"):
|
| 50 |
+
"""Read chess games in pgn format in pgn_file.
|
| 51 |
+
|
| 52 |
+
Filenames ending in .bz2 will be uncompressed.
|
| 53 |
+
|
| 54 |
+
Return the MultiDiGraph of players connected by a chess game.
|
| 55 |
+
Edges contain game data in a dict.
|
| 56 |
+
"""
|
| 57 |
+
G = nx.MultiDiGraph()
|
| 58 |
+
game = {}
|
| 59 |
+
with bz2.BZ2File(pgn_file) as datafile:
|
| 60 |
+
lines = [line.decode().rstrip("\r\n") for line in datafile]
|
| 61 |
+
for line in lines:
|
| 62 |
+
if line.startswith("["):
|
| 63 |
+
tag, value = line[1:-1].split(" ", 1)
|
| 64 |
+
game[str(tag)] = value.strip('"')
|
| 65 |
+
else:
|
| 66 |
+
if game:
|
| 67 |
+
white = game.pop("White")
|
| 68 |
+
black = game.pop("Black")
|
| 69 |
+
G.add_edge(white, black, **game)
|
| 70 |
+
game = {}
|
| 71 |
+
return G
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Draw Chess Masters Graph (the main section)
|
| 75 |
+
def display_chess_masters_graph():
|
| 76 |
+
st.title("Drawing: Chess Masters")
|
| 77 |
+
|
| 78 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 79 |
+
|
| 80 |
+
if option == "Default Example":
|
| 81 |
+
G = chess_pgn_graph("chess_masters_WCC.pgn.bz2")
|
| 82 |
+
|
| 83 |
+
# identify connected components of the undirected version
|
| 84 |
+
H = G.to_undirected()
|
| 85 |
+
Gcc = [H.subgraph(c) for c in nx.connected_components(H)]
|
| 86 |
+
if len(Gcc) > 1:
|
| 87 |
+
st.write(f"Note the disconnected component consisting of:\n{Gcc[1].nodes()}")
|
| 88 |
+
|
| 89 |
+
# find all games with B97 opening (as described in ECO)
|
| 90 |
+
openings = {game_info["ECO"] for (white, black, game_info) in G.edges(data=True)}
|
| 91 |
+
st.write(f"\nFrom a total of {len(openings)} different openings,")
|
| 92 |
+
st.write("the following games used the Sicilian opening")
|
| 93 |
+
st.write('with the Najdorff 7...Qb6 "Poisoned Pawn" variation.\n')
|
| 94 |
+
|
| 95 |
+
for white, black, game_info in G.edges(data=True):
|
| 96 |
+
if game_info["ECO"] == "B97":
|
| 97 |
+
summary = f"{white} vs {black}\n"
|
| 98 |
+
for k, v in game_info.items():
|
| 99 |
+
summary += f" {k}: {v}\n"
|
| 100 |
+
summary += "\n"
|
| 101 |
+
st.write(summary)
|
| 102 |
+
|
| 103 |
+
# Create undirected graph H without multi-edges
|
| 104 |
+
H = nx.Graph(G)
|
| 105 |
+
|
| 106 |
+
# Edge width is proportional to number of games played
|
| 107 |
+
edgewidth = [len(G.get_edge_data(u, v)) for u, v in H.edges()]
|
| 108 |
+
|
| 109 |
+
# Node size is proportional to number of games won
|
| 110 |
+
wins = dict.fromkeys(G.nodes(), 0.0)
|
| 111 |
+
for u, v, d in G.edges(data=True):
|
| 112 |
+
r = d["Result"].split("-")
|
| 113 |
+
if r[0] == "1":
|
| 114 |
+
wins[u] += 1.0
|
| 115 |
+
elif r[0] == "1/2":
|
| 116 |
+
wins[u] += 0.5
|
| 117 |
+
wins[v] += 0.5
|
| 118 |
+
else:
|
| 119 |
+
wins[v] += 1.0
|
| 120 |
+
nodesize = [wins[v] * 50 for v in H]
|
| 121 |
+
|
| 122 |
+
# Generate layout for visualization
|
| 123 |
+
pos = nx.kamada_kawai_layout(H)
|
| 124 |
+
|
| 125 |
+
# Manually tweak some positions to avoid label overlap
|
| 126 |
+
pos["Reshevsky, Samuel H"] += (0.05, -0.10)
|
| 127 |
+
pos["Botvinnik, Mikhail M"] += (0.03, -0.06)
|
| 128 |
+
pos["Smyslov, Vassily V"] += (0.05, -0.03)
|
| 129 |
+
|
| 130 |
+
# Draw the graph
|
| 131 |
+
draw_graph(H, pos, title="World Chess Championship Games: 1886 - 1985")
|
| 132 |
+
|
| 133 |
+
elif option == "Create your own":
|
| 134 |
+
uploaded_file = st.file_uploader("Upload your own PGN file", type="pgn")
|
| 135 |
+
if uploaded_file is not None:
|
| 136 |
+
G_custom = chess_pgn_graph(uploaded_file)
|
| 137 |
+
# Identify connected components and draw the graph for the uploaded data
|
| 138 |
+
H_custom = G_custom.to_undirected()
|
| 139 |
+
edgewidth = [len(G_custom.get_edge_data(u, v)) for u, v in H_custom.edges()]
|
| 140 |
+
wins = dict.fromkeys(G_custom.nodes(), 0.0)
|
| 141 |
+
for u, v, d in G_custom.edges(data=True):
|
| 142 |
+
r = d["Result"].split("-")
|
| 143 |
+
if r[0] == "1":
|
| 144 |
+
wins[u] += 1.0
|
| 145 |
+
elif r[0] == "1/2":
|
| 146 |
+
wins[u] += 0.5
|
| 147 |
+
wins[v] += 0.5
|
| 148 |
+
else:
|
| 149 |
+
wins[v] += 1.0
|
| 150 |
+
nodesize = [wins[v] * 50 for v in H_custom]
|
| 151 |
+
pos_custom = nx.kamada_kawai_layout(H_custom)
|
| 152 |
+
draw_graph(H_custom, pos_custom, title="Custom Chess Game Graph")
|
| 153 |
+
|
| 154 |
+
# Display other sections
|
| 155 |
+
def display_basic_properties():
|
| 156 |
+
st.title("Basic: Properties")
|
| 157 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
| 158 |
+
|
| 159 |
+
# Default example: 5x5 grid graph
|
| 160 |
+
if option == "Default Example":
|
| 161 |
+
G = nx.lollipop_graph(4, 6)
|
| 162 |
+
display_graph_properties(G)
|
| 163 |
+
|
| 164 |
+
elif option == "Create your own":
|
| 165 |
+
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
|
| 166 |
+
num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
|
| 167 |
+
|
| 168 |
+
if st.button("Generate"):
|
| 169 |
+
if num_nodes >= 2 and num_edges >= 1:
|
| 170 |
+
G_custom = nx.lollipop_graph(num_nodes, num_edges)
|
| 171 |
+
display_graph_properties(G_custom)
|
| 172 |
+
|
| 173 |
def display_graph_properties(G):
|
| 174 |
pathlengths = []
|
| 175 |
st.write("### Source vertex {target:length, }")
|
|
|
|
| 184 |
|
| 185 |
dist = {}
|
| 186 |
for p in pathlengths:
|
| 187 |
+
if p in dist:
|
| 188 |
+
dist[p] += 1
|
| 189 |
+
else:
|
| 190 |
+
dist[p] = 1
|
| 191 |
+
|
| 192 |
st.write("### Length #paths")
|
| 193 |
for d in sorted(dist.keys()):
|
| 194 |
st.write(f"Length {d}: {dist[d]} paths")
|
|
|
|
| 201 |
st.write(f"Periphery: {nx.periphery(G)}")
|
| 202 |
st.write(f"Density: {nx.density(G)}")
|
| 203 |
|
|
|
|
| 204 |
st.write("### Graph Visualization")
|
| 205 |
+
pos = nx.spring_layout(G, seed=3068)
|
| 206 |
+
plt.figure(figsize=(8, 6))
|
| 207 |
+
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
| 208 |
+
st.pyplot(plt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
# Display other sections
|
| 211 |
+
def display_read_write_graph():
|
| 212 |
+
st.title("Basic: Read and write graphs")
|
| 213 |
+
G = nx.karate_club_graph()
|
| 214 |
+
|
| 215 |
+
# Write the graph
|
| 216 |
+
nx.write_gml(G, "karate_club.gml")
|
| 217 |
+
st.write("Graph written to 'karate_club.gml'.")
|
| 218 |
+
|
| 219 |
+
# Read the graph back
|
| 220 |
+
G_new = nx.read_gml("karate_club.gml")
|
| 221 |
+
st.write("Graph read back from 'karate_club.gml'.")
|
| 222 |
+
nx.draw(G_new, with_labels=True)
|
| 223 |
st.pyplot(plt)
|
| 224 |
|
| 225 |
+
def display_simple_graph():
|
| 226 |
+
st.title("Basic: Simple graph")
|
| 227 |
+
G = nx.complete_graph(5)
|
| 228 |
+
nx.draw(G, with_labels=True)
|
| 229 |
+
st.pyplot(plt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
def display_simple_directed_graph():
|
| 232 |
+
st.title("Basic: Simple graph Directed")
|
| 233 |
+
G = nx.complete_graph(5, nx.DiGraph())
|
| 234 |
+
nx.draw(G, with_labels=True)
|
| 235 |
st.pyplot(plt)
|
| 236 |
|
|
|
|
| 237 |
def display_custom_node_position():
|
| 238 |
st.title("Drawing: Custom Node Position")
|
| 239 |
+
pos = {"A": (1, 2), "B": (2, 3), "C": (3, 1)}
|
| 240 |
+
G = nx.Graph(pos)
|
| 241 |
+
nx.draw(G, pos=pos, with_labels=True)
|
| 242 |
+
st.pyplot(plt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
+
# Call the appropriate function based on sidebar selection
|
| 246 |
+
if sidebar_option == "Basic: Properties":
|
| 247 |
+
display_basic_properties()
|
|
|
|
|
|
|
|
|
|
| 248 |
elif sidebar_option == "Basic: Read and write graphs":
|
| 249 |
+
display_read_write_graph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
elif sidebar_option == "Basic: Simple graph":
|
| 251 |
+
display_simple_graph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
elif sidebar_option == "Basic: Simple graph Directed":
|
| 253 |
+
display_simple_directed_graph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
elif sidebar_option == "Drawing: Custom Node Position":
|
| 255 |
display_custom_node_position()
|
| 256 |
+
elif sidebar_option == "Drawing: Chess Masters":
|
| 257 |
+
display_chess_masters_graph()
|
| 258 |
+
else:
|
| 259 |
+
st.write("Please select a valid option from the sidebar.")
|