Advertisement
usa38

Simple QR Code Generator (MicroPython)

Apr 18th, 2024
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. import uasyncio as asyncio
  2. import qrcode
  3. from picoweb import WebApp, start_response
  4.  
  5. app = WebApp(__name__)
  6.  
  7. @app.route("/")
  8. def index(req, resp):
  9.     yield from start_response(resp)
  10.     yield from resp.awrite("<html><body>")
  11.     yield from resp.awrite("<h1>QR Code Generator</h1>")
  12.     yield from resp.awrite("<form method='POST' action='/generate'>")
  13.     yield from resp.awrite("<input type='text' name='content' placeholder='Enter value'>")
  14.     yield from resp.awrite("<input type='submit' value='Generate QR Code'>")
  15.     yield from resp.awrite("</form>")
  16.     yield from resp.awrite("</body></html>")
  17.  
  18. @app.route("/generate", methods=["POST"])
  19. def generate_qr(req, resp):
  20.     content = req.form.get("content", "")
  21.     qr = qrcode.make(content)
  22.     qr.save("qr_code.png")
  23.     yield from start_response(resp)
  24.     yield from resp.awrite("<html><body>")
  25.     yield from resp.awrite(f"<h2>Generated QR Code for: {content}</h2>")
  26.     yield from resp.awrite("<img src='qr_code.png' alt='QR Code'>")
  27.     yield from resp.awrite("</body></html>")
  28.  
  29. loop = asyncio.get_event_loop()
  30. loop.create_task(app.run(debug=True))
  31. loop.run_forever()
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement