Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions QR-Code-Flask-App/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, render_template, request, send_file
from flask import Flask, render_template, request, send_file, redirect, url_for
import qrcode
import io

Expand All @@ -7,27 +7,23 @@

@app.route("/", methods=["GET", "POST"])
def index():
qr_ready = False

if request.method == "POST":
url = request.form.get("url", "").strip()

if url:
# Add https:// if the user forgot it
if not url.startswith(("http://", "https://")):
url = "https://" + url

qr_ready = True
return render_template("index.html", qr_ready=qr_ready, url=url)

return render_template("index.html", qr_ready=qr_ready, url="")


@app.route("/download")
if request.method == 'POST':
#using strip() to remove any leading/trailing whitespace from the URL
url = request.form.get('url', '').strip()
#using if not url: to check if the URL is empty or None, and redirecting to the index page if it is
if not url:
return redirect(url_for('index'))
return redirect(url_for('index', url=url))

url = request.args.get('url', '')
qr_ready = bool(url)
return render_template('index.html', url=url, qr_ready=qr_ready)

@app.route('/download')
def download():
"""Generates the QR code image on the fly and sends it to the browser as a download."""
url = request.args.get("url", "")

url = request.args.get('url', '')
preview = request.args.get('preview')
if not url:
return "No URL provided", 400

Expand Down Expand Up @@ -56,4 +52,4 @@ def download():


if __name__ == "__main__":
app.run(debug=True)
app.run(debug=True)
Loading