Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,6 @@ import streamlit as st
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
import networkx as nx
|
4 |
|
5 |
-
# Initialize session state variable if it does not exist
|
6 |
-
if 'graph_displayed' not in st.session_state:
|
7 |
-
st.session_state.graph_displayed = False
|
8 |
-
|
9 |
# Sidebar for selecting an option
|
10 |
sidebar_option = st.sidebar.radio("Select an option",
|
11 |
["Select an option", "Basic: Properties",
|
@@ -81,7 +77,7 @@ def display_read_write_graph(G):
|
|
81 |
st.pyplot(plt)
|
82 |
|
83 |
# Function to display Simple Graphs for Basic: Simple graph
|
84 |
-
def display_simple_graph(G, pos=None
|
85 |
options = {
|
86 |
"font_size": 36,
|
87 |
"node_size": 3000,
|
@@ -92,10 +88,7 @@ def display_simple_graph(G, pos=None, directed=False):
|
|
92 |
}
|
93 |
|
94 |
# Draw the network
|
95 |
-
|
96 |
-
nx.draw_networkx(G, pos, **options, arrows=True) # For directed graph with arrows
|
97 |
-
else:
|
98 |
-
nx.draw_networkx(G, pos, **options)
|
99 |
|
100 |
# Set margins for the axes so that nodes aren't clipped
|
101 |
ax = plt.gca()
|
@@ -164,24 +157,6 @@ elif sidebar_option == "Basic: Simple graph":
|
|
164 |
pos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}
|
165 |
display_simple_graph(G, pos)
|
166 |
|
167 |
-
# Default example: directed graph with custom positions (added the missing part)
|
168 |
-
if option == "Default Example" and not st.session_state.graph_displayed:
|
169 |
-
G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
|
170 |
-
|
171 |
-
# Group nodes by column
|
172 |
-
left_nodes = [0, 1, 2]
|
173 |
-
middle_nodes = [3, 4]
|
174 |
-
right_nodes = [5, 6]
|
175 |
-
|
176 |
-
# Set the position according to column (x-coordinates)
|
177 |
-
pos = {n: (0, i) for i, n in enumerate(left_nodes)}
|
178 |
-
pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
|
179 |
-
pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
|
180 |
-
|
181 |
-
# Visualize the directed graph
|
182 |
-
display_simple_graph(G, pos, directed=True)
|
183 |
-
st.session_state.graph_displayed = True
|
184 |
-
|
185 |
# Create your own graph
|
186 |
elif option == "Create your own":
|
187 |
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=5)
|
@@ -205,4 +180,4 @@ elif sidebar_option == "Basic: Simple graph":
|
|
205 |
|
206 |
# Set a basic layout (spring layout as default)
|
207 |
pos = nx.spring_layout(G_custom, seed=42)
|
208 |
-
display_simple_graph(G_custom, pos)
|
|
|
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",
|
|
|
77 |
st.pyplot(plt)
|
78 |
|
79 |
# Function to display Simple Graphs for Basic: Simple graph
|
80 |
+
def display_simple_graph(G, pos=None):
|
81 |
options = {
|
82 |
"font_size": 36,
|
83 |
"node_size": 3000,
|
|
|
88 |
}
|
89 |
|
90 |
# Draw the network
|
91 |
+
nx.draw_networkx(G, pos, **options)
|
|
|
|
|
|
|
92 |
|
93 |
# Set margins for the axes so that nodes aren't clipped
|
94 |
ax = plt.gca()
|
|
|
157 |
pos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}
|
158 |
display_simple_graph(G, pos)
|
159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
# Create your own graph
|
161 |
elif option == "Create your own":
|
162 |
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=20, value=5)
|
|
|
180 |
|
181 |
# Set a basic layout (spring layout as default)
|
182 |
pos = nx.spring_layout(G_custom, seed=42)
|
183 |
+
display_simple_graph(G_custom, pos)
|