QR Code link to open up a form with database row

for the generating a qrcode i’d probably give each row a unique id which you can reference as part of a url with a hash or as an api endpoint

something like this should work on the server to generate qr codes.

import uuid
import pyqrcode

@anvil.server.callable
def add_row(data):
  uid = str(uuid.uuid4())
  qrcode = get_qr_code(f"{anvil.server.get_app_origin()}/#{uid}", uid)
  # generate a url to later use with anvil.get_url_hash() (or HashRouting)
  # or use an api endpoint instead
  app_tables.my_table.add_row(**data, uid=uid, qrcode=qrcode)
  
def get_qr_code(url, uid):
  # from pyqrcode docs
  code = pyqrcode.create(url, error='L', version=27, mode='binary')
  file = f'/tmp/{uid}.png'
  code.png(file, scale=6, module_color=[0, 0, 0, 128], background=[0xff, 0xff, 0xcc])
  with open(file, 'rb') as f:
    media = anvil.BlobMedia('image/png', f.read(), uid)
  return media

as for opening the qrcode - I think most modern smart phone cameras that recognise a qrcode url will display a popup that asks if you want to open the url.

3 Likes