-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split interface into frontend and rest modules (#34)
* factor helper functions into own class for better modularity When refactoring the main app into different flask blueprints I will have to have access to these methods from a central location. This static helper class will enable me to do this. The name is still quite stupid. I will have to think about it in more detail. * refactor rest api into a flask blueprint This allows us to better separate logic from front-end in the future and easier extend the API and web front-end.
- Loading branch information
Showing
6 changed files
with
216 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Flask blueprint for accessing and manipulating image ressources | ||
This is used by the main flask application to provide a REST API. | ||
""" | ||
|
||
import os | ||
import shutil | ||
from PIL import Image | ||
from werkzeug.utils import secure_filename | ||
from flask import ( | ||
Blueprint, | ||
current_app, | ||
jsonify, | ||
session, | ||
request | ||
) | ||
from picasso import __version__ | ||
from picasso.utils import get_visualizations | ||
|
||
API = Blueprint('api', __name__) | ||
|
||
|
||
@API.route('/', methods=['GET']) | ||
def root(): | ||
"""The root of the REST API | ||
displays a hello world message. | ||
""" | ||
return jsonify(message='Picasso {version}. ' | ||
'See API documentation at: ' | ||
'https://picasso.readthedocs.io/en/latest/api.html' | ||
.format(version=__version__), | ||
version=__version__) | ||
|
||
|
||
@API.route('/images', methods=['POST', 'GET']) | ||
def images(): | ||
"""Upload images via REST interface | ||
Check if file upload was successful and sanatize user input. | ||
TODO: return file URL instead of filename | ||
""" | ||
if request.method == 'POST': | ||
file_upload = request.files['file'] | ||
if file_upload: | ||
image = {} | ||
image['filename'] = secure_filename(file_upload.filename) | ||
full_path = os.path.join(session['img_input_dir'], | ||
image['filename']) | ||
file_upload.save(full_path) | ||
image['uid'] = session['image_uid_counter'] | ||
session['image_uid_counter'] += 1 | ||
current_app.logger.debug('File %d is saved as %s', | ||
image['uid'], | ||
image['filename']) | ||
session['image_list'].append(image) | ||
return jsonify(ok="true", file=image['filename'], uid=image['uid']) | ||
return jsonify(ok="false") | ||
if request.method == 'GET': | ||
return jsonify(images=session['image_list']) | ||
|
||
|
||
@API.route('/visualize', methods=['GET']) | ||
def visualize(): | ||
"""Trigger a visualization via the REST API | ||
Takes a single image and generates the visualization data, returning the | ||
output exactly as given by the target visualization. | ||
""" | ||
|
||
session['settings'] = {} | ||
image_uid = request.args.get('image') | ||
vis_name = request.args.get('visualizer') | ||
vis = get_visualizations()[vis_name] | ||
if hasattr(vis, 'settings'): | ||
for key in vis.settings.keys(): | ||
if request.args.get(key) is not None: | ||
session['settings'][key] = request.args.get(key) | ||
else: | ||
session['settings'][key] = vis.settings[key][0] | ||
inputs = [] | ||
for image in session['image_list']: | ||
if image['uid'] == int(image_uid): | ||
full_path = os.path.join(session['img_input_dir'], | ||
image['filename']) | ||
entry = {} | ||
entry['filename'] = image['filename'] | ||
entry['data'] = Image.open(full_path) | ||
inputs.append(entry) | ||
if 'settings' in session: | ||
vis.update_settings(session['settings']) | ||
output = vis.make_visualization( | ||
inputs, output_dir=session['img_output_dir']) | ||
return jsonify(output=output) | ||
|
||
|
||
@API.route('/reset', methods=['GET']) | ||
def reset(): | ||
"""Delete the session and clear temporary directories | ||
""" | ||
shutil.rmtree(session['img_input_dir']) | ||
shutil.rmtree(session['img_output_dir']) | ||
session.clear() | ||
return jsonify(ok='true') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.