This question already has answers here:
Flask application traceback doesn't show up in server log
(4 answers)
Closed 5 years ago.
I have a server written on flask and I need to do error tracking (name of error, what caused the error, name of file and line with error).
How should I do it?
I'll appreciate any help.
enable the flask debugger, by setting debug to True for your flask app.
app = Flask(__name__)
app.config['DEBUG'] = True
As you shouldn't enable debug for a production server, you can create a logging
handler as follows: http://flask.pocoo.org/docs/0.12/errorhandling/
Related
This question already has answers here:
How to run a flask application?
(6 answers)
Closed 2 years ago.
I am following the flask quickstart to create a python server so that I can send data from postman to the server. However, when I run the code using "python main.py", nothing happens in the command line. What am I doing wrong?
Code:
from flask import Flask, request
from flask_restful import Resource, Api
from server import analyzeWeaknesses
app = Flask(__name__)
api = Api(app)
#app.route('/analyzeWeaknesses', methods=['POST'])
def WeaknessAnalysis():
if request.method == 'POST':
analyzeWeaknesses(request.data)
return {"status":"success"}
else:
error = 'Unable to access data!'
analyzeWeaknesses simply takes in an input and does an analysis of it and outputs integers. I would like this output to be also returned to postman. Desired outcome Postman to for python input and then receive its output
You're missing the main function. Add this at the bottom of your code-
if __name__ == '__main__':
app.run()
This question already has answers here:
How to run a flask application?
(6 answers)
Configure Flask dev server to be visible across the network
(17 answers)
Closed 2 years ago.
I'm trying to run my first flask site, but it just brings up the "This site can't be reached" message. What am I doing wrong?
Code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def welcome():
return f"<h1>Welcome!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)
Replace host='0.0.0.0' with your IP Address or you can just remove it.
Just return localhost for your development needs, even it comes to production it's okay because you could connect the app with Nginx/Apache.
This question already has answers here:
Display the contents of a log file as it is updated
(3 answers)
Closed 3 years ago.
I have script which executes for 10 minutes and generates logs/outputs while executing..i want these logs to be passed to html file using flask.
You can use ajax methods in your javascript code to ask server for new logs/outputs with any period.
You can open WebSocket and write your logs to client in real-time. Look at example on flask-socketIO documentation: https://flask-socketio.readthedocs.io/en/latest/ or somewhere else.
Here is example how to send message to client:
from flask import Flask, render_template
from flask_socketio import SocketIO, send
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
#socketio.on('message')
def handle_message(message):
send(message)
if __name__ == '__main__':
socketio.run(app)
This question already has an answer here:
Why isn't Flask giving me an interactive debugger?
(1 answer)
Closed 4 years ago.
I'm trying to enable debug mode in browser as follows:
from werkzeug.debug import DebuggedApplication
application = Flask(__name__)
app = application
application = DebuggedApplication(app, True)
But it does not enable debug mode in my production server Apache under mod_wsgi.
Did I have something wrong? Also the export method doesn't work either.
The DebuggedApplication() middleware only works if an uncaught exception reaches it.
Flask, in production mode, catches all exceptions so these never reach the middleware.
You need to explicitly tell Flask to not catch all exceptions with:
PROPAGATE_EXCEPTIONS = True
in your configuration. From the relevant documenation:
PROPAGATE_EXCEPTIONS
Exceptions are re-raised rather than being handled by the app’s error handlers. If not set, this is implicitly true if TESTING or DEBUG is enabled.
You can set it on the app object before wrapping it in the DebuggedApplication middleware:
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
Note that this only tells Flask to not catch all exceptions any more. You may also want to set other configuration options such as PRESERVE_CONTEXT_ON_EXCEPTION, or you could just enable debug mode with app.debug = True.
This question already has answers here:
Why does running the Flask dev server run itself twice?
(7 answers)
Closed 4 years ago.
I'm currently testing a socket io app and I'm noticing unusual behavior.
In my init python script I'm declaring a function that instantiates a flask object and initializes it via a socketIO instance (which is global) i.e. (assuming all of these files are within the same directory scope):
#/test_app/configs/__init__.py
__socketIO__ = SocketIO()
def create_app(address, port):
app = Flask(__name__, static_url_path='')
app.config.from_pyfile('config.py')
__socketIO__.init_app(app)
return app
in another python script:
#/test_app/run_app.py
from configs import create_app
from configs import __socketIO__ as launch_socket
ip_address = '0, 0, 0, 0'
port = 5000
APP = create_app(ip_address, port)
if __name__ == '__main__':
print 'launching....'
launch_socket.run(APP, debug=True, host=ip_addres, port=port)
Given this, if I run run_app.py, 'launching...' will print twice.
Is this behavior typical? Granted I'm still learning my way around socket.io so perhaps I'm missing some point to it. It just seems odd that it initializes twice. Is there a way to circumvent this behavior if it's unnecessary or detrimental?
Thanks!
This is normal when you run in debug mode. Flask launches two processes, one for watching the code for changes and one for the actual flask process.