Setup Flask routes.
(self)
| 669 | return json.dumps(fig, cls=PlotlyJSONEncoder) |
| 670 | |
| 671 | def _setup_routes(self): |
| 672 | """Setup Flask routes.""" |
| 673 | |
| 674 | @self.app.route('/') |
| 675 | def index(): |
| 676 | """Main page.""" |
| 677 | return render_template('index.html') |
| 678 | |
| 679 | @self.app.route('/api/data') |
| 680 | def get_data(): |
| 681 | """Get chart data.""" |
| 682 | try: |
| 683 | # Reload data on each request (when page refreshes) |
| 684 | self._load_data() |
| 685 | |
| 686 | data = self._prepare_chart_data() |
| 687 | |
| 688 | # Check if we have data |
| 689 | if not data["timestamps"]: |
| 690 | return jsonify({ |
| 691 | "error": "No data available", |
| 692 | "message": "No account values found in records" |
| 693 | }), 400 |
| 694 | |
| 695 | account_value_chart = self._create_account_value_chart(data) |
| 696 | returns_chart = self._create_returns_chart(data) |
| 697 | crypto_prices_chart = self._create_crypto_prices_chart(data) |
| 698 | |
| 699 | return jsonify({ |
| 700 | "account_value_chart": account_value_chart, |
| 701 | "returns_chart": returns_chart, |
| 702 | "crypto_prices_chart": crypto_prices_chart, |
| 703 | "action_points": data["action_points"] |
| 704 | }) |
| 705 | except Exception as e: |
| 706 | import traceback |
| 707 | error_msg = f"Error preparing chart data: {str(e)}\n{traceback.format_exc()}" |
| 708 | print(error_msg) |
| 709 | return jsonify({ |
| 710 | "error": "Failed to prepare chart data", |
| 711 | "message": str(e) |
| 712 | }), 500 |
| 713 | |
| 714 | @self.app.route('/api/action/<int:point_index>') |
| 715 | def get_action_details(point_index: int): |
| 716 | """Get action details for a specific point.""" |
| 717 | data = self._prepare_chart_data() |
| 718 | if 0 <= point_index < len(data["action_points"]): |
| 719 | point = data["action_points"][point_index] |
| 720 | return jsonify({ |
| 721 | "timestamp": point["timestamp"], |
| 722 | "account_value": point["account_value"], |
| 723 | "actions": point["actions"], |
| 724 | "thinking": point["actions"][0]["thinking"] if point["actions"] else None |
| 725 | }) |
| 726 | return jsonify({"error": "Invalid point index"}), 404 |
| 727 | |
| 728 | def stop(self): |