-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (43 loc) · 1.8 KB
/
app.py
File metadata and controls
62 lines (43 loc) · 1.8 KB
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
import os
import logging
from flask import (
Flask, url_for, render_template, Response, request
)
from utils.config_helper import ConfigHelper
from utils.echart_data_helper import EchartsDataHelper
app = Flask(__name__)
@app.before_request
def require_login():
config_helper = ConfigHelper()
if config_helper.need_login:
auth = request.authorization
if not auth:
return authenticate()
login_suc = ((auth.username == config_helper.user_name and auth.password == config_helper.password) or
(auth.username == config_helper.temp_user and auth.password == config_helper.temp_user_password))
if not login_suc:
return authenticate()
@app.route('/')
def index():
data_helper = EchartsDataHelper()
return render_template('echarts_less_data.html', json=data_helper.get_echarts_json_data(False))
@app.route('/more_data')
def echarts():
data_helper = EchartsDataHelper()
return render_template('echarts_more_data.html', json=data_helper.get_echarts_json_data(True))
def get_absolute_file_path(filename):
basedir = os.path.abspath(os.path.dirname(__file__))
absolute_file_path = basedir + url_for('static', filename='img/' + filename)
return absolute_file_path
# 使用 errorhandler() 装饰器定制出错页面
@app.errorhandler(404)
def page_not_found(error):
return render_template('page_not_found.html'), 404
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
app.run()
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response("<script>alert('Login Failed:Please enter the correct account and password.');</script>",
401,
{'WWW-Authenticate': 'Basic realm="Please enter the correct account and password."'})