I'm very new to flask and need a step by step explanation (if possible) on how to add routing hyperlink to all my values in the ID column in my view.html
table.
I've tried adding clickable URL to pandas column, but it doesn't seem to be working. I need to go to /templates/ids/.html from each value in the ID column in table displayed in view.html
Would someone be able to explain how this can be done step by step? Thanks for your understanding and help!
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>
desired output in view.html:
Try the following way. Let me know if it works. One notice: change the folder of id files to static and put them there, because templates file gives an error and needs some additional work to be done
Your 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>