#!/user/bin/env python import json import os import sys from optparse import OptionParser from flask import (Flask, Response, redirect, request, send_file, send_from_directory) import visualdl import visualdl.server import visualdl.server.graph as vdl_graph from visualdl.server import lib from visualdl.server.log import logger from visualdl.server.mock import data as mock_data from visualdl.server.mock import data as mock_tags from visualdl.python.storage import (LogWriter, LogReader) app = Flask(__name__, static_url_path="") # set static expires in a short time to reduce browser's memory usage. app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 30 SERVER_DIR = os.path.join(visualdl.ROOT, 'server') def option_parser(): """ :return: """ parser = OptionParser(usage="usage: visualDL -p port [options]") parser.add_option( "-p", "--port", type=int, default=8040, action="store", dest="port", help="api service port") parser.add_option( "-t", "--host", type=str, default="0.0.0.0", action="store", help="api service ip") parser.add_option( "-m", "--model_pb", type=str, action="store", help="model proto in ONNX format") 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 = os.path.join(SERVER_DIR, "./dist") mock_data_path = os.path.join(SERVER_DIR, "./mock_data/") log_reader = LogReader(options.logdir) graph_image_path = None # 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/') def serve_static(filename): return send_from_directory( os.path.join(server_path, static_file_path), filename) @app.route('/graphs/image') def serve_graph(): print 'send file', graph_image_path return send_file(graph_image_path) @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(): result = gen_result(0, "", lib.get_modes(log_reader)) return Response(json.dumps(result), mimetype='application/json') @app.route("/data/plugin/scalars/tags") def scalar_tags(): mode = request.args.get('mode') is_debug = bool(request.args.get('debug')) if is_debug: result = mock_tags.data() else: result = lib.get_scalar_tags(log_reader) result = gen_result(0, "", result) return Response(json.dumps(result), mimetype='application/json') @app.route("/data/plugin/images/tags") def image_tags(): mode = request.args.get('run') result = lib.get_image_tags(log_reader) result = gen_result(0, "", result) return Response(json.dumps(result), mimetype='application/json') @app.route("/data/plugin/histograms/tags") def histogram_tags(): mode = request.args.get('run') result = lib.get_histogram_tags(log_reader) result = gen_result(0, "", result) 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')) if is_debug: result = mock_data.sequence_data() else: result = lib.get_scalar(log_reader, run, tag) result = gen_result(0, "", result) return Response(json.dumps(result), mimetype='application/json') @app.route('/data/plugin/images/images') def images(): mode = request.args.get('run') tag = request.args.get('tag') result = lib.get_image_tag_steps(log_reader, mode, tag) result = gen_result(0, "", result) return Response(json.dumps(result), mimetype='application/json') @app.route('/data/plugin/images/individualImage') def individual_image(): mode = request.args.get('run') tag = request.args.get('tag') # include a index step_index = int(request.args.get('index')) # index of step offset = 0 imagefile = lib.get_invididual_image(log_reader, mode, tag, step_index) response = send_file( imagefile, as_attachment=True, attachment_filename='img.png') return response @app.route('/data/plugin/histograms/histograms') def histogram(): run = request.args.get('run') tag = request.args.get('tag') result = lib.get_histogram(log_reader, run, tag) result = gen_result(0, "", result) return Response(json.dumps(result), mimetype='application/json') @app.route('/data/plugin/graphs/graph') def graph(): # TODO(ChunweiYan) need to add a config for whether have graph. if graph_image_path is None: data = {'url': ''} else: data = {'url': '/graphs/image'} result = gen_result(0, "", data) return Response(json.dumps(result), mimetype='application/json') if __name__ == '__main__': logger.info(" port=" + str(options.port)) if not options.logdir: logger.error("should pass in logdir option") sys.exit(-1) if options.model_pb: # draw graph image_dir = os.path.join(options.logdir, "graphs") if not os.path.isdir(image_dir): os.mkdir(image_dir) graph_image_path = vdl_graph.draw_graph(options.model_pb, image_dir) app.run(debug=False, host=options.host, port=options.port)