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