I currently have a flask project deployed on IIS 11 using FastCGI and I was wondering if there is a way for me to print to the web console within the flask app.
1 Answer
Depends on when you need to print the message to the console. If it's live messaging like notifications / updates as they happen you'd need a websocket that creates an event on the Flask side that is picked up by Websockets on the javascript side that calls a console.log()
If it's a 1 time message once the page loads, just feed the message to the render_template() funciton:
render_template('index.html', message=message) You will then have that available in the template and then you can console.log(message) from the client side
6 Comments
ryan_thomp
Good idea I will try this out. I was thinking something similar with the live messages and the websockets, I have done something similar in the past.
ryan_thomp
However, would it be possible to feed the python runtime errors to the console using the websocket method?
Nick Dima
@ryan_thomp Im thinking a custom event for errors. On javascript side "socketio.on('error', function(data){console.log(data)})". On flask side have your try/except blocks emit a message on the except: "except Exception as e: socketio.emit('error', {'error': e})"
ryan_thomp
I did what you said however I see nothing within the developer console. Would you like me to make an edit to my main post to see what I did wrong
Nick Dima
@ryan_thomp that works! I'll take a look once changed
|