visual_dl.py 3.6 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
""" entry point of visual_dl
"""
import json
import os
import sys
from optparse import OptionParser

from flask import Flask, redirect
from flask import request
from flask import send_from_directory
from flask import Response

from visualdl.log import logger
import visualdl.mock.data as mock_data
import visualdl.mock.tags as mock_tags
S
superjom 已提交
16
import storage
Q
qiaolongfei 已提交
17
import graph
Q
Qiao Longfei 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31

app = Flask(__name__, static_url_path="")


def option_parser():
    """

    :return:
    """
    parser = OptionParser(usage="usage: visual_dl visual_dl.py "\
                          "-p port [options]")
    parser.add_option(
        "-p",
        "--port",
Q
Qiao Longfei 已提交
32
        type=int,
Q
Qiao Longfei 已提交
33 34 35
        default=8040,
        action="store",
        dest="port",
S
debug  
superjom 已提交
36 37 38 39 40 41 42 43
        help="api service port")
    parser.add_option(
        "-t",
        "--host",
        type=str,
        default="0.0.0.0",
        action="store",
        help="api service ip")
Q
Qiao Longfei 已提交
44 45 46 47 48 49 50 51 52 53
    parser.add_option(
        "--logdir", action="store", dest="logdir", help="log file directory")
    return parser.parse_args()


options, args = option_parser()
server_path = os.path.abspath(os.path.dirname(sys.argv[0]))
static_file_path = "./frontend/dist/"
mock_data_path = "./mock_data/"

S
superjom 已提交
54
storage = storage.StorageReader(options.logdir)
S
debug  
superjom 已提交
55

Q
Qiao Longfei 已提交
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

# return data
# status, msg, data
def gen_result(status, msg, data):
    """
    :param status:
    :param msg:
    :return:
    """
    result = dict()
    result['status'] = status
    result['msg'] = msg
    result['data'] = data
    return result


@app.route("/")
def index():
    return redirect('/static/index.html', code=302)


@app.route('/static/<path:filename>')
def serve_static(filename):
    return send_from_directory(
        os.path.join(server_path, static_file_path), filename)


@app.route('/data/logdir')
def logdir():
    result = gen_result(0, "", {"logdir": options.logdir})
    return Response(json.dumps(result), mimetype='application/json')


@app.route('/data/runs')
def runs():
    is_debug = bool(request.args.get('debug'))
    result = gen_result(0, "", ["train", "test"])
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/data/plugin/scalars/tags")
def tags():
    is_debug = bool(request.args.get('debug'))
S
debug  
superjom 已提交
99 100 101 102
    tag = request.args.get('tag')
    if is_debug:
        result = mock_tags.data()
    else:
S
superjom 已提交
103 104 105 106 107 108 109 110 111 112
        result = {}
        print 'modes', storage.modes()
        for mode in storage.modes():
            result[mode] = {}
            reader = storage.as_mode(mode)
            for tag in reader.tags("scalar"):
                result[mode][tag] = {
                    'displayName': reader.scalar(tag).caption(),
                    'description': ""
                }
S
debug  
superjom 已提交
113 114
    print 'tags', result
    result = gen_result(0, "", result)
Q
Qiao Longfei 已提交
115 116 117 118 119 120 121 122
    return Response(json.dumps(result), mimetype='application/json')


@app.route('/data/plugin/scalars/scalars')
def scalars():
    run = request.args.get('run')
    tag = request.args.get('tag')
    is_debug = bool(request.args.get('debug'))
S
debug  
superjom 已提交
123 124 125
    if is_debug:
        result = gen_result(0, "", mock_data.sequence_data())
    else:
S
superjom 已提交
126 127 128 129 130 131 132 133
        reader = storage.as_mode(run)
        scalar = reader.scalar(tag)

        records = scalar.records()
        ids = scalar.ids()
        timestamps = scalar.timestamps()

        result = zip(timestamps, ids, records)
S
debug  
superjom 已提交
134 135
        result = gen_result(0, "", result)

Q
Qiao Longfei 已提交
136 137 138
    return Response(json.dumps(result), mimetype='application/json')


Q
qiaolongfei 已提交
139 140 141 142 143 144
@app.route('/data/plugin/graphs/graph')
def graph():
    model_json = graph.load_model("")
    return Response(model_json, mimetype='application/json')


Q
Qiao Longfei 已提交
145 146
if __name__ == '__main__':
    logger.info(" port=" + str(options.port))
S
debug  
superjom 已提交
147
    app.run(debug=False, host=options.host, port=options.port)