| import math |
| import os |
| import json |
| import glob |
| import shutil |
| import logging |
| import re as _re |
| from numbers import Number |
| from typing import Optional, Any |
|
|
| from flask import Flask, render_template, jsonify |
|
|
| from manual import create_manual_blueprint |
|
|
|
|
| logger = logging.getLogger(__name__) |
| app = Flask(__name__, template_folder="templates") |
|
|
|
|
| def find_latest_checkpoint(base_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") |
|
|
| |
| if pid in pids: |
| base_pid = pid |
|
|
| |
| if "-copy" in base_pid: |
| base_pid = base_pid.rsplit("-copy", 1)[0] |
|
|
| |
| 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) |
| sanitize_program_for_visualization(prog) |
| 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, |
| } |
|
|
| def sanitize_program_for_visualization(program: dict[str, Any]) -> None: |
| for k, v in program["metrics"].items(): |
| if not check_json_float(v): |
| program["metrics"][k] = None |
| if "parent_metrics" in program["metadata"]: |
| for k, v in program["metadata"]["parent_metrics"].items(): |
| if not check_json_float(v): |
| program["metadata"]["parent_metrics"][k] = None |
|
|
| def check_json_float(v: Optional[float]) -> bool: |
| return isinstance(v, Number) and not (math.isinf(v) or math.isnan(v)) |
|
|
|
|
| @app.route("/") |
| def index(): |
| return render_template("index.html", checkpoint_dir=checkpoint_dir) |
|
|
|
|
| checkpoint_dir = None |
|
|
|
|
| @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/<program_id>") |
| 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 or {})} |
| 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) |
|
|
| |
| 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"<script>window.STATIC_DATA = {data_json};</script>" |
|
|
| |
| 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() |
|
|
| |
| html = _re.sub(r"\{\{\s*url_for\('static', filename='([^']+)'\)\s*\}\}", r"static/\1", html) |
| script_tag_idx = html.find('<script type="module"') |
|
|
| if script_tag_idx != -1: |
| html = html[:script_tag_idx] + inlined + "\n" + html[script_tag_idx:] |
| else: |
| html = html.replace("</body>", inlined + "\n</body>") |
|
|
| with open(os.path.join(output_dir, "index.html"), "w", encoding="utf-8") as f: |
| f.write(html) |
|
|
| |
| static_src = os.path.join(os.path.dirname(__file__), "static") |
| static_dst = os.path.join(output_dir, "static") |
| if os.path.exists(static_dst): |
| shutil.rmtree(static_dst) |
| shutil.copytree(static_src, static_dst) |
|
|
| logger.info( |
| f"Static export written to {output_dir}/\n" |
| f"Note: use a web server, not file://. " |
| f"Try: python3 -m http.server --directory {output_dir} 8080" |
| ) |
|
|
|
|
| |
| app.register_blueprint(create_manual_blueprint(lambda: os.environ.get("EVOLVE_OUTPUT", "examples/"))) |
|
|
|
|
| if __name__ == "__main__": |
| import argparse |
|
|
| parser = argparse.ArgumentParser(description="OpenEvolve Evolution Visualizer") |
| parser.add_argument( |
| "--path", |
| type=str, |
| default="examples/", |
| help="Path to OpenEvolve run output directory (e.g. openevolve_output) or its checkpoints/checkpoint_*.", |
| ) |
| parser.add_argument("--host", type=str, default="127.0.0.1") |
| parser.add_argument("--port", type=int, default=8080) |
| parser.add_argument( |
| "--log-level", |
| type=str, |
| default="INFO", |
| help="Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)", |
| ) |
| parser.add_argument( |
| "--static-output", |
| type=str, |
| default=None, |
| help="Produce a static HTML export in this directory and exit.", |
| ) |
| args = parser.parse_args() |
|
|
| log_level = getattr(logging, args.log_level.upper(), logging.INFO) |
| logging.basicConfig(level=log_level, format="[%(asctime)s] %(levelname)s %(name)s: %(message)s") |
|
|
| logger.info(f"Current working directory: {os.getcwd()}") |
|
|
| if args.static_output: |
| run_static_export(args) |
|
|
| os.environ["EVOLVE_OUTPUT"] = args.path |
| logger.info(f"Starting server at http://{args.host}:{args.port} with log level {args.log_level.upper()}") |
| logger.info(f"Manual UI: http://{args.host}:{args.port}/manual") |
| app.run(host=args.host, port=args.port, debug=True) |
|
|