我有一个csv文件。我已经转换成html文件。如何使html表可通过python编辑
import pandas as pd
df=pd.read_csv("D:/valli/abc.csv")
df.to_html("x.html")
我不知道如何进行flask,javascript和jquery
答案 0 :(得分:0)
这可能对您有用。我不太了解HTML,所以如果我不了解表格的正确结构,请告诉我。
我正在使用具有以下值的示例数据框:
Id Value Name
0 1 2 Paul
1 2 5 Sam
2 3 7 Jill
3 4 10 Karl
4 5 15 Sally
5 6 4 Irma
为了实现您的目标,我们将使用Python的join()函数。这是脚本:
import pandas as pd
ddict = {
'Id': ['1', '2', '3', '4', '5', '6'],
'Value': ['2','5', '7', '10', '15', '4'],
'Name': ['Paul','Sam', 'Jill', 'Karl', 'Sally', 'Irma']
}
df = pd.DataFrame(ddict)
### Print the table tag
print('<table>')
### Print the headers to a <th> tag; Close each <th> for every line
print('\t<tr>\n\t\t<th>' + '\t\t<th>'.join([f'{i}</th>\n' for i in df.columns.values.tolist()]) + '\t</tr>')
### Iterate over dataframe values; Enclose each value in a <td> tag
for i, v in enumerate(df.values):
print('\t<tr>\n\t\t<td>' + '\t\t<td>'.join([f'{x}</td>\n' for x in v]) + '\t</tr>')
### Print table tag closure.
print('</table>')
输出:
<table>
<tr>
<th>Id</th>
<th>Value</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>Paul</td>
</tr>
<tr>
<td>2</td>
<td>5</td>
<td>Sam</td>
</tr>
<tr>
<td>3</td>
<td>7</td>
<td>Jill</td>
</tr>
<tr>
<td>4</td>
<td>10</td>
<td>Karl</td>
</tr>
<tr>
<td>5</td>
<td>15</td>
<td>Sally</td>
</tr>
<tr>
<td>6</td>
<td>4</td>
<td>Irma</td>
</tr>
</table>
答案 1 :(得分:0)
经测试可正常工作
来自下面ans的参考
Render an editable table using Flask, Jinja2 templates, then process the form data returned
您可以通过使用FormField从数据库中生成可编辑表来执行此操作 您可以使用数据库或CSV 下例显示了如何从csv生成可编辑表
form.py
class MemberForm(Form):
name = StringField('name')
member_id = StringField('member_id')
inbox_share = IntegerField('inbox_share')
# etc.
class TeamForm(Form):
title = StringField('title')
teammembers = FieldList(FormField(MemberForm))
views.py
@app.route('/support/team-members-update', methods=['GET','POST'])
def update_team_member():
#below is to cover csv col to list
columns = defaultdict(list)
with open('file.csv') as cavfile:
reader = csv.DictReader(csvfile)
for row in render:
for (k,v) in row.items():
columns[k].append(v)
member_id= columns['name of your col 1'] #this will generate a list of your col
inbox_share = columns['name of you col 2'] #this will generate list of you col
for i,j in zip(list_col1, list_col2):
member_form = MemberForm()
member_form.member_id = i
member_form.inbox_share = j
teamform.teammembers.append_entry(member_form)
return render_template('ediit-team.html',teamform=teamform)
edit-team.html
<html>
<head>
<title>Edit Team Members</title>
</head>
<body>
<h1>Edit Team</h1>
<div>
<form action="" method="post" name="teamform">
{{ teamform.hidden_tag() }}
Team Title: {{ teamform.title }}<br>
<div>
<table>
<tr>
<th> Name </th>
<th> ID </th>
<th> Inbox Share </th>
</tr>
{% for member in teamform.teammembers %}
<tr>
<td>{{ member.member_id }}</td>
<td>{{ member.inbox_share }}</td>
</tr>
{% endfor %}
</table>
</div>
<p><input type="submit" name="edit" value="Send"></p>
</form>
</div>
</body>