[Flask] g/session/request

Craig Amundsen amundsen.craig at gene.com
Fri Jun 2 09:48:13 EDT 2017


Hi -

I'm trying to implement a sortable table using Flask-Table. My dummy test
works great. But querying my database and building the list of
 dictionaries that the Table instance uses takes a certain amount of time.
I'd like to keep that list of dictionaries around so when the User sorts on
a different column I don't have to build it again.

Currently I have one page where the User indicates which information they
want in the table. When they click on the submit button I query the
database, construct the dictionary, store the dictionary in the session,
and then call view that shows the table. That method grabs the dictionary
out of the session and constructs the table which is then sent to the html
template. This all works great.

Clicking on a column heading ends up calling the view that shows the table
again. This time, though, the session doesn't have the dictionary and so an
error gets thrown.

I tried messing about with g, but I can't get that to work either. Does
anyone have any hints on how I can save list so it's still available when I
sort the table?

Here's some snippets to show what I'm doing:

@main.route('/select-data', methods = ['GET', 'POST'])
@login_required
@view_data_required
def select_data():
    form = SelectDataForm()
    if form.validate_on_submit():
        colsToShow = { 'showName' : True,
                       'showX'    : form.showX.data,
                       'showY'    : form.showY.data,
                       'showZ'    : form.showZ.data }
        session.dataFlags = colsToShow

        # Build the list that Table wants

        session.theList = theList
        return show_data()
    return render_template(select_data.html", form = form)

@main.route('/show-data')
@login_required
@view_data_required
def show_data():
    colsToShow =  session.dataFlags

    TableClass = create_table()
    for key, attr, header in [('showName', 'name', "Name"), ('showX', 'x',
"X"),
                              ("showY", "y", "Y"), ("showZ", "z", "Z")]:
        if colsToShow[key]:
            TableClass.add_column(attr, Col(header))

    TableClass.allow_sort = True
    TableClass.sort_url = sort_url
    TableClass.classes = ["table", "table-hover", "followers"]

    sort = request.args.get('sort', 'name')
    direction = request.args.get('direction', 'asc')
    reverse = (request.args.get('direction', 'asc') == 'desc')

    theList = session.theList
    theList = sorted(theList, key = lambda x: x[sort], reverse = reverse)
    table = TableClass(things, sort_by = sort, sort_reverse = reverse)

    return render_template("dump_data.html", table = table, sort = sort,
direction = direction)

def sort_url(self, col_key, reverse=False):
    if reverse: direction = 'desc'
    else: direction = 'asc'
    return url_for('.show_data', sort=col_key, direction=direction)


I've tried re-assigning session.theList and session.colsToShow at the
bottom of show_data, but that doesn't work. I've tried messing around with
with g instead of session, but I don't seem to grasp its use any better.

If anyone has any hints on making the session/g/request know about the two
things I need to store there the second time I call show_data (by clicking
on a column header), I'd be most appreciative.

Thanks,
- Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170602/33b00680/attachment.html>


More information about the Flask mailing list