0

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

  • Default Table

Current Sort Issue

  • Current Sort Issue

Desired Result

  • Desired Result on Sort
Michael M
  • 57
  • 8
  • What do you mean by "id will always show 1-30 _regardless_ of what other column is sorted" ? That does not even make sense. Think about it again, or show us what you are trying to achieve with images. – BcK Jan 27 '22 at 23:57
  • I think you are confusing the term `ID` with the `row number`. – BcK Jan 27 '22 at 23:59
  • Well said - I am essentially equating ID & row number (i.e., I would like to show the row number). I've added images to support my question! – Michael M Jan 28 '22 at 14:32

0 Answers0