Problem to show data dynamically table in python flask

2024/9/22 8:22:47

I want to show a table in the html using python flask framework. I have two array. One for column heading and another for data record. The length of the column heading and data record are dynamic. I can dynamically manage the column 'headings'. But simply append information into the 'data' array is not showing the data correctly. Please help me to solve this problem. Should change something in the html file to make it dynamic? Python file from flask import Flask, request, render_template

app = Flask(__name__)@app.route('/')
def my_form():#headings = ("name", "role", "salary")headings = []headings.append("name")   headings.append("role")   data1 = ("rolf", "software engineer", "4500"), ("neu", "civil engineer", "1500"), ("neu", "civil engineer", "1500")
# =============================================================================data = []data.append("rolf")data.append("software engineer")data.append("neu")data.append("civil engineer")# =============================================================================print (data1)print (data)#ss =  '('+'('+ "rolf"+ ','+ "software engineer" + ',' + "4500" + ')'+','+')'return render_template('table2.html', data=data, headings=headings)if __name__ == '__main__':app.run()

html file

<table>
<tr>
{% for header in headings %}<th>{{ header }}</th>{% endfor %}</tr>{% for row in data %}<tr>{% for cell in row %}<td>{{ cell }}</td>{% endfor %}</tr>{% endfor %}
</table>

enter image description here

Answer

data1 is a tuple of tuples.

>>> data1 = ("rolf", "software engineer", "4500"), ("neu", "civil engineer", "1500"), ("neu", "civil engineer", "1500")
>>> data1
(('rolf', 'software engineer', '4500'), ('neu', 'civil engineer', '1500'), ('neu', 'civil engineer', '1500'))

data, which becomes a list of tuples has a completely different structure when built like this:

>>> data = []
>>> data.append("rolf")
>>> data.append("software engineer")
>>> data.append("neu")
>>> data.append("civil engineer") 
>>> data
['rolf', 'software engineer', 'neu', 'civil engineer']
>>> 

I think you're looking for:

>>> data = []
>>> data.append(("rolf","software engineer", "4500"))
>>> data.append(("neu","civil engineer", "1500"))
>>> data
[('rolf', 'software engineer', '4500'), ('neu', 'civil engineer', '1500')]

Perhaps this isn't the best way to represent the data. It's difficult to give further advice in the context of this question though.

I'm guessing this is just for educational purposes, and you're aware that in other types of implementation this info may be retreived from a database, or come from an API response, rather than being defined statically in a py file like this.

https://en.xdnf.cn/q/119156.html

Related Q&A

RuntimeError: generator raised StopIteration

I am in a course and try to find my problem. I cant understand why if I enter something other than 9 digits, the if should raise the StopIteration and then I want it to go to except and print it out. W…

split list elements into sub-elements in pandas dataframe

I have a dataframe as:-Filtered_data[defence possessed russia china,factors driving china modernise] [force bolster pentagon,strike capabilities pentagon congress detailing china] [missiles warheads, d…

Image does not display on Pyqt [duplicate]

This question already has an answer here:Why Icon and images are not shown when I execute Python QT5 code?(1 answer)Closed 2 years ago.I am using Pyqt5, python3.9, and windows 11. I am trying to add a…

Expand the following dictionary into following list

how to generate the following list from the following dictionary d = {2: 4, 3: 1, 5: 3}f = [2**1,2**2, 2**3, 2**4, 3**1, 5**1, 5**2, 5**3, 2**1 * 3, 2**2 * 3, 2**3 * 3, 2**4 * 3, 5**1 * 3, 5**2 * 3, 5*…

how can I improve the accuracy rate of the below trained model using CNN

I have trained a model using python detect the colors of the gemstone and have built a CNN.Herewith Iam attaching the code of mine.(Referred https://www.kaggle.com) import os import matplotlib.pyplot a…

Classes and methods, with lists in Python

I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class …

polars dataframe TypeError: must be real number, not str

so bascially i changed panda.frame to polars.frame for better speed in yolov5 but when i run the code, it works fine till some point (i dont exactly know when error occurs) and it gives me TypeError: m…

Get a string in Shell/Python using sys.argv

Im beginning with bash and Im executing a script :$ ./readtext.sh ./InputFiles/applications.txt Here is my readtext.sh code :#!/bin/bash filename="$1" counter=1 while IFS=: true; doline=read …

Need to combine two functions into one (Python)

Here is my code-def Max(lst):if len(lst) == 1:return lst[0]else:m = Max(lst[1:])if m > lst[0]: return melse:return lst[0] def Min(lst):if len(lst) == 1:return lst[0]else:m = Min(lst[1:])if m < ls…

Error: descriptor blit requires a pygame.Surface object but received a NoneType [duplicate]

This question already has answers here:How can I draw images and sprites in Pygame?(4 answers)Closed 2 years ago.I am creating a game. I wrote this code to create a sprite and its hitbox:hg = pygame.i…