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>
CodePudding user response:
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.
CodePudding user response:
For your data obviously you would like to generate a list of tuples like this:
>>> data = []
>>> row = tuple()
>>> row = ('rolf',)
>>> row = ('software engineer',)
>>> row = (4500,)
>>> row
('rolf', 'software engineer', 4500)
>>> data.append(row)
>>> data
[('rolf', 'software engineer', 4500)]
>>>
And your HTML code:
<table>
<thead>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

