diff --git a/QR-Code-Flask-App/Readme.md b/QR-Code-Flask-App/Readme.md new file mode 100644 index 0000000..b7ffcfe --- /dev/null +++ b/QR-Code-Flask-App/Readme.md @@ -0,0 +1,33 @@ +# QR Code Generator (Flask) + +A small web app that turns any URL into a downloadable QR code image. + +## Features +- Enter a URL and generate a QR code instantly +- Automatically adds `https://` if you forget it +- Download the QR code as a PNG + +## Requirements +- Python 3.8+ +- Flask, qrcode, Pillow + +## Installation +```bash +pip install -r requirements.txt +``` + +## Usage +```bash +python app.py +``` +Then open http://127.0.0.1:5000 in your browser, enter a URL, and click download. + +## Project Structure +``` +QR-Code-Flask-App/ +├── app.py +├── templates/ +│ └── index.html +├── requirements.txt +└── README.md +``` \ No newline at end of file diff --git a/QR-Code-Flask-App/app.py b/QR-Code-Flask-App/app.py new file mode 100644 index 0000000..44b9e71 --- /dev/null +++ b/QR-Code-Flask-App/app.py @@ -0,0 +1,59 @@ +from flask import Flask, render_template, request, send_file +import qrcode +import io + +app = Flask(__name__) + + +@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") +def download(): + """Generates the QR code image on the fly and sends it to the browser as a download.""" + url = request.args.get("url", "") + + if not url: + return "No URL provided", 400 + + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_M, + box_size=10, + border=4, + ) + qr.add_data(url) + qr.make(fit=True) + + img = qr.make_image(fill_color="black", back_color="white") + + # Save image to memory instead of disk, so it can be sent straight to the browser + buffer = io.BytesIO() + img.save(buffer, format="PNG") + buffer.seek(0) + + return send_file( + buffer, + mimetype="image/png", + as_attachment=True, + download_name="qr.png", + ) + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/QR-Code-Flask-App/requirments.txt.txt b/QR-Code-Flask-App/requirments.txt.txt new file mode 100644 index 0000000..e9501d7 --- /dev/null +++ b/QR-Code-Flask-App/requirments.txt.txt @@ -0,0 +1,2 @@ + Flask + qrcode[pil] \ No newline at end of file diff --git a/QR-Code-Flask-App/templates/index.html b/QR-Code-Flask-App/templates/index.html new file mode 100644 index 0000000..858cc6c --- /dev/null +++ b/QR-Code-Flask-App/templates/index.html @@ -0,0 +1,83 @@ + + + + + +QR Code Generator + + + +
+

QR Code Generator

+
+ + +
+ + {% if qr_ready %} +
+ QR code for {{ url }} + Download PNG +
+ {% endif %} +
+ +