Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,21 +5,44 @@ import json
|
|
| 5 |
from streamlit.components.v1 import html
|
| 6 |
|
| 7 |
# Streamlit app layout
|
| 8 |
-
st.title("Interactive Graph Visualization")
|
| 9 |
-
st.write("
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 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 |
# Function to create a NetworkX graph from data
|
| 24 |
def create_graph(data):
|
| 25 |
G = nx.DiGraph()
|
|
@@ -28,16 +51,10 @@ if uploaded_file is not None:
|
|
| 28 |
for edge in data["edges"]:
|
| 29 |
G.add_edge(edge["source"], edge["target"], label=edge.get("label", ""))
|
| 30 |
return G
|
| 31 |
-
|
| 32 |
# Generate the graph
|
| 33 |
graph = create_graph(graph_data)
|
| 34 |
-
|
| 35 |
-
# Check if the graph has any nodes or edges
|
| 36 |
-
if graph.number_of_nodes() == 0:
|
| 37 |
-
raise ValueError("The graph has no nodes.")
|
| 38 |
-
if graph.number_of_edges() == 0:
|
| 39 |
-
raise ValueError("The graph has no edges.")
|
| 40 |
-
|
| 41 |
# Function to create a pyvis network from NetworkX graph
|
| 42 |
def create_pyvis_graph(G):
|
| 43 |
net = Network(height="600px", width="100%", directed=True)
|
|
@@ -49,7 +66,7 @@ if uploaded_file is not None:
|
|
| 49 |
|
| 50 |
# Create the Pyvis graph
|
| 51 |
pyvis_graph = create_pyvis_graph(graph)
|
| 52 |
-
|
| 53 |
# Generate the HTML representation of the graph
|
| 54 |
pyvis_graph_html = pyvis_graph.generate_html()
|
| 55 |
|
|
@@ -57,6 +74,4 @@ if uploaded_file is not None:
|
|
| 57 |
html(pyvis_graph_html, height=600)
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
-
st.error(f"Error processing the
|
| 61 |
-
else:
|
| 62 |
-
st.info("Please upload a JSON file to display the graph.")
|
|
|
|
| 5 |
from streamlit.components.v1 import html
|
| 6 |
|
| 7 |
# Streamlit app layout
|
| 8 |
+
st.title("Interactive Graph Visualization and Editor")
|
| 9 |
+
st.write("Edit the graph's JSON data and visualize updates dynamically.")
|
| 10 |
|
| 11 |
+
# Read the test.json file initially
|
| 12 |
+
default_file = "test.json"
|
| 13 |
|
| 14 |
+
# Function to load JSON from file
|
| 15 |
+
def load_json_file(file_path):
|
| 16 |
+
try:
|
| 17 |
+
with open(file_path, "r") as file:
|
| 18 |
+
return json.load(file)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
st.error(f"Error loading the file: {e}")
|
| 21 |
+
return {"nodes": [], "edges": []}
|
| 22 |
+
|
| 23 |
+
# Load the graph data from the default JSON file
|
| 24 |
+
graph_data = load_json_file(default_file)
|
| 25 |
+
|
| 26 |
+
# Sidebar editor for JSON input
|
| 27 |
+
st.sidebar.header("Edit Graph JSON")
|
| 28 |
+
json_editor = st.sidebar.text_area("Graph JSON", value=json.dumps(graph_data, indent=4), height=300)
|
| 29 |
+
|
| 30 |
+
if st.sidebar.button("Update Graph"):
|
| 31 |
+
try:
|
| 32 |
+
# Parse the updated JSON
|
| 33 |
+
graph_data = json.loads(json_editor)
|
| 34 |
+
# Save back to the file
|
| 35 |
+
with open(default_file, "w") as file:
|
| 36 |
+
json.dump(graph_data, file, indent=4)
|
| 37 |
+
st.success("Graph JSON updated successfully!")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.sidebar.error(f"Invalid JSON format: {e}")
|
| 40 |
+
|
| 41 |
+
# Validate JSON structure
|
| 42 |
+
if "nodes" not in graph_data or "edges" not in graph_data:
|
| 43 |
+
st.error("The JSON file must contain 'nodes' and 'edges' keys.")
|
| 44 |
+
else:
|
| 45 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Function to create a NetworkX graph from data
|
| 47 |
def create_graph(data):
|
| 48 |
G = nx.DiGraph()
|
|
|
|
| 51 |
for edge in data["edges"]:
|
| 52 |
G.add_edge(edge["source"], edge["target"], label=edge.get("label", ""))
|
| 53 |
return G
|
| 54 |
+
|
| 55 |
# Generate the graph
|
| 56 |
graph = create_graph(graph_data)
|
| 57 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Function to create a pyvis network from NetworkX graph
|
| 59 |
def create_pyvis_graph(G):
|
| 60 |
net = Network(height="600px", width="100%", directed=True)
|
|
|
|
| 66 |
|
| 67 |
# Create the Pyvis graph
|
| 68 |
pyvis_graph = create_pyvis_graph(graph)
|
| 69 |
+
|
| 70 |
# Generate the HTML representation of the graph
|
| 71 |
pyvis_graph_html = pyvis_graph.generate_html()
|
| 72 |
|
|
|
|
| 74 |
html(pyvis_graph_html, height=600)
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
+
st.error(f"Error processing the graph: {e}")
|
|
|
|
|
|