def update_weight()

in project/paperbench/paperbench/gui/app.py [0:0]


def update_weight():
    """Updates the weight for the given `node_id`."""
    with lock:  # Use lock to support concurrent requests
        data = sanitize_request_data(request.json)
        node_id = data.get("node_id")
        new_weight = data.get("weight")

        try:
            new_weight = int(new_weight)

            with open(app.config["PATH_TO_PAPER"] / app.config["RUBRIC_FILE_NAME"], "r") as f:
                rubric = json.load(f)

            old_root = app.config["NODE_TYPE"].from_dict(rubric)
            old_node = old_root.find(node_id)
            new_node = old_node.set_weight(new_weight)
            new_root = old_root.replace(node_id, new_node)

            with open(app.config["PATH_TO_PAPER"] / app.config["RUBRIC_FILE_NAME"], "w") as f:
                json.dump(new_root.to_dict(), f, indent=4)

            return jsonify({"status": "success"})
        except Exception as e:
            return jsonify(
                {
                    "status": "error",
                    "message": f"Error updating weight for node {node_id}: {str(e)}",
                }
            )