Flask - custom error messages on abort - python-3.x

I'd like to be able to display custom error messages using abort() in Flask. I have run into an unusual situation that I am having trouble with. My code is below.
from flask import Flask, abort, jsonify, make_response, request
app = Flask(__name__)
#app.errorhandler(400)
def custom400(error):
return make_response(jsonify({'message': error.description}), 400)
#app.route('/test', methods=['POST'])
def test():
abort(400, 'custom error message')
if __name__ == '__main__':
app.run()
The code above works but behaves differently if I attempt to access any form data because I lose access to the custom error message.
def test():
a = request.form['some_value']
abort(400, 'custom error message')
How can I avoid this situation? Is this a Flask bug?
Note: The code above was taken from how to get access to error message from abort command when using custom error handler

The error doesn't have a description because you aren't setting some_value in your POST body so the 400 error comes from request.form.__getitem__ - change the first line of test to a = request.form.get('some_value') and your abort error will be thrown instead.

If you want to be able to render a template together with the error message, you can define the error handler
#app.errorhandler(404)
def page_not_found(error):
error=['Page Not Found','Oops']
return render_template('errors.html', error=error), 404
To use, raise the error by
abort(404)
Make sure to import abort
from flask import abort
Then you can access the error message list in jinja

Related

flask not responding with template

I am running a flask server in testing but the render_template() method is not responding
Here is my app.py
from flask import Flask, render_template, jsonify, request
from feeders import feeder
app = Flask(__name__)
#app.route("/", methods=["GET", "POST"])
def feed():
if request.method == "GET":
data = feeder.all_feed()
return render_template("feed.html", allfeed=data)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
I am using ThreadPoolExecutor for some of my tasks, here is how the all_feed() method looks like.
def all_feed():
with ThreadPoolExecutor(max_workers=7) as executor:
results = list(executor.map(get_feed, feeder_site_urls.values()))
print(results)
return results
I can see results on the terminal but the template is not rendering.
And yes all my templates are under templates/.
Edit: I can see that the flask is consuming memory (gradually increasing)
Apparently there was a bug in one of my templates
Always remember to run in DEBUG Mode.
Also create a .flaskenv file with following contents
FLASK_APP=app.py
FLASK_ENV=development
FLASK_RUN_PORT=8000
FLASK_RUN_HOST=0.0.0.0
Thanks to one of my friend

Batch file creation to execute POST method in Flask API (windows)

I've to create a batch file so that it calls the following API POST method and executes it seamlessly on Windows. There is no input that is required to be provided to the POST method here
this is for a ML module which is being called by the API. I've tried calling the mentioned module directly through batch file and anaconda prompt but that doesn't work fine.
import CLassName1
from flask import Flask
app=Flask(__name__)
#app.route('/api/model/testing', methods=['POST'])
def test_model():
response=ClassName1.method_name1()
return response
#app.route('/test')
def post_health():
return "health"
if __name__ == '__main__':
app.run(host='127.0.0.1',port=15010, debug=True)
expected to run method_name1 and subsequent methods and then populate another file- 'Output' created in the parent folder.
actually- when method_name1 is executed directly from anaconda prompt throws an Import error after some time and keeps looping over
Can you please share the exact error stack trace that you are getting when you calls method_name1() because as long as method_name1() returns string or there is no error in method_name1(), this code should run.

Trying to combine flask and guizero

So i was trying flask when i got an funny idea. If i could combine guizero with my server i could make like a console for my simple server. So i began working when i stumbled over 2 problems.
Here's my code:
from flask import Flask, render_template
from guizero import App, PushButton, Text, TextBox
app = Flask(__name__)
app.debug = True
console = App(title='web server')
text_input = "no message"
def message():
text_input = textbox.get
textbox.clear
header = Text(console, text="Web server console", size= 50)
description = Text(console, text="type message here:")
textbox = TextBox(console)
button = PushButton(console, text="send", command=message)
#app.route('/')
def index():
return render_template('index.html', text= text_input)
#app.route('/next')
def next():
return render_template('game.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
app.display()
The template index.html is just simply a paragraph with {{text}}. It does show the "no message" string.
Now i'm experiencing 2 problems with this code.
1: If i run it it only starts the server, but when i run it again it gives the "already in use" error and then opens the gui
2: If i use the gui the website won't update when i push the button, i think because the gui doesnt run in the same instance of the script as the server. And if it does i don't think the debug function works with variables in the script.
running the server on a raspi 3B on ethernet if that is important
i'm very new to flask and html so maybe i won't understand your answer but i'd be glad if you could help
I also am new to Flask but I think you have to make a new thread (either for the Flask app or the GUI). This makes sure that both Flask and the GUI can run simultaneously. Now you try to run two loops at the same time and tht doesn't work.

Flask - url_for automatically escapes '=' to '%3D'

So.. I'm having some issues with Flask's url_for . The code still works.. but when users navigate to a link that was generated by url_for the link looks bad in the address bar.
Namely, I have a decorated view function as follows:
#app.route("/")
#app.route("/page=<int:number")
def index(number=0):
return "Index Page: {}".format(number)
This all works fine except when I try to generate a url for that route. Calling:
url_for("index", number=10)
Yields: domain.tld:80/page%3D10
Is there any way to circumvent this issue? I'd like for '=' to be returned instead of '%3D' when it's built into the route itself.
I only noticed it was doing this when I was testing it in an assert and discovered that the routes were ending up different from what I expected them to be.
At the moment, I have my test case circumvent the issue by using urllib.parse.unquote to fix the url for testing purposes. I could probably just do that for all urls since I won't have any user input to worry about those causing problems.. but it's there for a reason so.... :P
One option you have is to not build the parameter in to the route itself, but use query parameters instead:
from flask import Flask, render_template, request, url_for
app = Flask(__name__)
#app.route("/")
def index():
page = request.args.get('page', 0, type=int)
print(url_for("index", page=10)) # yields /?page=10
return "Index Page: {}".format(page)
app.run(debug=True)
My making use of query parameters for the route, you avoid the issue of Flask encoding the = sign in the route definition.

Handling atexit for multiple app objects with Flask dev server reloader

This is yet another flask dev server reloader question. There are a million questions asking why it loads everything twice, and this is not one of them. I understand that it loads everything twice, my question involves dealing with this reality and I haven't found an answer that I think addresses what I'm trying to do.
My question is, how can I cleanup all app objects at exit?
My current approach is shown below. In this example I run my cleanup code using an atexit function.
from flask import Flask
app = Flask(__name__)
print("start_app_id: ", '{}'.format(id(app)))
import atexit
#atexit.register
def shutdown():
print("AtExit_app_id: ", '{}'.format(id(app)))
#do some cleanup on the app object here
if __name__ == "__main__":
import os
if os.environ.get('WERKZEUG_RUN_MAIN') == "true":
print("reloaded_main_app_id: ", '{}'.format(id(app)))
else:
print("first_main_app_id: ", '{}'.format(id(app)))
app.run(host='0.0.0.0', debug=True)
The output of this code is as follows:
start_app_id: 140521561348864
first_main_app_id: 140521561348864
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
start_app_id: 140105598483312
reloaded_main_app_id: 140105598483312
* Debugger is active!
* Debugger pin code: xxx-xxx-xxx
^CAtExit_app_id: 140521561348864
Note that when first loaded, an app object with ID '864 is created. During the automatic reloading, a new app object with ID '312 is created. Then when I hit Ctrl-C (last line), the atexit routine is called and the original '864 app object is the one that is accessible using the app variable -- not the newer '312 app object.
I want to be able to do cleanup on all app objects floating around when the server closes or is Ctrl-C'd (in this case both '864 and '312). Any recs on how to do this?
Or alternately, if I could just run the cleanup on the newer '312 object created after reloading I could also make that work -- however my current approach only lets me cleanup the original app object.
Thanks.
UPDATE1: I found a link that suggested using try/finally instead of the atexit hook to accomplish what I set out to do above. Switching to this results in exactly the same behavior as atexit and therefore doesn't help with my issue:
from flask import Flask
app = Flask(__name__)
print("start_app_id: ", '{}'.format(id(app)))
if __name__ == "__main__":
import os
if os.environ.get('WERKZEUG_RUN_MAIN') == "true":
print("reloaded_main_app_id: ", '{}'.format(id(app)))
else:
print("first_main_app_id: ", '{}'.format(id(app)))
try:
app.run(host='0.0.0.0', debug=True)
finally:
print("Finally_app_id: ", '{}'.format(id(app)))
#do app cleanup code here
After some digging through the werkzeug source I found the answer. The answer is that it isn't possible to do what I wanted -- and this is by design.
When using the flask dev server (werkzeug) it isn't possible to cleanup all existing app objects upon termination (e.g. ctrl-C) because the werkzeug server catches the keyboardinterrupt exception and "passes" on it. You can see this in the last lines of werkzeug's _reloader.py in the run_with_reloader function:
def run_with_reloader(main_func, extra_files=None, interval=1,
reloader_type='auto'):
"""Run the given function in an independent python interpreter."""
import signal
reloader = reloader_loops[reloader_type](extra_files, interval)
signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
try:
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
t = threading.Thread(target=main_func, args=())
t.setDaemon(True)
t.start()
reloader.run()
else:
sys.exit(reloader.restart_with_reloader())
except KeyboardInterrupt:
pass
If you replace the above "except KeyboardInterrupt:" with "finally:", and then run the second code snippet in the original question, you observe that both of the created app objects are cleaned up as desired. Interestingly, the first code snippet (that uses #atexit) still doesn't work as desired after making these changes.
So in conclusion, you can cleanup all existing app objects when using the flask dev server, but you need to modify the werkzeug source to do so.

Resources