コード例 #1
0
@app.route('/cdn/<path:filename>')
def custom_static(filename):
    return send_from_directory("/" + osp.dirname(filename),
                               osp.basename(filename))


@app.route('/')
def root():
    return redirect(url_for("index"))


@app.route('/index')
def index():
    resp = make_response(render_template("index.html"))
    return resp


@app.route("/show_nodestates")
def show_workers():
    node_list = []
    resp = make_response(
        render_template("show_nodestates.html", worker_list=node_list))
    return resp


uiapp = FlaskUI(app, host="localhost", port=5001)
uiapp.browser_thread = Thread(target=partial(
    open_browser_func, uiapp, localhost='http://127.0.0.1:{}/'.format(5001)))

uiapp.run()
コード例 #2
0
def create_guiservice(db_url: str, dispatch_work_func: callable, port: int) -> Tuple[FlaskUI, P2PFlaskApp]:
    """
    This factory method creates the FlaskUI and P2PFlaskApp. In order to start the application, the method run of
    FlaskUI object must be called. The P2PFlaskApp object can be used to add additional blueprints.

    Args:
        db_url: url to a database
        dispatch_work_func: function that receives a string as argument. This argument is a video file path. The function
            should do something with that file
        port: port for the GUI service

    Returns:
        FlaskUI object
        P2PFlaskApp object
    """
    app = P2PFlaskApp(__name__, template_folder=osp.join(osp.dirname(__file__), 'templates'),
                static_folder=osp.join(osp.dirname(__file__), 'templates', 'assets'))

    Bootstrap(app)

    @app.route('/check_status')
    @ignore_remote_addresses
    def check_status():
        session = create_session(db_url)
        VideoStatuses.check_and_download(session)
        # VideoStatuses.remove_dead_requests(session)
        # TODO call remove_dead_requests or insteand of removing, just restart them
        query = VideoStatuses.get_video_statuses(session)
        session.close()
        video_items = []

        #TODO also render on HTML the time of execution if it exists
        # TODO why the hell I am putting the query results in a list???? I should pass directly the query
        for item in query:
            video_items.append({'filename': item.file_path,
                                'status': 'ready' if item.results_path is not None else 'processing',
                                'progress': item.progress,
                                'time_of_request': item.time_of_request.strftime(
                                    "%m/%d/%Y, %H:%M:%S") if item.time_of_request is not None else 'none'})
        partial_destination_url = '/show_video?filename='
        resp = make_response(render_template("check_status.html", partial_destination_url=partial_destination_url,
                                             video_items=video_items))
        return resp

    @app.route('/file_select')
    @ignore_remote_addresses
    def file_select():
        fname = gui_select_file()
        if fname != '':
            dispatch_work_func(fname)
            #TODO probably because the redirect is faster than the upload, the check_status will delete the request from db
            # as it will consider it a dead request, thus I need to syncronize somewhere, or wait
            return redirect(url_for("check_status"))
        else:
            return redirect(url_for("index"))

    @app.route('/show_video')
    @ignore_remote_addresses
    def show_video():
        session = create_session(db_url)
        item = VideoStatuses.find_results_path(session, request.args.get('filename'))
        session.close()

        plots_gen = html_imgs_generator(item.file_path, item.results_path)

        try:
            first_image = next(plots_gen)
            resp = make_response(render_template("show_video.html", first_image_str=first_image, images=plots_gen))
        except StopIteration:
            resp = make_response(render_template("show_video.html", first_image_str='', images=[]))
        return resp

    @app.route("/show_nodestates")
    @ignore_remote_addresses
    def show_workers():
        node_list = get_available_nodes(local_port=port)
        resp = make_response(render_template("show_nodestates.html", worker_list=node_list))
        return resp

    @app.route('/')
    @ignore_remote_addresses
    def root():
        return redirect(url_for("index"))

    @app.route('/index')
    @ignore_remote_addresses
    def index():
        resp = make_response(render_template("index.html"))
        return resp

    uiapp = FlaskUI(app, host="0.0.0.0", port=port)
    uiapp.browser_thread = Thread(target=partial(open_browser_func, uiapp, localhost='http://127.0.0.1:{}/'.format(port)))

    return uiapp, app