I currently am using the following code to present a sortable table using Flask. However, I would prefer that the index (i.e., first column) is not a sortable column (i.e., it always shows 1-30 regardless of what other column is sorted). In addition, this will require that the default sort position is descending instead of ascending (I am also struggling with this). The code is below and any suggestions are greatly appreciated (note that main() returns a simple pandas df with three columns: id, team_name and average_age). Thanks!
class SortableTable(Table):
id = Col('#', allow_sort=False)
Team_Name = Col('Team Name')
Average_Age = Col('Average Age')
allow_sort = True
def sort_url(self, col_key, reverse=False):
if reverse:
direction = 'desc'
else:
direction = 'asc'
return url_for('index', sort=col_key, direction=direction)
@app.route('/', methods=['GET','POST'])
def index():
df = main()
sort = request.args.get('sort', 'Team_Name')
reverse = (request.args.get('direction', 'asc') == 'desc')
df = df.sort_values(by=[sort], ascending=reverse)
output_dict = df.to_dict(orient='records')
table = SortableTable(output_dict,
sort_by=sort,
sort_reverse=reverse)
return render_template('view.html', table=table.__html__())
if __name__ == "__main__":
app.run()
Example images below of what I'm trying to accomplish:
- Default Table
- Current Sort Issue
- Desired Result on Sort