1

I have a simple code that will generate QR code, But I want the generated QR code to be displayed in a new tab, I already achieved displaying the image in the current page, but I want it to be opened in a new tab instead.

  code = form.code.data
  qr = pyqrcode.create(code)
  qr.png(code+'.png', scale=6)
  with open(code+'.png', "rb") as f:
    return Response(f.read(), content_type="image/png")

P.S: Edited it for future readers.

JackSlayer
  • 35
  • 6

2 Answers2

1

You need to do this from the client side (the browser / template) as flask runs on the server and has no connection with the browser (client).

From the template you can open a new window, which may or may not open a new tab (it depends on how the user has configured their browser).

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

If "New tab" means only show on web browser(without downloads), You can modify below HTTP HEADRS to show image without downloads.

Content-Disposition: attachment; filename="filename.png"
Content-Type: image/png

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

in flask, you can change http headers by

response = flask.Response()
response.headers['Content-Disposition'] = 'attachment; filename="filename.png"'
response.headers['Content-Type'] = 'image/png'
devkingsejong
  • 76
  • 1
  • 5