def process_structure(): if flask.request.method == "POST": # check if the post request has the file part if "structurefile" not in flask.request.files: return flask.redirect(flask.url_for("input_data")) structurefile = flask.request.files["structurefile"] fileformat = flask.request.form.get("fileformat", "unknown") filecontent = structurefile.read().decode("utf-8") skin_factor = flask.request.form.get("skin-factor", "") try: skin_factor = float(skin_factor) except ValueError: flask.flash( "Invalid value for skin factor, must be float ({})".format(skin_factor) ) return flask.redirect(flask.url_for("input_data")) try: structure = parse_structure( filecontent=filecontent, fileformat=fileformat, extra_data=dict(flask.request.form), ) except Exception as exc: traceback.print_exc() flask.flash( "Unable to parse the structure, sorry... ({}, {})".format( str(type(exc)), str(exc) ) ) return flask.redirect(flask.url_for("input_data")) try: data_for_template = process_structure_core( structure=structure, logger=logger, flask_request=flask.request, skin_factor=skin_factor, ) config = get_config() tvars = header.template_vars return flask.render_template( "user_templates/visualizer_header.j2", **data_for_template, **config, **tvars, ) except FlaskRedirectException as e: flask.flash(str(e)) return flask.redirect(flask.url_for("input_data")) except Exception as exc: traceback.print_exc() flask.flash( "Unable to process the structure, sorry... ({}, {})".format( str(type(exc)), str(exc) ) ) return flask.redirect(flask.url_for("input_data")) else: # GET Request return flask.redirect(flask.url_for("input_data"))
def process_example_structure(): """ Process an example structure (example name from POST request) """ if flask.request.method == "POST": examplestructure = flask.request.form.get("examplestructure", "<none>") fileformat = "xsf-ase" try: filename, skin_factor = VALID_EXAMPLES[examplestructure] except KeyError: flask.flash("Invalid example structure '{}'".format(examplestructure)) return flask.redirect(flask.url_for("input_data")) # I expect that the valid_examples dictionary already filters only # existing files, so I don't try/except here with open( os.path.join(os.path.dirname(__file__), "xsf-examples", filename,) ) as structurefile: filecontent = structurefile.read() try: structure = parse_structure(filecontent=filecontent, fileformat=fileformat,) except Exception as exc: flask.flash( "Unable to parse the example structure, sorry... ({}, {})".format( str(type(exc)), str(exc) ) ) return flask.redirect(flask.url_for("input_data")) try: data_for_template = process_structure_core( structure=structure, logger=logger, flask_request=flask.request, skin_factor=skin_factor, ) config = get_config() tvars = header.template_vars return flask.render_template( "user_templates/visualizer_header.j2", **data_for_template, **config, **tvars, ) except FlaskRedirectException as e: flask.flash(str(e)) return flask.redirect(flask.url_for("input_data")) except Exception as exc: traceback.print_exc() flask.flash( "Unable to process the structure, sorry... ({}, {})".format( str(type(exc)), str(exc) ) ) return flask.redirect(flask.url_for("input_data")) else: # GET Request return flask.redirect(flask.url_for("input_data"))
def input_data(): """ Main view, input data selection and upload """ return flask.render_template(get_visualizer_select_template(flask.request), **get_config())