-
Notifications
You must be signed in to change notification settings - Fork 4
/
flask_demo.py
98 lines (78 loc) · 2.14 KB
/
flask_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# coding: utf-8
"""
Demo for the live reloading of Flask server.
"""
from __future__ import absolute_import
# Standard imports
import os
import sys
# External imports
import flask
def main():
"""
Main function.
:return:
None.
"""
try:
# Get the `src` directory's absolute path
src_path = os.path.dirname(
# `aoiklivereload` directory's absolute path
os.path.dirname(
# `demo` directory's absolute path
os.path.dirname(
# This file's absolute path
os.path.abspath(__file__)
)
)
)
# If the `src` directory path is not in `sys.path`
if src_path not in sys.path:
# Add to `sys.path`.
#
# This aims to save user setting PYTHONPATH when running this demo.
#
sys.path.append(src_path)
# Import reloader class
from aoiklivereload import LiveReloader
# Create reloader
reloader = LiveReloader()
# Start watcher thread
reloader.start_watcher_thread()
# Server host
server_host = '0.0.0.0'
# Server port
server_port = 8000
# Get message
msg = '# ----- Run server -----\nHost: {}\nPort: {}'.format(
server_host, server_port
)
# Print message
print(msg)
# Create Flask app
flask_app = flask.Flask(__name__)
# Create request handler
@flask_app.route('/')
def hello_handler(): # pylint: disable=unused-variable
"""
Request handler.
:return:
Response body.
"""
# Return response body
return 'hello'
# Run server
flask_app.run(
host=server_host,
port=server_port,
# Disable Flask's reloader
debug=False,
)
# If have `KeyboardInterrupt`
except KeyboardInterrupt:
# Not treat as error
pass
# If is run as main module
if __name__ == '__main__':
# Call main function
exit(main())