Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,18 +16,31 @@ if uploaded_file is not None:
|
|
16 |
# Load JSON data
|
17 |
graph_data = json.load(uploaded_file)
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Function to create a NetworkX graph from data
|
20 |
def create_graph(data):
|
21 |
G = nx.DiGraph()
|
22 |
-
for node in data
|
23 |
G.add_node(node["id"], label=node.get("label", node["id"]))
|
24 |
-
for edge in data
|
25 |
G.add_edge(edge["source"], edge["target"], label=edge.get("label", ""))
|
26 |
return G
|
27 |
|
28 |
# Generate the graph
|
29 |
graph = create_graph(graph_data)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Function to create a pyvis network from NetworkX graph
|
32 |
def create_pyvis_graph(G):
|
33 |
net = Network(height="600px", width="100%", directed=True)
|
@@ -45,7 +58,8 @@ if uploaded_file is not None:
|
|
45 |
pyvis_graph.show(output_file)
|
46 |
|
47 |
# Display the graph in Streamlit
|
48 |
-
|
|
|
49 |
|
50 |
# Clean up the temporary file
|
51 |
if os.path.exists(output_file):
|
|
|
16 |
# Load JSON data
|
17 |
graph_data = json.load(uploaded_file)
|
18 |
|
19 |
+
# Validate JSON structure
|
20 |
+
if "nodes" not in graph_data or "edges" not in graph_data:
|
21 |
+
raise ValueError("The JSON file must contain 'nodes' and 'edges' keys.")
|
22 |
+
|
23 |
+
if not isinstance(graph_data["nodes"], list) or not isinstance(graph_data["edges"], list):
|
24 |
+
raise ValueError("'nodes' and 'edges' must be lists.")
|
25 |
+
|
26 |
# Function to create a NetworkX graph from data
|
27 |
def create_graph(data):
|
28 |
G = nx.DiGraph()
|
29 |
+
for node in data["nodes"]:
|
30 |
G.add_node(node["id"], label=node.get("label", node["id"]))
|
31 |
+
for edge in data["edges"]:
|
32 |
G.add_edge(edge["source"], edge["target"], label=edge.get("label", ""))
|
33 |
return G
|
34 |
|
35 |
# Generate the graph
|
36 |
graph = create_graph(graph_data)
|
37 |
|
38 |
+
# Check if the graph has any nodes or edges
|
39 |
+
if graph.number_of_nodes() == 0:
|
40 |
+
raise ValueError("The graph has no nodes.")
|
41 |
+
if graph.number_of_edges() == 0:
|
42 |
+
raise ValueError("The graph has no edges.")
|
43 |
+
|
44 |
# Function to create a pyvis network from NetworkX graph
|
45 |
def create_pyvis_graph(G):
|
46 |
net = Network(height="600px", width="100%", directed=True)
|
|
|
58 |
pyvis_graph.show(output_file)
|
59 |
|
60 |
# Display the graph in Streamlit
|
61 |
+
with open(output_file, "r") as file:
|
62 |
+
st.components.v1.html(file.read(), height=600, width=800)
|
63 |
|
64 |
# Clean up the temporary file
|
65 |
if os.path.exists(output_file):
|