Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Defines a simple web site in Flask which allows unit testing
5"""
6import logging
7from flask import Flask, request
8from .flask_helper import Text2Response, Exception2Response
11def create_application():
12 """
13 Creates a :epkg:`Flask` application.
14 """
15 log = logging.getLogger('werkzeug')
16 log.setLevel(logging.ERROR)
17 app = Flask(__name__)
19 @app.route('/shutdown/', methods=['POST'])
20 def shutdown(): # pylint: disable=W0612
21 """
22 shuts down the service
23 """
24 shutdown_server()
25 return Text2Response('Server shutting down...')
27 @app.route('/help/<path:command>')
28 def help_command(command): # pylint: disable=W0612
29 """
30 return a very basic help message on command command
32 @param command command
33 @return help
34 """
35 try:
36 if command is None or command == "exception":
37 raise Exception("no help for command: {0}".format(command))
38 return Text2Response("help for command: {0}".format(command))
39 except Exception as e:
40 return Exception2Response(e)
42 @app.route('/form/', methods=['POSTS'])
43 def form(): # pylint: disable=W0612
44 """
45 process a form
46 """
47 try:
48 rows = []
49 for k, v in request.form.to.dict().items():
50 rows.append("{0}={1}".format(k, v))
51 return Text2Response("\n".join(rows))
52 except Exception as e:
53 return Exception2Response(e)
55 @app.route('/')
56 def main_page(): # pylint: disable=W0612
57 """
58 defines the main page
59 """
60 message = """Simple Flask Site
61 / help on command
62 /help/<command> help on command command
63 /upload/ upload a file (use post)
64 /shutdown/ shutdown the server (for unit test)
65 /form/ process a form
66 """.replace(" ", "")
67 return Text2Response(message)
69 return app
72def shutdown_server():
73 """
74 to shutdown the service
75 """
76 func = request.environ.get('werkzeug.server.shutdown')
77 if func is None:
78 raise RuntimeError('Not running with the Werkzeug Server')
79 func()