Pass Flask wtfform result data to another function [duplicate] - python

Suppose I have following case;
#app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
#app.route('/b', methods=['GET'])
def b():
print a
....
In the redered page there is one link that directs page /a to /b. I try to pass variable a to page /b to reuse it. How should I do this Flask app? Do I need to use session or is there any other solution?

If you want to pass some python value around that the user doesn't need to see or have control over, you can use the session:
#app.route('/a')
def a():
session['my_var'] = 'my_value'
return redirect(url_for('b'))
#app.route('/b')
def b():
my_var = session.get('my_var', None)
return my_var
The session behaves like a dict and serializes to JSON. So you can put anything that's JSON serializable in the session. However, note that most browsers don't support a session cookie larger than ~4000 bytes.
You should avoid putting large amounts of data in the session, since it has to be sent to and from the client every request. For large amounts of data, use a database or other data storage. See Are global variables thread safe in flask? How do I share data between requests? and Store large data or a service connection per Flask session.
If you want to pass a value from a template in a url, you can use a query parameter:
Send my_value
will produce the url:
/b?my_var=my_value
which can be read from b:
#app.route('/b')
def b():
my_var = request.args.get('my_var', None)

The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b. Alternatively you could store the value for a in a session variable to access it between views.

Related

Is it possible to write to the attributes of an AnonymousUserMixin subclass? [duplicate]

Suppose I have following case;
#app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
#app.route('/b', methods=['GET'])
def b():
print a
....
In the redered page there is one link that directs page /a to /b. I try to pass variable a to page /b to reuse it. How should I do this Flask app? Do I need to use session or is there any other solution?
If you want to pass some python value around that the user doesn't need to see or have control over, you can use the session:
#app.route('/a')
def a():
session['my_var'] = 'my_value'
return redirect(url_for('b'))
#app.route('/b')
def b():
my_var = session.get('my_var', None)
return my_var
The session behaves like a dict and serializes to JSON. So you can put anything that's JSON serializable in the session. However, note that most browsers don't support a session cookie larger than ~4000 bytes.
You should avoid putting large amounts of data in the session, since it has to be sent to and from the client every request. For large amounts of data, use a database or other data storage. See Are global variables thread safe in flask? How do I share data between requests? and Store large data or a service connection per Flask session.
If you want to pass a value from a template in a url, you can use a query parameter:
Send my_value
will produce the url:
/b?my_var=my_value
which can be read from b:
#app.route('/b')
def b():
my_var = request.args.get('my_var', None)
The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b. Alternatively you could store the value for a in a session variable to access it between views.

How to correctly pass a variable using redirect function in Flask? [duplicate]

Suppose I have following case;
#app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
#app.route('/b', methods=['GET'])
def b():
print a
....
In the redered page there is one link that directs page /a to /b. I try to pass variable a to page /b to reuse it. How should I do this Flask app? Do I need to use session or is there any other solution?
If you want to pass some python value around that the user doesn't need to see or have control over, you can use the session:
#app.route('/a')
def a():
session['my_var'] = 'my_value'
return redirect(url_for('b'))
#app.route('/b')
def b():
my_var = session.get('my_var', None)
return my_var
The session behaves like a dict and serializes to JSON. So you can put anything that's JSON serializable in the session. However, note that most browsers don't support a session cookie larger than ~4000 bytes.
You should avoid putting large amounts of data in the session, since it has to be sent to and from the client every request. For large amounts of data, use a database or other data storage. See Are global variables thread safe in flask? How do I share data between requests? and Store large data or a service connection per Flask session.
If you want to pass a value from a template in a url, you can use a query parameter:
Send my_value
will produce the url:
/b?my_var=my_value
which can be read from b:
#app.route('/b')
def b():
my_var = request.args.get('my_var', None)
The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b. Alternatively you could store the value for a in a session variable to access it between views.

Generate a random username for each session [duplicate]

Suppose I have following case;
#app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
#app.route('/b', methods=['GET'])
def b():
print a
....
In the redered page there is one link that directs page /a to /b. I try to pass variable a to page /b to reuse it. How should I do this Flask app? Do I need to use session or is there any other solution?
If you want to pass some python value around that the user doesn't need to see or have control over, you can use the session:
#app.route('/a')
def a():
session['my_var'] = 'my_value'
return redirect(url_for('b'))
#app.route('/b')
def b():
my_var = session.get('my_var', None)
return my_var
The session behaves like a dict and serializes to JSON. So you can put anything that's JSON serializable in the session. However, note that most browsers don't support a session cookie larger than ~4000 bytes.
You should avoid putting large amounts of data in the session, since it has to be sent to and from the client every request. For large amounts of data, use a database or other data storage. See Are global variables thread safe in flask? How do I share data between requests? and Store large data or a service connection per Flask session.
If you want to pass a value from a template in a url, you can use a query parameter:
Send my_value
will produce the url:
/b?my_var=my_value
which can be read from b:
#app.route('/b')
def b():
my_var = request.args.get('my_var', None)
The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b. Alternatively you could store the value for a in a session variable to access it between views.

How to return variables in Python Flask function and use them in another function? [duplicate]

Suppose I have following case;
#app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
#app.route('/b', methods=['GET'])
def b():
print a
....
In the redered page there is one link that directs page /a to /b. I try to pass variable a to page /b to reuse it. How should I do this Flask app? Do I need to use session or is there any other solution?
If you want to pass some python value around that the user doesn't need to see or have control over, you can use the session:
#app.route('/a')
def a():
session['my_var'] = 'my_value'
return redirect(url_for('b'))
#app.route('/b')
def b():
my_var = session.get('my_var', None)
return my_var
The session behaves like a dict and serializes to JSON. So you can put anything that's JSON serializable in the session. However, note that most browsers don't support a session cookie larger than ~4000 bytes.
You should avoid putting large amounts of data in the session, since it has to be sent to and from the client every request. For large amounts of data, use a database or other data storage. See Are global variables thread safe in flask? How do I share data between requests? and Store large data or a service connection per Flask session.
If you want to pass a value from a template in a url, you can use a query parameter:
Send my_value
will produce the url:
/b?my_var=my_value
which can be read from b:
#app.route('/b')
def b():
my_var = request.args.get('my_var', None)
The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b. Alternatively you could store the value for a in a session variable to access it between views.

Flask: How can I get the parameter with the second query which is used for the function called with the first query? [duplicate]

Suppose I have following case;
#app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
#app.route('/b', methods=['GET'])
def b():
print a
....
In the redered page there is one link that directs page /a to /b. I try to pass variable a to page /b to reuse it. How should I do this Flask app? Do I need to use session or is there any other solution?
If you want to pass some python value around that the user doesn't need to see or have control over, you can use the session:
#app.route('/a')
def a():
session['my_var'] = 'my_value'
return redirect(url_for('b'))
#app.route('/b')
def b():
my_var = session.get('my_var', None)
return my_var
The session behaves like a dict and serializes to JSON. So you can put anything that's JSON serializable in the session. However, note that most browsers don't support a session cookie larger than ~4000 bytes.
You should avoid putting large amounts of data in the session, since it has to be sent to and from the client every request. For large amounts of data, use a database or other data storage. See Are global variables thread safe in flask? How do I share data between requests? and Store large data or a service connection per Flask session.
If you want to pass a value from a template in a url, you can use a query parameter:
Send my_value
will produce the url:
/b?my_var=my_value
which can be read from b:
#app.route('/b')
def b():
my_var = request.args.get('my_var', None)
The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b. Alternatively you could store the value for a in a session variable to access it between views.

Categories

Resources