werkzeug

Flask route giving 404 with floating point numbers in the URL

Flask route giving 404 with floating point numbers in the URL Question: I have the following route definition in my Flask app’s server.py: @app.route(‘/nearby/<float:lat>/<float:long>’) def nearby(lat, long): for truck in db.trucks.find({‘loc’: {‘$near’: [lat, long]}}).limit(5): if truck.has_key(‘loc’): del truck[‘loc’] return json.dumps(trucks) But when I go to http://localhost:5000/nearby/37.7909470419234/-122.398633589404, I get a 404. The other routes work fine, …

Total answers: 3

Flask and Werkzeug: Testing a post request with custom headers

Flask and Werkzeug: Testing a post request with custom headers Question: I’m currently testing my app with suggestions from http://flask.pocoo.org/docs/testing/, but I would like to add a header to a post request. My request is currently: self.app.post(‘/v0/scenes/test/foo’, data=dict(image=(StringIO(‘fake image’), ‘image.png’))) but I would like to add a content-md5 to the request. Is this possible? My …

Total answers: 2

Capture arbitrary path in Flask route

Capture arbitrary path in Flask route Question: I have a simple Flask route that I want to capture a path to a file. If I use <path> in the rule, it works for /get_dir/one but not /get_dir/one/two. How can I capture an arbitrary path, so that path=’/one/two/etc will be passed to the view function? @app.route(‘/get_dir/<path>’) …

Total answers: 1

Flask url_for generating http URL instead of https

Flask url_for generating http URL instead of https Question: I am using url_for to generate a redirect URL when a user has logged out: return redirect(url_for(‘.index’, _external=True)) However, when I changed the page to a https connection, the url_for still gives me http. I would like to explicitly ask url_for to add https at the …

Total answers: 7

Get raw POST body in Python Flask regardless of Content-Type header

Get raw POST body in Python Flask regardless of Content-Type header Question: Previously, I asked How to get data received in Flask request because request.data was empty. The answer explained that request.data is the raw post body, but will be empty if form data is parsed. How can I get the raw post body unconditionally? …

Total answers: 4

Get the data received in a Flask request

Get the data received in a Flask request Question: I want to be able to get the data sent to my Flask app. I’ve tried accessing request.data but it is an empty string. How do you access request data? from flask import request @app.route(‘/’, methods=[‘GET’, ‘POST’]) def parse_request(): data = request.data # data is empty …

Total answers: 24

What's the right approach for calling functions after a flask app is run?

What's the right approach for calling functions after a flask app is run? Question: I’m a little confused about how to do something that I thought would be quite simple. I have a simple app written using Flask. It looks something like this: from flask import Flask app = Flask(__name__) def _run_on_start(a_string): print “doing something …

Total answers: 3

Getting the array as GET query parameters in Python

Getting the array as GET query parameters in Python Question: I know in php I could just use $_GET[‘key1’][‘key2′] to retrieve GET data that is sent in the form of an array but is that something possible in Python as I just receive a string and it’s not recognized as an array/list. I use flask/werkzeug …

Total answers: 2

Werkzeug AttributeError: 'module' object has no attribute 'InteractiveInterpreter'

Werkzeug AttributeError: 'module' object has no attribute 'InteractiveInterpreter' Question: Using Flask (0.8) and Werkzeug (0.8.1) when attempting to run code with app.run(debug=True) I get the below described error. There are no errors when using app.run() The error Traceback (most recent call last): File “code2.py”, line 9, in <module> app.run(debug=True) File “/<snip>/env/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py”, line 703, in run …

Total answers: 2

How do I use url_for if my method has multiple route annotations?

How do I use url_for if my method has multiple route annotations? Question: So I have a method that is accessible by multiple routes: @app.route(“/canonical/path/”) @app.route(“/alternate/path/”) def foo(): return “hi!” Now, how can I call url_for(“foo”) and know that I will get the first route? Asked By: jiggy || Source Answers: Ok. It took some …

Total answers: 3