Flask/jinjas - url_for - 来自 pandas 迭代数据(itertuples)
Flask/jinjas - url_for - from pandas iterated data (itertuples)
迭代 pandas 数据帧得到 HTML table。
除了简单地显示数据框的第一列外,我还希望它通过标签成为 app.route(‘account’) 的 link。
<table>
{% for row in df.itertuples() %}
<tr>
<td><a href="{{ url_for('account',table_name='{{ row[1] }}') }}">{{ row[1] }}</a></td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
</tr>
{% endfor %}
</table>
使用 table_name=‘{{ row[1] }}
无效。 HTML 输出中显示的文本是 table_name
变量的正确字符串。 (即“Bank_Account”、“产品”等)。然而,使用 ’{{ row[1] }}’
给出类似 %7B%7B%20row%5B1%5D%20%7D%7D
而不是 Bank_Account
.
我尝试使用 {{ row[1]|safe }}
使用字符串作为文本,Bank_Account
(例如)对银行帐户行有效,即这有效:
<td><a href="{{ url_for('account',table_name='Bank_Account') }}">{{ row[1] }}</a></td>
MySQL 语法错误,我认为这是由于错误的 jinjas 用法导致 row[1]
:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{ row[1] }} ORDER BY Date' at line 1
app.routes
@app.route('/')
def trial_balance():
data = {
'Account': table_list,
'Balance': Balance,
}
...
df = pandas.DataFrame (data, columns = ['Account','Balance'])
df['Dr'] = ''
df['Cr'] = ''
...
return render_template('trial_balance.html',df=df,database=database)
@app.route('/account/<string:table_name>')
def account(table_name):
...
df = pandas.read_sql_query("SELECT * FROM "+table_name+" ORDER BY Date;", con=mydb)
account = df.to_html(index=False)
...
return render_template('individual_account.html',account=account,account_name=table_name)
你能试试下面的代码吗?
<td><a href="{{ url_for('account',table_name=row[1]) }}">{{ row[1] }}</a></td>
我的理解是当“解析器”检测到花括号时,它进入“python-执行”模式。因此对于 {{ url_for('account',table_name=row[1]) }}
变量 row
应该已经可以访问了,我们应该将花括号中的部分视为正常的 python 代码。
迭代 pandas 数据帧得到 HTML table。 除了简单地显示数据框的第一列外,我还希望它通过标签成为 app.route(‘account’) 的 link。
<table>
{% for row in df.itertuples() %}
<tr>
<td><a href="{{ url_for('account',table_name='{{ row[1] }}') }}">{{ row[1] }}</a></td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
</tr>
{% endfor %}
</table>
使用 table_name=‘{{ row[1] }}
无效。 HTML 输出中显示的文本是 table_name
变量的正确字符串。 (即“Bank_Account”、“产品”等)。然而,使用 ’{{ row[1] }}’
给出类似 %7B%7B%20row%5B1%5D%20%7D%7D
而不是 Bank_Account
.
我尝试使用 {{ row[1]|safe }}
使用字符串作为文本,Bank_Account
(例如)对银行帐户行有效,即这有效:
<td><a href="{{ url_for('account',table_name='Bank_Account') }}">{{ row[1] }}</a></td>
MySQL 语法错误,我认为这是由于错误的 jinjas 用法导致 row[1]
:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{ row[1] }} ORDER BY Date' at line 1
app.routes
@app.route('/')
def trial_balance():
data = {
'Account': table_list,
'Balance': Balance,
}
...
df = pandas.DataFrame (data, columns = ['Account','Balance'])
df['Dr'] = ''
df['Cr'] = ''
...
return render_template('trial_balance.html',df=df,database=database)
@app.route('/account/<string:table_name>')
def account(table_name):
...
df = pandas.read_sql_query("SELECT * FROM "+table_name+" ORDER BY Date;", con=mydb)
account = df.to_html(index=False)
...
return render_template('individual_account.html',account=account,account_name=table_name)
你能试试下面的代码吗?
<td><a href="{{ url_for('account',table_name=row[1]) }}">{{ row[1] }}</a></td>
我的理解是当“解析器”检测到花括号时,它进入“python-执行”模式。因此对于 {{ url_for('account',table_name=row[1]) }}
变量 row
应该已经可以访问了,我们应该将花括号中的部分视为正常的 python 代码。