2021年4月23日星期五

How do I display a generated QR code in flask

I am making a web app that generates a QR code using input from the user and saves it into a specified file. After the QR code is saved, It should display the generated QR code in a small box next to the input box. I am attempting to do this by having a GET submit button that goes to this route but I receive an error every time I click it.

The first block of code is for receiving the input and making a QR code out of it, and the second is my attempt to send back the finished PNG and display it.

Python Code:

    @app.route('/', methods= ['POST'])      def textfield():      text = request.form['text']      processed_text = text      if processed_text == '':         flash("Please enter a value!")      else:         QR(processed_text)      return render_template('base.html')                @app.route('/QR', methods= ['GET'])      def QRimage(text):      im = Image.open(f'{text}.png')      data = io.BytesIO()      im.save(data, "PNG")      encoded_img_data = base64.b64encode(data.getvalue())      img_data = encoded_img_data.decode('utf-8')      return render_template('base.html', value=img_data)  

Here is the HTML to attempt to display the image:

<img id="picture" src="data:image/jpeg;base64,">  

And the GET button that routes to /QR:

<form action="/QR" method="GET">  <button type="submit" type="button">QR Button</button>        
https://stackoverflow.com/questions/67239085/how-do-i-display-a-generated-qr-code-in-flask April 24, 2021 at 11:31AM

没有评论:

发表评论