Calmcode - qr code: python qr service

Python QR Service

1 2 3 4

There are many ways to handle QR codes but it's likely you'll like to build a web interface for it. We've implemented one with FastAPI as an example below.

QRCode and FastAPI Example

import io
import qrcode

from fastapi import FastAPI
from starlette.responses import StreamingResponse

app = FastAPI()

@app.get("/generate/{message}")
def generate(message: str):
    img = qrcode.make(message)
    buf = io.BytesIO()
    img.save(buf)
    buf.seek(0) # important here!
    return StreamingResponse(buf, media_type="image/jpeg")

You can run this locally by running this command from the terminal:

uvicorn fastqrapi:app --reload

These commands do assume that both fastapi and uvicorn are installed. If you've never worked with FastAPI before you might appreciate our series of videos on fastapi.