Unverified Commit c6d4c7b5 authored by Claude's avatar Claude
Browse files

Fix Jinja2 template error in predict.html

Fixed UndefinedError caused by complex Jinja2 expression when calculating
Year 10 averages. Moved the average calculation to Python backend for better
maintainability and performance.

Changes:
- Calculate year10_averages in the predict() route
- Pass averages to template as a simple dictionary
- Simplified template logic to use direct dictionary lookup

🤖 Generated with [Claude Code](https://claude.com/claude-code

)

Co-Authored-By: default avatarClaude <noreply@anthropic.com>
parent 1a8e7155
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -276,9 +276,20 @@ def predict():
    # Get last 12 months as reference
    last_year = training_data['historical_data'][-12:]

    # Calculate averages for Year 10 for each watch
    year10_averages = {}
    for watch in watches:
        watch_id = watch['id']
        total_demand = sum(
            month['watches'][watch_id - 1]['demand']
            for month in last_year
        )
        year10_averages[watch_id] = round(total_demand / 12)

    return render_template('predict.html',
                          watches=watches,
                          last_year=last_year)
                          last_year=last_year,
                          year10_averages=year10_averages)


@app.route('/results')
+1 −3
Original line number Diff line number Diff line
@@ -41,9 +41,7 @@
                        <tr class="has-background-light">
                            <td><strong>Average</strong></td>
                            {% for watch in watches %}
                            {% set watch_id = watch.id %}
                            {% set avg = (last_year | map(attribute='watches') | map('selectattr', 'watch_id', 'equalto', watch_id) | list | map(attribute='0') | map(attribute='demand') | sum / 12) | round(0) | int %}
                            <td class="has-text-right"><strong>{{ avg }} units</strong></td>
                            <td class="has-text-right"><strong>{{ year10_averages[watch.id] }} units</strong></td>
                            {% endfor %}
                        </tr>
                    </tbody>