Forums

Loading 2 dictionaries into 2 html tables

I have a currently running app here that passes a dictionary from my main python file to a table in my html file, and it works. I am trying to pass a second dictionary to the same html file to display in a second table, but I get errors in my log that I can't figure out. If I try to pass the second dictionary by itself, everything works. I feel like I am missing something obvious or making a dumb newbie error.

This is the end of my main function in my python file passing the 2 dictionaries:

    return weeklyPointsDict, sortedTotalsDict
   # print('Week number: ', week)

main()

This is the flaskApp.py

# flask app to launch scoreboard

from flask import Flask, render_template
import harknessScoreboard
#import testDict

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/")
#@app.route("/scoreboard")
def scoreboard():
    weeklyPointsDict = harknessScoreboard.main()
    sortedTotalsDict = harknessScoreboard.main()
    #weeklyPointsDict = testDict.main()
    #weeklyPointsDict = {"Julian": 25, "Bob": 26, "Dan": 47, "Cornelius": 3}
    return render_template("scoreboard.html", weeklyPointsDict=weeklyPointsDict, sortedTotalsDict=sortedTotalsDict)
    #print(weeklyPointsDict)

#scoreboard()

And here is my template html

<!doctype html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    <title>Harkness Scoreboard</title>
</head>
<body>
    <img src="/static/images/HarknessLogo.svg" alt="Harkness Logo" style="max-width:25%;height:auto;">
    <h2 style="margin-bottom: 0">Harkness Scoreboard</h2>
    <h4 style="margin-top: 0">Alpha v0.1</h4>

    <table class="centered thick-border">
        <tr>
            <th>Player</th>
            <th>Weekly Points</th>
        </tr>
        {% for k, v in weeklyPointsDict.items() %}
            <tr>
                <td>{{ k }}</td>
                <td>{{ v }}</td>
            </tr>
        {% endfor %}
    </table>
    <table class="centered thick-border">
        <tr>
            <th>Player</th>
            <th>Total Points</th>
        </tr>
        {% for k, v in sortedTotalsDict.items() %}
            <tr>
                <td>{{ k }}</td>
                <td>{{ v }}</td>
            </tr>
        {% endfor %}
    </table>
    <p>For entertainment only. See sheet for official scores. No wagering please.</p>
</body>
</html>

Any insight would be helpful.

And here is the error log:

2024-12-12 23:40:36,076: Error running WSGI application
2024-12-12 23:40:36,079: jinja2.exceptions.UndefinedError: 'tuple object' has no attribute 'items'
2024-12-12 23:40:36,079:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2095, in __call__
2024-12-12 23:40:36,079:     return self.wsgi_app(environ, start_response)
2024-12-12 23:40:36,079: 
2024-12-12 23:40:36,079:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2080, in wsgi_app
2024-12-12 23:40:36,079:     response = self.handle_exception(e)
2024-12-12 23:40:36,080: 
2024-12-12 23:40:36,080:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
2024-12-12 23:40:36,080:     response = self.full_dispatch_request()
2024-12-12 23:40:36,080: 
2024-12-12 23:40:36,080:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
2024-12-12 23:40:36,080:     rv = self.handle_user_exception(e)
2024-12-12 23:40:36,080: 
2024-12-12 23:40:36,080:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
2024-12-12 23:40:36,080:     rv = self.dispatch_request()
2024-12-12 23:40:36,080: 
2024-12-12 23:40:36,080:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
2024-12-12 23:40:36,081:     return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2024-12-12 23:40:36,081: 
2024-12-12 23:40:36,081:   File "/home/HarknessFB/mysite/flask_app.py", line 17, in scoreboard
2024-12-12 23:40:36,081:     return render_template("scoreboard.html", weeklyPointsDict=weeklyPointsDict, sortedTotalsDict=sortedTotalsDict)
2024-12-12 23:40:36,081: 
2024-12-12 23:40:36,081:   File "/usr/local/lib/python3.10/site-packages/flask/templating.py", line 148, in render_template
2024-12-12 23:40:36,081:     return _render(
2024-12-12 23:40:36,081: 
2024-12-12 23:40:36,082:   File "/usr/local/lib/python3.10/site-packages/flask/templating.py", line 128, in _render
2024-12-12 23:40:36,082:     rv = template.render(context)
2024-12-12 23:40:36,082: 
2024-12-12 23:40:36,082:   File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
2024-12-12 23:40:36,082:     self.environment.handle_exception()
2024-12-12 23:40:36,082: 
2024-12-12 23:40:36,082:   File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
2024-12-12 23:40:36,082:     raise rewrite_traceback_stack(source=source)
2024-12-12 23:40:36,082: 
2024-12-12 23:40:36,082:   File "/home/HarknessFB/mysite/templates/scoreboard.html", line 18, in top-level template code
2024-12-12 23:40:36,082:     {% for k, v in weeklyPointsDict.items() %}
2024-12-12 23:40:36,082: 
2024-12-12 23:40:36,083:   File "/usr/local/lib/python3.10/site-packages/jinja2/utils.py", line 83, in from_obj
2024-12-12 23:40:36,083:     if hasattr(obj, "jinja_pass_arg"):

Your main function returns a tuple, and you are assigning both items in the tuple to each variable.

Instead of:

weeklyPointsDict = harknessScoreboard.main()
sortedTotalsDict = harknessScoreboard.main()

You should unpack the tuple like this:

weeklyPointsDict, sortedTotalsDict = harknessScoreboard.main()

I think I get it. It’s treating the 2 values as a tuple and not 2 discreet values. Thanks, I’ll give that a try.

OK -- let us know if you have any further problems!