问题描述
我对Flask还是很陌生,需要逐步解释(如果可能的话)如何在view.html
表的ID列中为我的所有值添加路由超链接。
我尝试将可点击的URL添加到pandas列,但似乎无法正常工作。我需要从view.html中显示的表的ID列中的每个值转到/templates/ids/.html。
有人可以解释如何逐步进行吗?感谢您的理解和帮助!
my.csv
date,ID,Name,Value1,Value2,Value3
01-09-2020,1,Acme,0
02-09-2020,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并将其放在此处,因为template文件出现错误,需要做一些额外的工作
您的app.py
EDITOR
view.html
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)