Construct QR Code Generator with Python and Flask
In our earlier Python Porject instructional, now we have defined to increase School Management System with Python, Flask and MySQL. On this instructional, we will be able to give an explanation for find out how to construct QR Code Generator with Python and Flask.
This present day, QR code is grow to be part of our lifestyles. All of us use QR code for a couple of functions like making bills, activating gives, obtain apps, ebook tickets and so forth.
As programmer, have you ever ever attempted to generate QR codes? It’s in point of fact quite simple. You’ll be able to simply increase mission to generate QR Code as in line with your want. On this, we will be able to increase a mission the usage of Python and Flask to create QR code.
So let’s continue with growing mission:
1. Create Mission and Set up Module
We can create mission listing qrcde-python-flask
and set up all required modules.
- Flask: This can be a micro framework from Python to create internet utility. So we will be able to set up this module to create internet packages. We can set up it the usage of the under command:
pip set up Flask
- qrcode: We can set up
qrcode
Python module to generate QA code. We can set up it the usage of the under command:
pip set up qrcode
2. Create Flask Utility
We can create app.py
and import required modules. We come with put in flask module and it’s strategies. We can additionally import primary
module and it’s way generateCode
. Then we will be able to create our flask app.
from flask import Flask, render_template, request, redirect, url_for from primary import generateCode app = Flask(__name__)
We create direction and test psot strategies. We can create serve as index()
and enforce capability to name serve as generateCode()
to get QR code by means of passing code textual content. We can render the template index.html
and show generated QR code.
@app.direction('/', strategies=['GET', 'POST']) def index(): generatedCode="" mesage="" if request.way == 'POST': codeText = str(request.shape['codeText']) if codeText: generatedCode = generateCode(codeText) else: mesage="Please input textual content to generate code" go back render_template('index.html', mesage = mesage, generatedCode = generatedCode) if __name__ == '__main__': app.run()
3. Enforce QR Code Generator Serve as
We can create primary.py
and import qrcode
module. We can create serve as generateCode()
and enforce capability to generate QR code the usage of qrcode
module.
import qrcode from io import BytesIO import base64 def generateCode(textual content: str, img_name: str="QRCode.png") -> any: code = qrcode.QRCode( model=1, box_size=10, border=4 ) code.add_data(textual content) code.make(have compatibility=True) img = code.make_image(fill_color="black", back_color="white") buffer = BytesIO() img.save(buffer, layout="PNG") qrCode = f"information:symbol/png;base64,{base64.b64encode(buffer.getvalue()).decode()}" go back qrCode
4. Create Template Record
We can create template report index.html
and create FORM HTML to create QA code. We can additionally show generated QR code.
{% endblock %}
You’ll be able to obtain all the supply code of mission from the Obtain hyperlink under.