visual_dl.py 1.5 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3
""" entry point of visual_dl
"""
import json
Q
qiaolongfei 已提交
4
import os
Q
qiaolongfei 已提交
5
import sys
Q
qiaolongfei 已提交
6 7
from optparse import OptionParser

Q
qiaolongfei 已提交
8 9
from flask import Flask, redirect
from flask import send_from_directory
Q
qiaolongfei 已提交
10

Q
qiaolongfei 已提交
11
from visualdl.log import logger
Q
qiaolongfei 已提交
12

Q
qiaolongfei 已提交
13
app = Flask(__name__, static_url_path="")
Q
qiaolongfei 已提交
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


def option_parser():
    """

    :return:
    """
    parser = OptionParser(usage="usage: visual_dl visual_dl.py "\
                          "-p port [options]")
    parser.add_option(
        "-p",
        "--port",
        default=8040,
        action="store",
        dest="port",
        help="rest api service port")
    return parser.parse_args()


# return data
# status, msg, data
def gen_result(status, msg):
    """

    :param status:
    :param msg:
    :return:
    """
    result = dict()
    result['status'] = status
    result['msg'] = msg
    result['data'] = {}
    return result

Q
qiaolongfei 已提交
48

Q
qiaolongfei 已提交
49 50 51 52 53
server_path = os.path.abspath(os.path.dirname(sys.argv[0]))
static_file_path = "../visualdl/frontend/dist/"


@app.route('/static/<path:filename>')
Q
qiaolongfei 已提交
54
def serve_static(filename):
Q
qiaolongfei 已提交
55
    return send_from_directory(os.path.join(server_path, static_file_path), filename)
Q
qiaolongfei 已提交
56

Q
qiaolongfei 已提交
57 58

@app.route("/")
Q
qiaolongfei 已提交
59
def index():
Q
qiaolongfei 已提交
60
    return redirect('/static/index.html', code=302)
Q
qiaolongfei 已提交
61

Q
qiaolongfei 已提交
62 63 64

@app.route('/hello')
def hello():
Q
qiaolongfei 已提交
65 66 67 68 69 70 71 72
    result = gen_result(0, "Hello, this is VisualDL!")
    return json.dumps(result)


if __name__ == '__main__':
    options, args = option_parser()
    logger.info(" port=" + str(options.port))
    app.run(debug=False, host="0.0.0.0", port=options.port)