from flask import Flask, render_template, request, send_from_directory, session, url_for import json app = Flask(__name__) app.config["UPLOAD_FOLDER"] = "static/files" with open("files/secrets") as f: app.secret_key = f.read() @app.route("/", methods=["GET", "POST"]) def hello_world(): if request.method == "GET": return render_template("waiting.html", argument="foofa") return "nuh uh" @app.route("/searching", methods=["POST"]) def searching(): user_query = request.form.get("keyword") keywords = json.loads(open("static/mapping", "r").read()) session["filename"] = keywords.get(user_query) return render_template("searching.html") @app.route("/result", methods=["GET", "POST"]) def result(): if request.method == "POST": return searching() if session.get("filename"): return render_template("result.html", path=True) return render_template("result.html", path=False) @app.route("/show", methods=["GET", "POST"]) def show(): path = session.get("filename") if path: return send_from_directory( app.config["UPLOAD_FOLDER"], path, as_attachment=True ) return "something has gone horribly wrong" if __name__ == "__main__": mapping = {"foo": "test.txt"} with open("files/mapping", "w") as f: f.write(json.dumps(mapping)) app.run(debug=True) # os.chdir("files")