Home > database >  Python flask json, i have a list of dictionaries i need to load and then return them as a table in h
Python flask json, i have a list of dictionaries i need to load and then return them as a table in h

Time:11-06

I have a json file where the data is a list with dictionaries as data What i need to do is take index 0 for instance, and return the firstname, mail and username keys values. the json data is like this: [{},{},{},{}]

this is my code right now:

@app.route("/showallusers")
def show_users():
temp_list = []
with open("users.json") as file:
    file = json.load(file)

res = """
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
    <tr>
        <th>Navn</th>
        <th>Brugernavn</th>
        <th>Email</th>
    </tr>
</thead>
<tbody>
"""

i = 0

for i in len(file):
    res  = """<tr>"""
    j = 0
    for j in len(file):
        res  = """<td>""" f"{file[i]}" """</td>"""
    res  = """</tr>"""
res  = """
</tbody>
</table>
</body>
</html>
"""
return res

the table should look like this and should repeat adding tablerows with table data for each dictionary in the list: |Name|Username|Mail| |:---|--------|--------------| |bob|bob1234 |[email protected]|

CodePudding user response:

Your for loop will change into something like this:

for row in file:
    res  = """<tr>"""
    for key, value in row.items():
        res  = f"""<td> {value} </td>"""
    res  = """</tr>"""
  • Related