如何在 Flask 应用程序中将 ID 列显示为路由超链接?
How can I display ID column as routing hyperlink in flask app?
我是 Flask 的新手,需要逐步解释(如果可能的话)如何将路由超链接添加到我 view.html
table 中 ID 列中的所有值。
我试过将可点击的 URL 添加到 pandas 列,但它似乎不起作用。我需要从 view.html
中显示的 table 中 ID 列中的每个值转到 /templates/ids/.html
有人能解释一下这是如何一步一步完成的吗?感谢您的理解与帮助!
my.csv
date,ID,Name,Value1,Value2,Value3
01-09-2020,1,ACME,0,0,0
02-09-2020,1,ACME,0,0,0
app.py
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route('/ids/<id>')
def landing_page(id):
return print("Hello World" + <ID>)
@app.route("/")
def show_home():
data = pd.read_csv("/path/my.csv", quotechar='"')
def make_clickable(val):
return '<a href="{{ url_for('templates', filename='ids/<ID>.html') }}">{<ID>}</a>'.format(val,val)
data['ID'].style.format(make_clickable)
data.set_index(['ID'], inplace=True)
data.index.ID=None
myId = data.loc[data.Item=='Sword']
return render_template('view.html',tables=[myId.to_html(classes='sword')],
titles = ['na', 'Sample title'])
if __name__ == "__main__":
app.run(debug=True)
view.html
<!doctype html>
<title>Project</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='styles/style.css') }}">
<div class=page>
<h1>My header</h1>
{% for table in tables %}
<h2>{{titles[loop.index]}}</h2>
{{ table|safe }}
{% endfor %}
</div>
view.html 中的所需输出:
试试下面的方法。让我知道它是否有效。一个注意:将id文件的文件夹更改为static并将它们放在那里,因为模板文件会出错并且需要完成一些额外的工作
你的app.py
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route('/ids/<id>')
def landing_page(id):
return print("Hello World" + <ID>)
@app.route("/")
def show_home():
data = pd.read_csv("/path/my.csv", quotechar='"')
return render_template('view.html', data=data, cols=data.columns)
if __name__ == "__main__":
app.run(debug=True)
view.html
<!doctype html>
<title>Project</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='styles/style.css') }}">
<div class=page>
<h1>My header</h1>
<table>
<th>
{% for i in cols %}
<td> {{i}} </td>
{% endfor %}
</th>
{% for k in range(data|length) %}
<tr>
<td> <a href="{{ url_for('static', filename='ids/' + data.iloc[k,0]|string) }}"> {{data.iloc[k,0]}} </a> </td>
{% for j in cols[1:] %}
<td> {{data.iloc[k][j]}} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
我是 Flask 的新手,需要逐步解释(如果可能的话)如何将路由超链接添加到我 view.html
table 中 ID 列中的所有值。
我试过将可点击的 URL 添加到 pandas 列,但它似乎不起作用。我需要从 view.html
中显示的 table 中 ID 列中的每个值转到 /templates/ids/.html有人能解释一下这是如何一步一步完成的吗?感谢您的理解与帮助!
my.csv
date,ID,Name,Value1,Value2,Value3
01-09-2020,1,ACME,0,0,0
02-09-2020,1,ACME,0,0,0
app.py
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route('/ids/<id>')
def landing_page(id):
return print("Hello World" + <ID>)
@app.route("/")
def show_home():
data = pd.read_csv("/path/my.csv", quotechar='"')
def make_clickable(val):
return '<a href="{{ url_for('templates', filename='ids/<ID>.html') }}">{<ID>}</a>'.format(val,val)
data['ID'].style.format(make_clickable)
data.set_index(['ID'], inplace=True)
data.index.ID=None
myId = data.loc[data.Item=='Sword']
return render_template('view.html',tables=[myId.to_html(classes='sword')],
titles = ['na', 'Sample title'])
if __name__ == "__main__":
app.run(debug=True)
view.html
<!doctype html>
<title>Project</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='styles/style.css') }}">
<div class=page>
<h1>My header</h1>
{% for table in tables %}
<h2>{{titles[loop.index]}}</h2>
{{ table|safe }}
{% endfor %}
</div>
view.html 中的所需输出:
试试下面的方法。让我知道它是否有效。一个注意:将id文件的文件夹更改为static并将它们放在那里,因为模板文件会出错并且需要完成一些额外的工作
你的app.py
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route('/ids/<id>')
def landing_page(id):
return print("Hello World" + <ID>)
@app.route("/")
def show_home():
data = pd.read_csv("/path/my.csv", quotechar='"')
return render_template('view.html', data=data, cols=data.columns)
if __name__ == "__main__":
app.run(debug=True)
view.html
<!doctype html>
<title>Project</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='styles/style.css') }}">
<div class=page>
<h1>My header</h1>
<table>
<th>
{% for i in cols %}
<td> {{i}} </td>
{% endfor %}
</th>
{% for k in range(data|length) %}
<tr>
<td> <a href="{{ url_for('static', filename='ids/' + data.iloc[k,0]|string) }}"> {{data.iloc[k,0]}} </a> </td>
{% for j in cols[1:] %}
<td> {{data.iloc[k][j]}} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>