Let's generate your Wifi qr code with flask Python

Let's generate your Wifi qr code with flask Python

Welcome to the world of QR code generators! Today, we're going to build our very own WiFi QR code generator using the Flask web framework and the qrcode library. But before we get started, let's make sure we have everything we need.

First, make sure you have Flask installed. You can install it by running pip install flask in your terminal. Next, we'll need to install the qrcode library. You can do this by running pip install qrcode==6.0. This is an important step because it ensures that we're using the right version of the library that is compatible with the rest of our code.

Now that we have everything we need, let's get started on our code! We'll start by importing Flask, request, and send_file from the flask module, as well as qrcode and BytesIO from the io module.

from flask import Flask, request, send_file
import qrcode
from io import BytesIO

Next, we'll create an instance of the Flask class, which we'll use to define and run our web application.

app = Flask(__name__)

Now, we'll define a route for our application using the @app.route decorator. This route will handle requests to the "/" endpoint, and call the generate_qr() function that we'll define next.

@app.route('/')
def generate_qr():

Inside the generate_qr() function, we'll create two variables, ssid and password, which will store the name and password for our WiFi network, respectively. We'll then use these variables to create a string in the format that qrcode library uses to encode WiFi information into a QR code.

    ssid = "Get Your Own Wifi"
    password = "ItIsMyWifi69"

    wifi_data = 'WIFI:S:{};T:WPA;P:{};;'.format(ssid, password)

Next, we'll create an instance of the QRCode class, which will allow us to generate a QR code with the information we've provided. We'll set the version, error correction, box size, and border properties to configure the QR code.

    qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=5)

Then, we'll add the wifi_data to the QR code object and fit the data to the QR code

    qr.add_data(wifi_data)
    qr.make(fit=True)

We'll create an image of the QR code with the fill_color and back_color to be black and white respectively.

    img = qr.make_image(fill_color="black", back_color="white")

We'll now create a BytesIO object to hold the image in memory and save the image to the BytesIO object in PNG format

    byteIO = BytesIO()
    img.save(byteIO, 'PNG')

We'll reset the seek to the start of the byteIO object and return the image file with 'image/png' mimetype.

    byteIO.seek(0)
    return send_file(byteIO, mimetype='image/png')

Finally, we'll add a check to run the application only if the script is being run directly (and not imported as a module).

if __name__ == '__main__':
    app.run(port=5000,debug=True)

And that's it! Our WiFi QR code generator is now complete. You can now run the script and visit "localhost:5000" to see your very own QR code for your WiFi network. With this handy tool, you can easily share your WiFi network with friends and family without having to reveal your password. Just remember, never share your wifi passwords with anyone you don't trust. Have fun and happy coding!

Full Code :

from flask import Flask, request, send_file
import qrcode
from io import BytesIO

app = Flask(__name__)

@app.route('/')
def generate_qr():
    ssid = "Get Your Own Wifi"
    password = "ItIsMyWifi69"

    wifi_data = 'WIFI:S:{};T:WPA;P:{};;'.format(ssid, password)

    qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=5)
    qr.add_data(wifi_data)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white")

    byteIO = BytesIO()
    img.save(byteIO, 'PNG')
    byteIO.seek(0)
    return send_file(byteIO, mimetype='image/png')

if __name__ == '__main__':
    app.run(port=5000,debug=True)