grail-heart / scripts /build_explorer.py
Tumo505's picture
Upload folder using huggingface_hub
9eba44b verified
#!/usr/bin/env python3
"""
Build GRAIL-Heart Network Explorer
Embeds JSON data directly into HTML for GitHub Pages deployment
"""
import json
import os
from pathlib import Path
def build_explorer():
"""Build the explorer with embedded data for static hosting."""
base_dir = Path(__file__).parent.parent
cytoscape_dir = base_dir / "outputs" / "cytoscape"
data_dir = cytoscape_dir / "data"
# Load all network data
all_networks_path = data_dir / "all_networks.json"
if not all_networks_path.exists():
print("❌ Network data not found. Run generate_network_data.py first.")
return False
with open(all_networks_path, 'r') as f:
network_data = json.load(f)
print(f"Loaded network data: {len(network_data)} networks")
# Read the HTML template
html_path = cytoscape_dir / "grail_heart_explorer.html"
with open(html_path, 'r', encoding='utf-8') as f:
html_content = f.read()
# Create embedded data version
# Replace the dynamic loading with embedded data
embedded_js = f""" <script>
// Network data - embedded for static hosting (GitHub Pages)
// Generated by scripts/build_explorer.py
const networkData = {json.dumps(network_data, separators=(',', ':'))};
// No need to fetch - data is embedded
async function loadNetworkData() {{
console.log('Using embedded network data');
return true;
}}
// Region descriptions
const regionInfo = {{"""
# Find and replace the data loading section
import re
# Pattern to match from <script> to const regionInfo = {
pattern = r' <script>\s*// Network data.*?// Region descriptions\s*const regionInfo = \{'
replacement = embedded_js
embedded_html = re.sub(pattern, replacement, html_content, flags=re.DOTALL)
# Write the built version
output_path = cytoscape_dir / "grail_heart_explorer_built.html"
with open(output_path, 'w', encoding='utf-8') as f:
f.write(embedded_html)
print(f"Built explorer saved to: {output_path}")
print(f" File size: {output_path.stat().st_size / 1024:.1f} KB")
# Also create a version that replaces the original for GitHub Pages
# (keeping original for local development with fetch)
gh_pages_path = cytoscape_dir / "index.html"
with open(gh_pages_path, 'w', encoding='utf-8') as f:
f.write(embedded_html)
print(f"GitHub Pages version saved to: {gh_pages_path}")
return True
if __name__ == "__main__":
build_explorer()