import os import json import glob import logging import shutil import re as _re from flask import Flask, render_template, render_template_string, jsonify logger = logging.getLogger(__name__) app = Flask(__name__, template_folder="templates") def find_latest_checkpoint(base_folder): # Check whether the base folder is itself a checkpoint folder if os.path.basename(base_folder).startswith("checkpoint_"): return base_folder checkpoint_folders = glob.glob("**/checkpoint_*", root_dir=base_folder, recursive=True) if not checkpoint_folders: logger.info(f"No checkpoint folders found in {base_folder}") return None checkpoint_folders = [os.path.join(base_folder, folder) for folder in checkpoint_folders] checkpoint_folders.sort(key=lambda x: os.path.getmtime(x), reverse=True) logger.debug(f"Found checkpoint folder: {checkpoint_folders[0]}") return checkpoint_folders[0] def load_evolution_data(checkpoint_folder): meta_path = os.path.join(checkpoint_folder, "metadata.json") programs_dir = os.path.join(checkpoint_folder, "programs") if not os.path.exists(meta_path) or not os.path.exists(programs_dir): logger.info(f"Missing metadata.json or programs dir in {checkpoint_folder}") return {"archive": [], "nodes": [], "edges": [], "checkpoint_dir": checkpoint_folder} with open(meta_path) as f: meta = json.load(f) nodes = [] id_to_program = {} pids = set() for island_idx, id_list in enumerate(meta.get("islands", [])): for pid in id_list: prog_path = os.path.join(programs_dir, f"{pid}.json") # Keep track of PIDs and if one is double, append "-copyN" to the PID if pid in pids: base_pid = pid # If base_pid already has a "-copyN" suffix, strip it if "-copy" in base_pid: base_pid = base_pid.rsplit("-copy", 1)[0] # Find the next available copy number copy_num = 1 while f"{base_pid}-copy{copy_num}" in pids: copy_num += 1 pid = f"{base_pid}-copy{copy_num}" pids.add(pid) if os.path.exists(prog_path): with open(prog_path) as pf: prog = json.load(pf) prog["id"] = pid prog["island"] = island_idx nodes.append(prog) id_to_program[pid] = prog else: logger.debug(f"Program file not found: {prog_path}") edges = [] for prog in nodes: parent_id = prog.get("parent_id") if parent_id and parent_id in id_to_program: edges.append({"source": parent_id, "target": prog["id"]}) logger.info(f"Loaded {len(nodes)} nodes and {len(edges)} edges from {checkpoint_folder}") return { "archive": meta.get("archive", []), "nodes": nodes, "edges": edges, "checkpoint_dir": checkpoint_folder, } @app.route("/") def index(): return render_template("index.html", checkpoint_dir=checkpoint_dir) checkpoint_dir = None # Global variable to store the checkpoint directory @app.route("/api/data") def data(): global checkpoint_dir base_folder = os.environ.get("EVOLVE_OUTPUT", "examples/") checkpoint_dir = find_latest_checkpoint(base_folder) if not checkpoint_dir: logger.info(f"No checkpoints found in {base_folder}") return jsonify({"archive": [], "nodes": [], "edges": [], "checkpoint_dir": ""}) logger.info(f"Loading data from checkpoint: {checkpoint_dir}") data = load_evolution_data(checkpoint_dir) logger.debug(f"Data: {data}") return jsonify(data) @app.route("/program/") def program_page(program_id): global checkpoint_dir if checkpoint_dir is None: return "No checkpoint loaded", 500 data = load_evolution_data(checkpoint_dir) program_data = next((p for p in data["nodes"] if p["id"] == program_id), None) program_data = {"code": "", "prompts": {}, **program_data} artifacts_json = program_data.get("artifacts_json", None) return render_template( "program_page.html", program_data=program_data, checkpoint_dir=checkpoint_dir, artifacts_json=artifacts_json, ) def run_static_export(args): output_dir = args.static_output os.makedirs(output_dir, exist_ok=True) # Load data and prepare JSON string checkpoint_dir = find_latest_checkpoint(args.path) if not checkpoint_dir: raise RuntimeError(f"No checkpoint found in {args.path}") data = load_evolution_data(checkpoint_dir) logger.info(f"Exporting visualization for checkpoint: {checkpoint_dir}") with app.app_context(): data_json = jsonify(data).get_data(as_text=True) inlined = f"" # Load index.html template templates_dir = os.path.join(os.path.dirname(__file__), "templates") template_path = os.path.join(templates_dir, "index.html") with open(template_path, "r", encoding="utf-8") as f: html = f.read() # Insert static json data into the HTML html = _re.sub(r"\{\{\s*url_for\('static', filename='([^']+)'\)\s*\}\}", r"static/\1", html) script_tag_idx = html.find('