Spaces:
Sleeping
Sleeping
File size: 15,433 Bytes
d10db5f 1451c9d d10db5f 6657ee6 d10db5f 29fed68 d10db5f 71721f2 d10db5f 29fed68 3914eaf 29fed68 d10db5f 7ded169 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
import streamlit as st
import barcode
import segno
import segno.helpers
import io
class QRCodeGenerator:
"""
QRCodeGenerator class generates different types of QR codes and barcodes.
"""
def __init__(self):
"""
Constructor of the class. Sets the title of the Streamlit application and creates interactive buttons.
"""
st.title("QR Code Generator")
# Button labels and functions
button_labels = ["Text", "Link", "VCard", "Wifi", "Email", "Geo", "Micro", "Barcode"]
button_job = [self.text_exp, self.link_exp, self.vcard_exp, self.wifi_exp,
self.email_exp, self.geo_exp, self.micro_exp, self.barcode_exp]
# Check state variables
if 'active_expander' not in st.session_state:
st.session_state.active_expander = None # No expander is open initially
# Create buttons
cols = st.columns(4)
for i, label in enumerate(button_labels):
with cols[i % 4]:
if st.button(label):
st.session_state.active_expander = label # Keep the clicked button's expander open
# Close other expanders
for lbl in button_labels:
if lbl != label:
st.session_state[f'show_{lbl.lower()}'] = False
# Show expanders
for label in button_labels:
if st.session_state.active_expander == label:
button_job[button_labels.index(label)]()
def generate(self, qr_type: str, input_data: dict):
"""
Generates a QR code or barcode of the given type.
Args:
qr_type (str): The type of code to generate (Text, Link, vCard, Wifi, Email, Geo, Micro, Barcode).
input_data (dict): The content of the code.
Returns:
QRCode or barcode object.
"""
try:
if qr_type == "Text":
data = self.text_link_qr(input_data)
elif qr_type == "Link":
data = self.text_link_qr(input_data)
elif qr_type == "VCard":
data = self.vcard_qr(input_data)
elif qr_type == "Wifi":
data = self.wifi_qr(input_data)
elif qr_type == "Email":
data = self.email_qr(input_data)
elif qr_type == "Geo":
data = self.geo_qr(input_data)
elif qr_type == "Micro":
data = self.micro_qr(input_data)
elif qr_type == "Barcode":
data = self.barcode_(input_data)
return data
except Exception as e:
st.write("An error occurred while generating the QR code:", e)
def display(self, qr_code):
"""
Displays the generated QR code or barcode.
Args:
qr_code: QR code or barcode object.
"""
buffer = io.BytesIO()
if type(qr_code) == segno.QRCode:
qr_code.save(buffer, kind="png", scale=50) # Save QR code in PNG format
else:
qr_code.write(buffer) # Save barcode
buffer.seek(0)
st.image(buffer, caption="Generated Code", use_container_width = True) # Show the image
def download(self, data):
"""
Makes the generated QR code or barcode available for download.
Args:
data: QR code or barcode object to download.
"""
try:
buffer = io.BytesIO()
if type(data) == segno.QRCode:
data.save(buffer, kind="png", scale=40) # Save QR code as PNG
else:
data.write(buffer) # Save barcode
buffer.seek(0)
st.download_button("Download", buffer, file_name="code.png", mime="image/png") # Download button
except Exception as e:
st.write("An error occurred while downloading the QR code:", e)
def text_exp(self):
"""
Interface for creating text QR code.
"""
with st.expander("Text", expanded=True):
content = st.text_area("Enter your text here", placeholder="Enter text here", key='text_area')
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_text = True
input_data = {"Content": content}
qr_code = self.generate("Text", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {"Content": content}
data = self.generate("Text", input_data)
self.download(data)
def link_exp(self):
"""
Interface for creating link QR code.
"""
with st.expander("Link", expanded=True):
content = st.text_input("Enter your link here", placeholder="Enter link here")
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_link = True # Update state
input_data = {"Content": content}
qr_code = self.generate("Text", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {"Content": content}
data = self.generate("Link", input_data)
self.download(data)
def vcard_exp(self):
"""
Interface for creating VCard QR code.
"""
with st.expander("VCard", expanded=True):
name = st.text_input("Name*", placeholder="Enter name here")
displayname = st.text_input("Display Name*", placeholder="Enter display name here")
email = st.text_input("Email (optional)", placeholder="Enter email here")
phone = st.text_input("Phone (optional)", placeholder="Enter phone number here")
memo = st.text_input("Note (optional)", placeholder="Enter notes here")
birthday = st.date_input("Birthday (optional)")
url = st.text_input("URL (optional)", placeholder="Enter URL here")
pobox = st.text_input("Post Box (optional)", placeholder="Enter Post Box here")
street = st.text_input("Street (optional)", placeholder="Enter street here")
city = st.text_input("City (optional)", placeholder="Enter city here")
region = st.text_input("Region (optional)", placeholder="Enter region here")
zipcode = st.text_input("Zip Code (optional)", placeholder="Enter zip code here")
country = st.text_input("Country (optional)", placeholder="Enter country here")
org = st.text_input("Organization (optional)", placeholder="Enter organization here")
title = st.text_input("Title (optional)", placeholder="Enter title here")
photo_uri = st.text_input("Photo URI (optional)", placeholder="Enter photo URI here")
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_vcard = True # Update state
input_data = {
"Name": name, "Displayname": displayname, "Email": email,
"Phone": phone, "Memo": memo, "Birthday": birthday,
"URL": url, "Pobox": pobox, "Street": street, "City": city,
"Region": region, "Zipcode": zipcode, "Country": country,
"Org": org, "Title": title, "Photo_Uri": photo_uri
}
qr_code = self.generate("VCard", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {
"Name": name, "Displayname": displayname, "Email": email,
"Phone": phone, "Memo": memo, "Birthday": birthday,
"URL": url, "Pobox": pobox, "Street": street, "City": city,
"Region": region, "Zipcode": zipcode, "Country": country,
"Org": org, "Title": title, "Photo_Uri": photo_uri
}
data = self.generate("VCard", input_data)
self.download(data)
def wifi_exp(self):
"""
Interface for creating Wifi QR code.
"""
with st.expander("Wifi", expanded=True):
ssid = st.text_input("SSID", placeholder="Enter SSID here")
password = st.text_input("Password", placeholder="Enter password here")
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_wifi = True # Update state
input_data = {"SSID": ssid, "Password": password}
qr_code = self.generate("Wifi", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {"SSID": ssid, "Password": password}
data = self.generate("Wifi", input_data)
self.download(data)
def email_exp(self):
"""
Interface for creating Email QR code.
"""
with st.expander("Email", expanded=True):
subject = st.text_input("Subject", placeholder="Enter subject here")
body = st.text_area("Body", placeholder="Enter body here")
to = st.text_input("To", placeholder="Enter to here")
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_email = True # Update state
input_data = {"Subject": subject, "Body": body, "To": to}
qr_code = self.generate("Email", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {"Subject": subject, "Body": body, "To": to}
data = self.generate("Email", input_data)
self.download(data)
def geo_exp(self):
"""
Interface for creating Geo QR code.
"""
with st.expander("Geo", expanded=True):
lat = st.number_input("Latitude", min_value=-90.0, max_value=90.0, format="%.4f", placeholder="Enter latitude here")
lng = st.number_input("Longitude", min_value=-180.0, max_value=180.0, format="%.4f", placeholder="Enter longitude here")
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_geo = True # Update state
input_data = {"Latitude": lat, "Longitude": lng}
qr_code = self.generate("Geo", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {"Latitude": lat, "Longitude": lng}
data = self.generate("Geo", input_data)
self.download(data)
def micro_exp(self):
"""
Interface for creating Micro QR code.
"""
with st.expander("Micro", expanded=True):
text = st.text_area("Enter your text here", placeholder="Enter text here")
col1, col2 = st.columns(2)
if col1.button("Show"):
st.session_state.show_micro = True # Update state
input_data = {"Text": text}
qr_code = self.generate("Micro", input_data)
self.display(qr_code)
if col2.button("Download"):
input_data = {"Text": text}
data = self.generate("Micro", input_data)
self.download(data)
def barcode_exp(self):
"""
Interface for creating Barcode.
"""
with st.expander("Barcode", expanded=True):
type = st.selectbox("Select a type", ["code128", "ean13", "ean8", "upc", "isbn10", "isbn13", "pzn", "itf", "codabar", "qr"])
number = st.text_input("Enter a number", placeholder="Enter a number here")
st.error("Contains non-working code parts.")
# col1, col2 = st.columns(2)
# if col1.button("Show"):
# st.session_state.show_barcode = True # Update state
# input_data = {"Type": type, "Number": number}
# brcd = self.generate("Barcode", input_data)
# self.display(brcd)
# if col2.button("Download"):
# input_data = {"Type": type, "Number": number}
# data = self.generate("Barcode", input_data)
# self.download(data)
def text_link_qr(self, content: dict):
"""
Creates a QR code for text or link.
Args:
content (dict): QR code content.
Returns:
segno.QRCode: The created QR code object.
"""
qr = segno.make_qr(content["Content"])
return qr
def vcard_qr(self, vcard: dict):
"""
Creates a QR code for VCard information.
Args:
vcard (dict): VCard information.
Returns:
segno.QRCode: The created QR code object.
"""
qr = segno.helpers.make_vcard (
name=vcard["Name"],
displayname=vcard["Displayname"],
email=vcard["Email"],
phone=vcard["Phone"],
memo=vcard["Memo"],
birthday=vcard["Birthday"],
url=vcard["URL"],
pobox=vcard["Pobox"],
street=vcard["Street"],
city=vcard["City"],
region=vcard["Region"],
zipcode=vcard["Zipcode"],
country=vcard["Country"],
org=vcard["Org"],
title=vcard["Title"],
photo_uri=vcard["Photo_Uri"]
)
return qr
def wifi_qr(self, wifi: dict):
"""
Creates a QR code for Wifi network.
Args:
wifi (dict): Wifi information.
Returns:
segno.QRCode: The created QR code object.
"""
qr = segno.helpers.make_wifi(ssid=wifi["SSID"], password=wifi["Password"])
return qr
def email_qr(self, email: dict):
"""
Creates a QR code for Email information.
Args:
email (dict): Email information.
Returns:
segno.QRCode: The created QR code object.
"""
qr = segno.helpers.make_email(email["Subject"], email["Body"], email["To"])
return qr
def geo_qr(self, geo: dict):
"""
Creates a QR code for geographical location.
Args:
geo (dict): Geographical location information.
Returns:
segno.QRCode: The created QR code object.
"""
qr = segno.helpers.make_geo(geo["Latitude"], geo["Longitude"])
return qr
def micro_qr(self, content: dict):
"""
Creates a Micro QR code.
Args:
content (dict): Micro QR code content.
Returns:
segno.QRCode: The created micro QR code object.
"""
qr = segno.make_micro(content["Text"])
return qr
def barcode_(self, brcode: dict):
"""
Creates a barcode.
Args:
brcode (dict): Barcode information.
Returns:
barcode.barcode.Barcode: The created barcode object.
"""
# Gives an error: AttributeError: module 'barcode' has no attribute 'get_barcode'
# brcd = barcode.get_barcode(name=brcode["Type"], code=brcode["Number"], writer=barcode.writer.ImageWriter())
# return brcd
pass
# Start QRCodeGenerator class
QRCodeGenerator() |