Voucher_Create_ / app.py
Junad-Khn1's picture
Update app.py
92f16fb verified
import streamlit as st
import qrcode
from deep_translator import GoogleTranslator
from io import BytesIO
import pandas as pd
from PIL import Image, ImageDraw, ImageFont
import time
def generate_qr(data, size=150):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=5,
border=2,
)
qr.add_data(data)
qr.make(fit=True)
# Convert QR to PIL Image
img = qr.make_image(fill='black', back_color='white').convert("RGB") # Ensures compatibility
# Resize the image
img = img.resize((size, size), Image.LANCZOS)
# Save image to BytesIO
buf = BytesIO()
img.save(buf, format="PNG") # Explicitly define format
buf.seek(0)
return buf.getvalue() # Return raw byte data instead of BytesIO object
def translate_text(text):
try:
time.sleep(1) # Adding a small delay to prevent too many requests error
return GoogleTranslator(source='en', target='ar').translate(text) if text else "Not provided"
except:
return "Translation Error"
# Streamlit UI Styling
st.set_page_config(page_title="Voucher Generator", page_icon="🧾", layout="wide")
st.title("🧾 Voucher Generator")
st.markdown("---")
# Company Information
st.header("🏒 Business Details")
col1, col2 = st.columns(2)
with col1:
company_name = st.text_input("Company Name (English)")
company_address = st.text_input("Company Address (English)")
company_mobile = st.text_input("Company Mobile")
with col2:
company_name_arabic = translate_text(company_name)
company_address_arabic = translate_text(company_address)
company_mobile_arabic = translate_text(company_mobile)
st.text(f"Arabic: {company_name_arabic}")
st.text(f"Arabic: {company_address_arabic}")
st.text(f"Arabic: {company_mobile_arabic}")
st.markdown("---")
# Items Section
st.header("πŸ›’ Add Items to Voucher")
num_items = st.number_input("Number of Items", min_value=1, max_value=10, value=1)
items = []
total_amount = 0
for i in range(num_items):
st.markdown(f"### Item {i+1}")
col1, col2, col3 = st.columns(3)
with col1:
item_name = st.text_input(f"Item Name {i+1}", key=f"item_name_{i}")
with col2:
quantity = st.number_input(f"Quantity {i+1}", min_value=1, value=1, key=f"quantity_{i}")
with col3:
rate = st.number_input(f"Rate {i+1}", min_value=0.1, value=1.0, key=f"rate_{i}")
total = quantity * rate
items.append([item_name, quantity, rate, total])
total_amount += total
st.markdown("---")
if st.button("🧾 Generate Voucher"):
# Display Business Info
st.markdown(f"<h2 style='text-align: center;'>{company_name} / {company_name_arabic}</h2>", unsafe_allow_html=True)
st.markdown(f"<p style='text-align: center;'>πŸ“ {company_address} / {company_address_arabic}</p>", unsafe_allow_html=True)
st.markdown(f"<p style='text-align: center;'>πŸ“ž {company_mobile} / {company_mobile_arabic}</p>", unsafe_allow_html=True)
st.markdown("---")
# Display Items
if items:
df = pd.DataFrame(items, columns=["Item", "Quantity", "Rate", "Total"])
st.markdown("### πŸ›’ Voucher Items", unsafe_allow_html=True)
st.table(df)
st.markdown(f"<h3 style='text-align: center;'>🏷️ Total Amount: ${total_amount:.2f}</h3>", unsafe_allow_html=True)
# Generate QR Code with Voucher Data
voucher_data = f"Voucher from {company_name}\n" + "\n".join(
[f"{item[0]} - Qty: {item[1]}, Rate: {item[2]}, Total: {item[3]}" for item in items]
)
# βœ… Fix Indentation Here
qr_image_data = generate_qr(voucher_data) # Get raw image bytes
# Display the QR Code
st.markdown("<p style='text-align: center;'>Scan to View Voucher</p>", unsafe_allow_html=True)
st.image(qr_image_data, caption="Voucher QR Code")
# Download Button
st.download_button(
label="πŸ“₯ Download QR Code",
data=qr_image_data, # Use raw byte data
file_name="voucher_qr.png",
mime="image/png"
)
st.success("βœ… Voucher Created Successfully!")