Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
import matplotlib.pyplot as plt
|
3 |
import networkx as nx
|
|
|
4 |
|
5 |
# Sidebar for selecting an option
|
6 |
sidebar_option = st.sidebar.radio("Select an option",
|
7 |
["Select an option", "Basic: Properties",
|
8 |
-
"Basic: Read and write graphs", "Basic: Simple graph",
|
|
|
9 |
|
10 |
# Function to display properties and graph for Basic: Properties
|
11 |
def display_graph_properties(G):
|
@@ -115,6 +117,58 @@ def display_simple_directed_graph(G, pos=None):
|
|
115 |
plt.axis("off")
|
116 |
st.pyplot(plt)
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
# Display Basic: Properties if selected
|
119 |
if sidebar_option == "Basic: Properties":
|
120 |
st.title("Basic: Properties")
|
@@ -247,3 +301,7 @@ elif sidebar_option == "Basic: Simple graph Directed":
|
|
247 |
# Set a basic layout (spring layout as default)
|
248 |
pos = nx.spring_layout(G_custom, seed=42)
|
249 |
display_simple_directed_graph(G_custom, pos)
|
|
|
|
|
|
|
|
|
|
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 |
# Function to display properties and graph for Basic: Properties
|
13 |
def display_graph_properties(G):
|
|
|
117 |
plt.axis("off")
|
118 |
st.pyplot(plt)
|
119 |
|
120 |
+
# Function to display Custom Node Position Graphs for Drawing: Custom Node Position
|
121 |
+
def display_custom_node_position():
|
122 |
+
st.title("Drawing: Custom Node Position")
|
123 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
124 |
+
|
125 |
+
if option == "Default Example":
|
126 |
+
# Default example graph (path graph with custom node position)
|
127 |
+
G = nx.path_graph(20)
|
128 |
+
center_node = 5
|
129 |
+
edge_nodes = set(G) - {center_node}
|
130 |
+
|
131 |
+
# Ensure the nodes around the circle are evenly distributed
|
132 |
+
pos = nx.circular_layout(G.subgraph(edge_nodes))
|
133 |
+
pos[center_node] = np.array([0, 0]) # Manually specify node position
|
134 |
+
|
135 |
+
# Draw the graph
|
136 |
+
nx.draw(G, pos, with_labels=True)
|
137 |
+
st.pyplot(plt)
|
138 |
+
|
139 |
+
elif option == "Create your own":
|
140 |
+
# Let the user define a graph with custom edges
|
141 |
+
st.write("Enter the edges (as pairs of nodes) separated by commas. For example, 1,2 or 3,4.")
|
142 |
+
edge_input = st.text_area("Edges:", value="0,1\n1,2\n2,3")
|
143 |
+
|
144 |
+
edges = []
|
145 |
+
if edge_input:
|
146 |
+
edge_list = edge_input.split("\n")
|
147 |
+
for edge in edge_list:
|
148 |
+
u, v = map(int, edge.split(","))
|
149 |
+
edges.append((u, v))
|
150 |
+
|
151 |
+
# Create the graph and allow user to input custom node positions
|
152 |
+
if st.button("Generate"):
|
153 |
+
G_custom = nx.Graph()
|
154 |
+
G_custom.add_edges_from(edges)
|
155 |
+
|
156 |
+
# Let user define node positions
|
157 |
+
st.write("Enter custom positions for nodes (format: node: x,y). For example, 0: 0,0 or 1: 1,2.")
|
158 |
+
pos_input = st.text_area("Node positions:", value="0: 0,0\n1: 1,1\n2: 2,0")
|
159 |
+
|
160 |
+
pos = {}
|
161 |
+
if pos_input:
|
162 |
+
pos_list = pos_input.split("\n")
|
163 |
+
for position in pos_list:
|
164 |
+
node, coordinates = position.split(":")
|
165 |
+
x, y = map(float, coordinates.split(","))
|
166 |
+
pos[int(node)] = np.array([x, y])
|
167 |
+
|
168 |
+
# Draw the graph with custom node positions
|
169 |
+
nx.draw(G_custom, pos, with_labels=True)
|
170 |
+
st.pyplot(plt)
|
171 |
+
|
172 |
# Display Basic: Properties if selected
|
173 |
if sidebar_option == "Basic: Properties":
|
174 |
st.title("Basic: Properties")
|
|
|
301 |
# Set a basic layout (spring layout as default)
|
302 |
pos = nx.spring_layout(G_custom, seed=42)
|
303 |
display_simple_directed_graph(G_custom, pos)
|
304 |
+
|
305 |
+
# Display Drawing: Custom Node Position if selected
|
306 |
+
elif sidebar_option == "Drawing: Custom Node Position":
|
307 |
+
display_custom_node_position()
|