Skip to content

Commit 52410ca

Browse files
committed
Serve the graph data from a static file
Instead of fetching the data from the `/extract` endpoint (which expects a `graph.json` file to be present, and then adds extra data that it fetches from S3), use a new `/graph-viewmodel` endpoint, which simply serves the data from a static file.
1 parent f849057 commit 52410ca

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RUN uv sync --no-dev --no-install-project
1717
COPY src/ ./src/
1818
COPY static/ ./static/
1919
COPY templates/ ./templates/
20+
COPY graph-viewmodel.json ./graph-viewmodel.json
2021
COPY app.py ./app.py
2122

2223
# Set environment variables

app.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from flask import Flask, request, jsonify, render_template
44
from dotenv import load_dotenv
5-
from src.generate_graph import generate_graph
5+
from src.generate_graph import generate_graph, load_graph_viewmodel
66

77
load_dotenv()
88

@@ -24,6 +24,18 @@ def graph_page():
2424
"""Serve the Cytoscape graph viewer page."""
2525
return render_template('graph.html')
2626

27+
@app.route('/graph-viewmodel', methods=['GET'])
28+
async def graph_viewmodel():
29+
"""Serve the graph data as JSON for the frontend."""
30+
try:
31+
logger.info('Loading graph data for viewmodel endpoint...')
32+
graph_data = load_graph_viewmodel("graph-viewmodel.json")
33+
logger.info('Graph data loaded successfully.')
34+
return jsonify(graph_data), 200
35+
except Exception as e:
36+
app.logger.error(f"Error loading graph data: {str(e)}")
37+
return jsonify({"error": str(e)}), 500
38+
2739
@app.route('/healthcheck/ready', methods=['GET'])
2840
def health_check():
2941
"""Simple health check endpoint."""

graph-viewmodel.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/generate_graph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,17 @@ async def generate_graph(input_data: Union[str, Dict[str, Any]], output_path: Op
150150

151151
return cy_json
152152

153+
def load_json_file(file_path: str) -> Dict[str, Any]:
154+
"""Utility function to load JSON data from a file."""
155+
if not os.path.exists(file_path):
156+
logger.error(f"File {file_path} not found.")
157+
return {}
158+
with open(file_path, "r") as f:
159+
return json.load(f)
160+
161+
def load_graph_viewmodel(file_path: str) -> Dict[str, Any]:
162+
"""Loads the graph viewmodel JSON for the frontend."""
163+
return load_json_file(file_path)
164+
153165
if __name__ == "__main__":
154166
asyncio.run(generate_graph("graph.json", "outputs/graphNode.json"))

templates/graph.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h2 id="sidebar-title">No node selected</h2>
5858

5959
async function loadGraph() {
6060
try {
61-
const response = await fetch('/extract');
61+
const response = await fetch('/graph-viewmodel');
6262
if (!response.ok) {
6363
throw new Error(`Failed to load graph: ${response.status} ${response.statusText}`);
6464
}

0 commit comments

Comments
 (0)