flask not responding with template - python-3.x

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

Related

How do I create an alert based on the last page visited?

I'm trying to mimic the 'dashboard' of https://finance.cs50.net/ (once you've logged in), which displays a 'bought' alert if you are redirected there from /buy, but no alert if you end up there otherwise.
I'm using Flask, jinja2, python, and HTML. The address provided is ("/"), which works fine with return redirect, but not with return render_template. I tried passing in a flag with return redirect, but my computer didn't like that. You always know the UserId, but navigation is not being tracked (i.e. no cookies). Simple answers that I can understand are preferred to clever ones that I cannot.
# from application.py, at the end of the 'buy' function
# flag says local to 'buy' so this does not help me
flag = True
return redirect("/")
## also tried, but computer got angry
return render_template("/")
I don't really know how the alert looks like but it sounds like you might be looking for flash. The documentation is available here
Also using the example provided in the documentation, the handler for '/buy' should look like buy() below:
from flask import Flask, flash, redirect, render_template, \
request, url_for
app = Flask(__name__)
app.secret_key = 'some_secret'
#app.route('/')
def index():
return render_template('index.html')
#app.route('/buy', methods=['GET', 'POST'])
def buy():
if False:
pass
else:
flash('You were redirected from /buy')
return redirect(url_for('index'))
return render_template('buy.html')

Basic HTTP Server not fully working and not throwing errors

I've started to explore HTTP servers in python and i wanted to do something very simple. The idea is just if a client connects to "my ip"/admin it shows his header on the web page, if not it just uses the default do_GET() function.
My code:
#!/usr/bin/env python3
import http.server
import socketserver
class HttpHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/admin":
self.wfile.write("This page is for administrators only".encode())
self.wfile.write(str(self.headers).encode())
else:
http.server.SimpleHTTPRequestHandler.do_GET(self)
http_server=socketserver.TCPServer(("",10002),HttpHandler)
http_server.serve_forever()
For some reason i can't see the headers (unless i do a print of the self.headers to show them in the terminal) it isn't even throwing any errors so i'm kinda lost here.
Thanks for the help
Ok so the solution i came up with (for anyone interested or might friend this thread with the same doubts) is this:
#!/usr/bin/env python3
import http.server
import socketserver
class HttpHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/admin":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html> <head><title> Testing </title> </head><body><p> This page is only for administrators</p>"+str(self.headers)+"</body>", "UTF-8"))
else:
http.server.SimpleHTTPRequestHandler.do_GET(self)
http_server=socketserver.TCPServer(("",10001), HttpHandler)
http_server.serve_forever()

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.

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.

Flask - custom error messages on abort

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

Resources