visual_dl.py 4.3 KB
Newer Older
Q
Qiao Longfei 已提交
1 2
import json
import os
S
superjom 已提交
3
import re
Q
Qiao Longfei 已提交
4 5 6
import sys
from optparse import OptionParser

S
superjom 已提交
7 8
from flask import (Flask, Response, redirect, request, send_file,
                   send_from_directory)
Q
Qiao Longfei 已提交
9

S
superjom 已提交
10 11
import lib
import storage
Q
Qiao Longfei 已提交
12 13
import visualdl.mock.data as mock_data
import visualdl.mock.tags as mock_tags
S
superjom 已提交
14
from visualdl.log import logger
S
superjom 已提交
15
import storage
Q
qiaolongfei 已提交
16
import graph
Q
Qiao Longfei 已提交
17 18

app = Flask(__name__, static_url_path="")
S
superjom 已提交
19 20
# set static expires in a short time to reduce browser's memory usage.
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 30
Q
Qiao Longfei 已提交
21 22 23 24 25 26 27 28 29 30 31


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

# 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():
S
superjom 已提交
91
    modes = storage.modes()
S
superjom 已提交
92
    result = gen_result(0, "", lib.get_modes())
Q
Qiao Longfei 已提交
93 94 95 96
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/data/plugin/scalars/tags")
S
superjom 已提交
97 98
def scalar_tags():
    mode = request.args.get('mode')
Q
Qiao Longfei 已提交
99
    is_debug = bool(request.args.get('debug'))
S
debug  
superjom 已提交
100 101 102
    if is_debug:
        result = mock_tags.data()
    else:
S
superjom 已提交
103
        result = lib.get_scalar_tags(storage, mode)
S
superjom 已提交
104
    print 'scalar tags (mode: %s)' % mode, result
S
superjom 已提交
105 106 107 108 109
    result = gen_result(0, "", result)
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/data/plugin/images/tags")
S
superjom 已提交
110
def image_tags():
S
superjom 已提交
111 112 113
    mode = request.args.get('run')
    result = lib.get_image_tags(storage)
    print 'image tags (mode: %s)'%mode, result
S
debug  
superjom 已提交
114
    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
    if is_debug:
S
superjom 已提交
124
        result = mock_data.sequence_data()
S
debug  
superjom 已提交
125
    else:
S
superjom 已提交
126
        result = lib.get_scalar(storage, run, tag)
S
debug  
superjom 已提交
127

S
superjom 已提交
128
    result = gen_result(0, "", result)
Q
Qiao Longfei 已提交
129 130 131
    return Response(json.dumps(result), mimetype='application/json')


S
superjom 已提交
132 133
@app.route('/data/plugin/images/images')
def images():
S
superjom 已提交
134
    mode = request.args.get('run')
S
superjom 已提交
135
    # TODO(ChunweiYan) update this when frontend fix the field name
S
superjom 已提交
136 137
    #tag = request.args.get('tag')
    tag = request.args.get('displayName')
S
superjom 已提交
138

S
superjom 已提交
139
    result = lib.get_image_tag_steps(storage, mode, tag)
S
superjom 已提交
140
    result = gen_result(0, "", result)
S
superjom 已提交
141 142 143 144 145 146

    return Response(json.dumps(result), mimetype='application/json')


@app.route('/data/plugin/images/individualImage')
def individual_image():
S
superjom 已提交
147
    mode = request.args.get('run')
S
superjom 已提交
148
    tag = request.args.get('tag')  # include a index
S
superjom 已提交
149
    step_index = int(request.args.get('index'))  # index of step
S
superjom 已提交
150 151
    offset = 0

S
superjom 已提交
152
    imagefile = lib.get_invididual_image(storage, mode, tag, step_index)
S
superjom 已提交
153 154 155 156
    response = send_file(
        imagefile, as_attachment=True, attachment_filename='img.png')
    return response

S
superjom 已提交
157

Q
qiaolongfei 已提交
158 159 160 161 162
@app.route('/data/plugin/graphs/graph')
def graph():
    model_json = graph.load_model("")
    return Response(model_json, mimetype='application/json')

S
superjom 已提交
163

Q
Qiao Longfei 已提交
164 165
if __name__ == '__main__':
    logger.info(" port=" + str(options.port))
S
superjom 已提交
166
    app.run(debug=True, host=options.host, port=options.port)