[Flask] g/session/request
Gergely Polonkai
gergely at polonkai.eu
Sat Jun 3 01:45:34 EDT 2017
On Fri, Jun 2, 2017, 16:08 Corey Boyle <coreybrett at gmail.com> wrote:
> I could be wrong about this, but...
>
> The default session storage is done with cookies, so if you store the
> data in the session, all that data will get shipped back and forth
> with each request made by the client. Also, cookies have a storage
> limit that may be an issue.
>
> The "g" proxy object is tied to the request context, so anything
> stored on it will be garbage collected when the request is fulfilled.
>
Not anymore. With 0.10, it is tied to app context, so it doesn't get
collected, but introduces another problem: every user of the system will
see the same data.
> Option 1 - Create a new function outside your view function used
> solely for building your data. Either write some custom caching code
> inside that function, or use something like Flask-Caching. Then call
> that function inside your view function.
>
+1 on Flask-Caching, but beware if you fetch your data through an ORM like
SQLAlchemy. They don't play nice with caching.
> Option 2 - Build the table in HTML as you are now, and allow the
> sorting to be done on the client side using JS. DataTables
> (https://datatables.net) is AMAZING and actually pretty easy to figure
> out.
>
I was about to suggest the same, although there's probably a reason OP
didn't do it that way. If this is a graphical browser only app, though,
it's not a problem.
> Option 3 - Option 1 + Option 2... Doing the sorting on the client side
> would be much more efficient, and caching the data on the server side
> would be advisable is you are expecting to handle many requests
> simultaneously.
>
> On Fri, Jun 2, 2017 at 9:48 AM, Craig Amundsen <amundsen.craig at gene.com>
> wrote:
> > 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
> >
> > _______________________________________________
> > Flask mailing list
> > Flask at python.org
> > https://mail.python.org/mailman/listinfo/flask
> >
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170603/cd9af0c9/attachment.html>
More information about the Flask
mailing list