hello,
I was wondering if someone could help me out getting started with how to
pass variables (in other ways besides using form requests). I keep reading
documentation and its not gelling with. Does flask g only work with
database connections or can it used with any type variables? Is it better
to use sessions? I sort have the same misunderstanding about how to
implement sessions too (is there good example out there).
Basically it would be great if I can do something like this.
from flask import Flask
from flask import g
@app.route('/')
def index():
g.a = 0 # here I create the variable
return render_template('do-something.html')
@app.route('/initialized')
def nindex():
print g.a # here I want to just print it
return render_template('do-something-else.html')
thanks,
Richard
Richard, The only way to "pass variables" between requests is to store them somewhere in between. The `g` object is just a place for storing stuff in the context of a single request. For your case, you have a few options: 1. Store the variable on the session, (which is then stored in the user's browser). You just want to avoid saving too much stuff here, as it is sent back and forth every time. 2. Keep it in an url parameter, and have all links reflect that. Ugly urls, but it makes sense if you only want to keep things around for the next link a user clicks on. 3. Save it to a database like Redis, Postgres, etc, and simply store something like a key on the session or url (as in the examples above). This would be useful if you want to store lots of stuff, where persisting it server-side makes more sense. I'm sure I'm missing some things, but those are a few options. Let me know if you have any questions about how to implement any of these. - Kyle On Tue, Nov 15, 2011 at 9:28 AM, Richard Rouse <rjdrouse@gmail.com> wrote: > hello, > I was wondering if someone could help me out getting started with how to > pass variables (in other ways besides using form requests). I keep reading > documentation and its not gelling with. Does flask g only work with database > connections or can it used with any type variables? Is it better to use > sessions? I sort have the same misunderstanding about how to implement > sessions too (is there good example out there). > Basically it would be great if I can do something like this. > from flask import Flask > from flask import g > @app.route('/') > def index(): > g.a = 0 # here I create the variable > return render_template('do-something.html') > @app.route('/initialized') > def nindex(): > print g.a # here I want to just print it > return render_template('do-something-else.html') > > thanks, > Richard >
Hi Kyle, Thanks for the suggestions, so I ended just writing the data into a file. In case I am using the technique as a type of flag to call specific requests on a page. Your email helped a lot to identify my options so thanks. cheers, Richard On Tue, Nov 15, 2011 at 9:47 AM, Kyle Jones <kyle@bucebuce.com> wrote: > Richard, > > The only way to "pass variables" between requests is to store them > somewhere in between. The `g` object is just a place for storing stuff > in the context of a single request. > > For your case, you have a few options: > > 1. Store the variable on the session, (which is then stored in the > user's browser). You just want to avoid saving too much stuff here, as > it is sent back and forth every time. > 2. Keep it in an url parameter, and have all links reflect that. Ugly > urls, but it makes sense if you only want to keep things around for > the next link a user clicks on. > 3. Save it to a database like Redis, Postgres, etc, and simply store > something like a key on the session or url (as in the examples above). > This would be useful if you want to store lots of stuff, where > persisting it server-side makes more sense. > > I'm sure I'm missing some things, but those are a few options. > > Let me know if you have any questions about how to implement any of these. > > - Kyle > > On Tue, Nov 15, 2011 at 9:28 AM, Richard Rouse <rjdrouse@gmail.com> wrote: > > hello, > > I was wondering if someone could help me out getting started with how to > > pass variables (in other ways besides using form requests). I keep > reading > > documentation and its not gelling with. Does flask g only work with > database > > connections or can it used with any type variables? Is it better to use > > sessions? I sort have the same misunderstanding about how to implement > > sessions too (is there good example out there). > > Basically it would be great if I can do something like this. > > from flask import Flask > > from flask import g > > @app.route('/') > > def index(): > > g.a = 0 # here I create the variable > > return render_template('do-something.html') > > @app.route('/initialized') > > def nindex(): > > print g.a # here I want to just print it > > return render_template('do-something-else.html') > > > > thanks, > > Richard > > >