Python 如何在flask应用程序中将ID列显示为路由超链接?
我对flask非常陌生,需要一个逐步的解释(如果可能的话),说明如何将路由超链接添加到我的Python 如何在flask应用程序中将ID列显示为路由超链接?,python,pandas,flask,url-routing,Python,Pandas,Flask,Url Routing,我对flask非常陌生,需要一个逐步的解释(如果可能的话),说明如何将路由超链接添加到我的view.html表中ID列中的所有值 我曾尝试将可点击的URL添加到熊猫栏,但似乎不起作用。我需要从view.html中显示的表的ID列中的每个值转到/templates/ids/.html 有人能解释一下如何一步一步地做到这一点吗?感谢您的理解和帮助 my.csv date,ID,Name,Value1,Value2,Value3 01-09-2020,1,ACME,0,0,0 02-09-2020,1
view.html
表中ID列中的所有值
我曾尝试将可点击的URL添加到熊猫栏,但似乎不起作用。我需要从view.html中显示的表的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)
从烧瓶导入*
作为pd进口熊猫
app=烧瓶(名称)
@app.route(“/ids/”)
def登录页面(id):
返回打印(“Hello World”+)
@附件路线(“/”)
def show_home():
data=pd.read_csv(“/path/my.csv”,quotechar='”)
def使_可点击(val):
返回“”。格式(val,val)
数据['ID'].style.format(使可点击)
data.set_索引(['ID'],inplace=True)
data.index.ID=None
myId=data.loc[data.Item=='swarm']
返回render_template('view.html',tables=[myId.to_html(class='swill')],
标题=['na','Sample title'])
如果名称=“\uuuuu main\uuuuuuuu”:
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>
项目
我的头球
{对于表%%中的表,%}
{{titles[loop.index]}
{{表|安全}}
{%endfor%}
view.html中所需的输出:
尝试以下方法。让我知道它是否有效。注意:将id文件的文件夹更改为静态,并将它们放在那里,因为模板文件会出错,需要做一些额外的工作 你的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)
从烧瓶导入*
作为pd进口熊猫
app=烧瓶(名称)
@app.route(“/ids/”)
def登录页面(id):
返回打印(“Hello World”+)
@附件路线(“/”)
def show_home():
data=pd.read_csv(“/path/my.csv”,quotechar='”)
返回呈现模板('view.html',data=data,cols=data.columns)
如果名称=“\uuuuu main\uuuuuuuu”:
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>
项目
我的头球
{cols%中的i的%s}
{{i}
{%endfor%}
{范围内k的百分比(数据|长度)%}
{%j在cols[1:]}
{{data.iloc[k][j]}
{%endfor%}
{%endfor%}
对于您最终想要显示的内容,它看起来非常复杂。你能提供一些你的最终结果(html)的例子吗?我相信有更简单的解决方案可以做到这一点,除非你有意让它如此复杂,你需要把href放在你的模板循环中。Jinja不会替换变量中的其他变量(有一些方法可以对其进行预处理,但这是毫无意义的)。跟随烧瓶开始了解烧瓶和Jinja如何工作Hi archer,我的目标是尽可能简单,但由于我对烧瓶知之甚少,我可能做错了。。。我添加了所需的输出和.csv格式。你能帮忙吗?谢谢我看了烧瓶文档,但我想如果可能的话,我还需要一些逐步的指导…非常感谢阿切尔!现在对我有用了!我只需要在这行中添加.html
:
太棒了!很高兴听到这个消息