Exemplo n.º 1
0
def landing():
    """Landing page for the application

    If the request is `GET`, render the landing page.  If
    `POST`, then store the visualization in the session and
    render the visualization settings page (if applicable) or
    render file selection.

    """
    if request.method == 'POST':
        session['vis_name'] = request.form.get('choice')
        vis = get_visualizations()[session['vis_name']]
        if vis.ALLOWED_SETTINGS:
            return visualization_settings()
        return select_files()

    # otherwise, on GET request
    visualizations = get_visualizations()
    vis_desc = [{'name': vis,
                 'description': visualizations[vis].DESCRIPTION}
                for vis in visualizations]
    session.clear()
    return render_template('select_visualization.html',
                           app_state=get_app_state(),
                           visualizations=sorted(vis_desc,
                                                 key=itemgetter('name')))
Exemplo n.º 2
0
def landing():
    """Landing page for the application

    If the request is `GET`, render the landing page.  If
    `POST`, then store the visualization in the session and
    render the visualization settings page (if applicable) or
    render file selection.

    """
    if request.method == 'POST':
        session['vis_name'] = request.form.get('choice')
        vis = get_visualizations()[session['vis_name']]
        if vis.ALLOWED_SETTINGS:
            return visualization_settings()
        return select_files()

    # otherwise, on GET request
    visualizations = get_visualizations()
    vis_desc = [{'name': vis,
                 'description': visualizations[vis].DESCRIPTION}
                for vis in visualizations]
    session.clear()
    return render_template('select_visualization.html',
                           app_state=get_app_state(),
                           visualizations=sorted(vis_desc,
                                                 key=itemgetter('name')))
Exemplo n.º 3
0
def visualization_settings():
    """Visualization settings page

    Will only render if the visualization object has a non-null
    `ALLOWED_SETTINGS` attribute.

    """
    if request.method == 'POST':
        vis = get_visualizations()[session['vis_name']]
        return render_template('settings.html',
                               app_state=get_app_state(),
                               current_vis=session['vis_name'],
                               settings=vis.ALLOWED_SETTINGS)
Exemplo n.º 4
0
def visualization_settings():
    """Visualization settings page

    Will only render if the visualization object has a non-null
    `ALLOWED_SETTINGS` attribute.

    """
    if request.method == 'POST':
        vis = get_visualizations()[session['vis_name']]
        return render_template('settings.html',
                               app_state=get_app_state(),
                               current_vis=session['vis_name'],
                               settings=vis.ALLOWED_SETTINGS)
Exemplo n.º 5
0
def app_state():
    state = get_app_state()
    return jsonify(state)
Exemplo n.º 6
0
def not_found_error(e):
    return render_template('404.html', app_state=get_app_state()), 404
Exemplo n.º 7
0
def internal_server_error(e):
    return render_template('500.html', app_state=get_app_state()), 500
Exemplo n.º 8
0
def select_files():
    """File selection and final display of visualization

    If the request contains no files, then render the file
    selection page.  Otherwise render the visualization.

    Todo:
        Logically, this route should be split into `select_files`
        and `result`.

    """
    if 'file[]' in request.files:
        vis = get_visualizations()[session['vis_name']]
        inputs = []
        for file_obj in request.files.getlist('file[]'):
            entry = {}
            entry.update({'filename': file_obj.filename})
            # Why is this necessary? Unsure why Flask sometimes
            # sends the files as bytestreams vs. strings.
            try:
                entry.update({'data':
                    Image.open(
                        io.BytesIO(file_obj.stream.getvalue())
                    )})
            except AttributeError:
                entry.update({'data':
                    Image.open(
                        io.BytesIO(file_obj.stream.read())
                    )})
            inputs.append(entry)

        start_time = time.time()
        if 'settings' in session:
            vis.update_settings(session['settings'])
        output = vis.make_visualization(inputs,
                                        output_dir=session['img_output_dir'])
        duration = '{:.2f}'.format(time.time() - start_time, 2)

        for i, file_obj in enumerate(request.files.getlist('file[]')):
            output[i].update({'filename': file_obj.filename})

        for entry in inputs:
            path = os.path.join(session['img_input_dir'], entry['filename'])
            entry['data'].save(path, 'PNG')

        kwargs = {}
        if vis.REFERENCE_LINK:
            kwargs['reference_link'] = vis.REFERENCE_LINK

        return render_template('{}.html'.format(session['vis_name']),
                               inputs=inputs,
                               results=output,
                               current_vis=session['vis_name'],
                               app_state=get_app_state(),
                               duration=duration,
                               **kwargs)

    # otherwise, if no files in request
    session['settings'] = request.form.to_dict()
    if 'choice' in session['settings']:
        session['settings'].pop('choice')
    return render_template('select_files.html',
                           app_state=get_app_state(),
                           current_vis=session['vis_name'],
                           settings=session['settings'])
Exemplo n.º 9
0
def not_found_error(e):
    return render_template('404.html', app_state=get_app_state()), 404
Exemplo n.º 10
0
def internal_server_error(e):
    return render_template('500.html', app_state=get_app_state()), 500
Exemplo n.º 11
0
def select_files():
    """File selection and final display of visualization

    If the request contains no files, then render the file
    selection page.  Otherwise render the visualization.

    Todo:
        Logically, this route should be split into `select_files`
        and `result`.

    """
    if 'file[]' in request.files:
        vis = get_visualizations()[session['vis_name']]
        inputs = []
        for file_obj in request.files.getlist('file[]'):
            entry = {}
            entry.update({'filename': file_obj.filename})
            # Why is this necessary? Unsure why Flask sometimes
            # sends the files as bytestreams vs. strings.
            try:
                entry.update({'data':
                              Image.open(
                                  io.BytesIO(file_obj.stream.getvalue())
                              )})
            except AttributeError:
                entry.update({'data':
                              Image.open(
                                  io.BytesIO(file_obj.stream.read())
                              )})
            inputs.append(entry)

        start_time = time.time()
        if 'settings' in session:
            vis.update_settings(session['settings'])
        output = vis.make_visualization(inputs,
                                        output_dir=session['img_output_dir'])
        duration = '{:.2f}'.format(time.time() - start_time, 2)

        for i, file_obj in enumerate(request.files.getlist('file[]')):
            output[i].update({'filename': file_obj.filename})

        for entry in inputs:
            path = os.path.join(session['img_input_dir'], entry['filename'])
            entry['data'].save(path, 'PNG')

        kwargs = {}
        if vis.REFERENCE_LINK:
            kwargs['reference_link'] = vis.REFERENCE_LINK

        return render_template('{}.html'.format(session['vis_name']),
                               inputs=inputs,
                               results=output,
                               current_vis=session['vis_name'],
                               app_state=get_app_state(),
                               duration=duration,
                               **kwargs)

    # otherwise, if no files in request
    session['settings'] = request.form.to_dict()
    if 'choice' in session['settings']:
        session['settings'].pop('choice')
    return render_template('select_files.html',
                           app_state=get_app_state(),
                           current_vis=session['vis_name'],
                           settings=session['settings'])