diff --git a/pyKADMOS/sample/graph.py b/pyKADMOS/sample/graph.py index acaf4290a434dc426fd9229925a41028fed511b3..f977b72113226bdcbc689ec9f88e73e3a7864ed0 100644 --- a/pyKADMOS/sample/graph.py +++ b/pyKADMOS/sample/graph.py @@ -4487,6 +4487,16 @@ def cmdows_integrity_check(graph, MPG=None): return result + +def load_from_kdms(filename, source_folder=None, kadmos_check_critical=True): + # TODO: Make general open class that included assertions and kadmos_check_critical + return open_kdms(filename, source_folder) + + +def load_from_graphml(filename, source_folder=None, kadmos_check_critical=True): + return import_graphml(filename, source_folder) + + def open_kdms(filename, source_folder=None): """ Method to open any KadmosGraph graph object using pickling. diff --git a/pyKADMOS/sample/interface.py b/pyKADMOS/sample/interface.py index b7522545e04c398474d7eb9942a261d04ada1f51..7ac36ba91a93792141e72fe468e3bc7f1653c0f2 100644 --- a/pyKADMOS/sample/interface.py +++ b/pyKADMOS/sample/interface.py @@ -1,9 +1,11 @@ import logging import tempfile import os -from flask import Flask, request, redirect, url_for, send_file -from werkzeug import secure_filename -from graph import load_from_cmdows +import shutil + +from flask import Flask, request, redirect, url_for, send_file, render_template, send_from_directory + +from graph import * # Initial settings @@ -14,79 +16,86 @@ temp = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../temp/') tempfile.tempdir = temp -# Script -def allowed_file(filename): - return '.' in filename and \ - filename.rsplit('.', 1)[1] in set(['xml']) +# Initial variables +file_types = ['cmdows', 'kdms', 'graphml'] +file_extensions = ['xml', 'kdms', 'graphml'] # Index -@app.route("/", methods=['GET', 'POST']) +@app.route("/") +def index(message=None): + # Check if message is send + if request.values.get('message', False) and message is None: + message = request.values['message'] + # Render index + return render_template('index.html', types=file_types, message=message) + + +# Upload +@app.route("/", methods=['POST']) def upload(): - # If file is submitted - if request.method == 'POST': - file = request.files['file'] - if file and allowed_file(file.filename): - # Make temporary directory - temp_dir = tempfile.mkdtemp() - # Process filename - filename = secure_filename(file.filename) - file.save(os.path.join(temp_dir, 'input.xml')) - # Redicrect - return redirect(url_for('view', temp_id=os.path.basename(os.path.normpath(temp_dir)))) + # Get file + file = request.files['file'] + # Check file existence + if not file: + return index(message='Please upload a KADMOS graph file.') + # Get file type + if request.values.get('file_type', 'auto') != 'auto': + file_type = request.values.get('file_type') + elif file.filename.lower().endswith(tuple(file_extensions)): + file_type = file_types[file_extensions.index(os.path.splitext(file.filename)[1].lower()[1:])] else: - # Process message - if request.values.get('message', None) is not None: - message = """<p><font color="red">"""+str(request.values.get('message'))+"""</font></p>""" - else: - message = "" - # Render page - return ''' - <!doctype html> - <title>Simple KADMOS interface</title> - <h1>Simple KADMOS interface</h1> - ''' + message + ''' - <p>Upload a CMDOWS file to get started!</p> - <form action="" method=post enctype=multipart/form-data> - <p><input type=file name=file> - <input type=submit value=Upload> - </form> - ''' + return index(message='The file type could not be recognized. Please specify it under advanced options.') + # Create temporary directory + temp_dir = tempfile.mkdtemp() + # Move file in temporary directory + path = os.path.join(temp_dir, 'input.'+file_extensions[file_types.index(file_type)]) + file.save(path) + # Get graph from file + # TODO Add exceptions and warnings if importing fails + if file_type == 'cmdows': + graph = load_from_cmdows(path) + elif file_type == 'kdms': + graph = load_from_kdms(path) + elif file_type == 'graphml': + graph = load_from_graphml(path) + # Save graph for further use + graph.save_as_kdms('graph.kdms', temp_dir) + # Send user to view page + return redirect(url_for('view', temp_id=os.path.basename(os.path.normpath(temp_dir)))) # View -@app.route('/view/<temp_id>') -def view(temp_id=None): - if temp_id is None: - return redirect(url_for('upload', message='Please upload a file.')) - elif not os.path.exists(os.path.join(temp, temp_id)): - return redirect(url_for('upload', message='The requested file does not exist (anymore). Please upload a file.')) - else: - return ''' - <!doctype html> - <title>Simple KADMOS interface</title> - <h1>Simple KADMOS interface</h1> - <p>Here we go! You just uploaded a CMDOWS file. What do you want to do with it?</p> - <ul> - <li><a href="'''+temp_id+'''/dsm">Create a DSM PDF</a></li> - <li><a>Create a dynamic DSM HTML</a></li> - <li><a>Create a new CMDOWS</a></li> - <li><a>Delete all data</a></li> - <li><a>Something else</a></li> - <ul> - ''' - -# DSM -@app.route('/view/<temp_id>/dsm') -def dsm(temp_id=None): - # TODO Add checks - # TODO Load graph only once then pickle +@app.route('/<temp_id>') +@app.route('/<temp_id>/<action>') +def view(temp_id=None, action=None): + # Check if files exist temp_dir = str(os.path.join(temp, temp_id)) - # TODO Change assert in load_from_cmdows to accept os paths next to strings - graph = load_from_cmdows('input.xml', source_folder=temp_dir) - # TODO Create_dsm should return path to file with extension - graph.create_dsm(filename='dsm', destination_folder=temp_dir) - return send_file(os.path.join(temp_dir, 'dsm.pdf')) + if not os.path.exists(temp_dir): + return redirect(url_for('index', message='The requested graph not exist (anymore). Please upload a new file.')) + # Load graph + graph = load_from_kdms('graph.kdms', source_folder=temp_dir) + # Perform actions + if action == 'pdf': + graph.create_dsm(filename='graph', + destination_folder=temp_dir) + return send_file(os.path.join(temp_dir, 'graph.pdf')) + if action == 'cmdows': + graph.save_as_cmdows(filename='graph', + destination_folder=temp_dir, + description='CMDOWS file created with the KADMOS interface', + creator='KADMOS interface', + file_version='1.0') + return send_file(os.path.join(temp_dir, 'graph.xml'), as_attachment=True) + if action == 'vispack': + graph.create_visualization_package(os.path.join(temp_dir, 'graph.vispack')) + return send_file(os.path.join(temp_dir, 'graph.vispack'), as_attachment=True) + if action == 'delete': + shutil.rmtree(temp_dir) + return redirect(url_for('index', message='All files were deleted.')) + + return render_template('view.html', temp_id=temp_id) + if __name__ == '__main__': app.run() diff --git a/pyKADMOS/sample/static/style.css b/pyKADMOS/sample/static/style.css new file mode 100644 index 0000000000000000000000000000000000000000..3b35d9dfc0009ae2ab502170af4ca7d57cdf49fd --- /dev/null +++ b/pyKADMOS/sample/static/style.css @@ -0,0 +1 @@ +input:checked + label + div { display: none; } \ No newline at end of file diff --git a/pyKADMOS/sample/templates/base.html b/pyKADMOS/sample/templates/base.html new file mode 100644 index 0000000000000000000000000000000000000000..6a2299debf45b8afba14ef0ee67a8dbce5ba5037 --- /dev/null +++ b/pyKADMOS/sample/templates/base.html @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<html lang=en> +<head> + <meta charset=UTF-8> + {% if title %} + <title>{{ title }} - KADMOS interface</title> + {% else %} + <title>KADMOS interface</title> + {% endif %} + <link rel=stylesheet href="{{ url_for('static', filename='style.css') }}"> +</head> +<body> + {% block content %}{% endblock %} +</body> +</html> \ No newline at end of file diff --git a/pyKADMOS/sample/templates/index.html b/pyKADMOS/sample/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..389270d846bafad245ae2cf6520a2864b9aae710 --- /dev/null +++ b/pyKADMOS/sample/templates/index.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% block content %} + <h1>Welcome to the KADMOS interface!</h1> + {% if message %} + <p><font color=red>{{ message }}</font></p> + {% endif %} + <p>Upload a KADMOS graph file to get started.<br>CMDOWS, KDMS and GRAPHML file types are supported.</p> + <form action="" method=post enctype=multipart/form-data> + <input type=file name=file> + <input type=submit value=Upload> + <br> + <input type=checkbox style="display: none" id=advanced checked> + <label for=advanced>Advanced Options</label> + <div id=advanced_options> + Filetype: + <select name=file_type> + <option value=auto selected>auto</option> + {% for type in types %} + <option value={{type}}>{{type.upper()}}</option> + {% endfor %} + </select> + </div> + </form> +{% endblock %} \ No newline at end of file diff --git a/pyKADMOS/sample/templates/view.html b/pyKADMOS/sample/templates/view.html new file mode 100644 index 0000000000000000000000000000000000000000..d25af836938b553461d017b83f51fd25a636f8f8 --- /dev/null +++ b/pyKADMOS/sample/templates/view.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% block content %} + <h1>Welcome to the KADMOS interface!</h1> + {% if message %} + <p><font color=red>{{ message }}</font></p> + {% endif %} + <p>Here we go! You just uploaded a KADMOS graph file. The graph was imported successfully. What do you want to do with it?</p> + <ul> + <li><a href="{{ temp_id }}/pdf">Create a (X)DSM PDF file</a></li> + <li><a href="{{ temp_id }}/vispack">Create a dynamic (X)DSM HTML package</a></li> + <li><a href="{{ temp_id }}/cmdows">Create a CMDOWS XML file</a></li> + <li><a href="{{ temp_id }}/delete">Delete all files</a></li> + <li><a>Something else</a></li> + </ul> +{% endblock %} \ No newline at end of file