Search code examples
pythonflaskweb-applicationsreportpermalinks

Flask webapp generating permanent report as link


Ok I can't find any description, which matches my problem or use-case.

I have developed a flask web-app, where a user can input some form data in the first template to make a database query. Based on the query result a second template with the output is rendered, which will rendering the results in a readable way.

My problem is, that I would like to provide a link for documentation/report where someone just opens the link and can view the same displayed report.

Or alternatively save the generated html as pdf on google drive.

Any suggestions how to do this problems?

When you see the example code, I want to open the out.html as it was rendered with the specific user input at a later point in time.

I did not find any method how to generate that link, if it is possible?


from flask import Flask
app = Flask(__name__)

@app.route('/start')
def start():
   return render_template("start.html")

@app.route('/out')
def out():
   input = request.form
   return render_template("out.html", input=input["Test"])

Start.html

  <!doctype html>
  <html>
  <body>
  <h1>Start</h1>

  <form action = "/out" method = "POST">
          <p>Test<input type = "text" name = "Test" required/></p>
          <p><input type = "submit" value = "SUBMIT" /></p>
      </form>
  </body>
  </html>

  '''

  '''out.html

  <!doctype html>
  <html>
  <body>
  <h1>Out</h1>

  <p>{{input}}</p>

  </body>
  </html>


Solution

  • from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return """<form action="/result" method="GET">
              <input type="text" name="test"><input type="submit"></form>"""
    
    @app.route("/result")
    def result():
        return request.args.get("test")
    if __name__ == "__main__":
        app.run()
    

    You can copy the link from the address bar which provides the same result.

    EDIT:

    I can't understand your problem. So I am guessing that your {{input}} is not getting the value. If that so,

    You need to do this in your backend:

    ......
    input = request.args.get("test")
    return render_template("out.html", input=input)
    ......
    

    EDIT 2

    Or you can use sql database to store the input values with 16bit unique id for each new input values.

    ....
    @app.route("/<unique>")
    def result(unique):
        #retrieve the input values from the database using the id
        # and render the template