Reputation: 31
I need to add the data from the pandas dataframe to dropdown list in an html document using python flask...
@app.route('/api/v1/resources/getservices', methods=['GET'])
def api_services():
return render_template('view.html',table=df.to_html())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown</title>
<h1>Services</h1>
</head>
<body>
<select name="table" method="GET" action="/">
<option value="{{table[0]}}" selected>{{table[0]}}</option>
{% for colour in table[1:] %}
<option value="{{colour}}">{{colour}}</option>
{% endfor %}
</select>
</body>
</html>
What I am expecting is that the data from the column 'Service' in the pandas dataframe df should be added as items in the drop down list in the html file.. but whenever i am trying with the above code, drop down list is created without any items...
Upvotes: 2
Views: 5264
Reputation: 652
when you call to_html()
method, it will create the html table and you can't iterate over it. I don't know what was your df data, but i think this might work for you.
app.py
@app.route('/api/v1/resources/getservices', methods=['GET'])
def api_services():
d = {'Services': ["red", "green", "blue"]}
df = pd.DataFrame(data=d)
return render_template('view.html', table=df)
view.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown</title>
<h1>Services</h1>
</head>
<body>
<select name="table" method="GET" action="/">
{% for colour in table["Services"] %}
<option value="{{ colour }}">{{ colour }}</option>
{% endfor %}
</select>
</body>
</html>
Upvotes: 0