# -*- coding: utf-8 -*-
"""
Helpers for :epkg:`Flask`.
:githublink:`%|py|6`
"""
import traceback
import threading
from flask import Response
[docs]def Text2Response(text):
"""
convert a text into plain text
:param text: text to convert
:return: textReponse
:githublink:`%|py|17`
"""
return Response(text, mimetype='text/plain')
[docs]def Exception2Response(e):
"""
convert an exception into plain text and display the stack trace
:param e: Exception
:return: textReponse
:githublink:`%|py|27`
"""
text = traceback.format_exc()
return Text2Response("Exception: {0}\nSTACK:\n{1}".format(str(e), text))
[docs]class FlaskInThread(threading.Thread):
"""
Defines a thread for the server.
:githublink:`%|py|36`
"""
[docs] def __init__(self, app, host="localhost", port=8081):
"""
:param app: :epkg:`Flask` application
:githublink:`%|py|41`
"""
threading.Thread.__init__(self)
self._app = app
self._host = host
self._port = port
self.daemon = True
[docs] def run(self):
"""
Starts the server.
:githublink:`%|py|51`
"""
self._app.run(host=self._host, port=self._port)
[docs] def shutdown(self):
"""
Shuts down the server, the function could work if:
* method run keeps a pointer on a server instance
(the one owning method
`serve_forever <https://docs.python.org/3/library/socketserver.html#socketserver.BaseServer.serve_forever>`_)
* module `werkzeug <http://werkzeug.pocoo.org/>`_
returns this instance in function
`serving.run_simple <https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py>`_
* module `Flask <http://flask.pocoo.org/>`_
returns this instance in method
`app.Flask.run <https://github.com/mitsuhiko/flask/blob/master/flask/app.py>`_
:githublink:`%|py|67`
"""
raise NotImplementedError()
# self.server.shutdown()
# self.server.server_close()