qr-python / src /streamlit_app.py
sameernotes's picture
Update src/streamlit_app.py
11872ae verified
raw
history blame
8.26 kB
import streamlit as st
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer, CircleModuleDrawer, SquareModuleDrawer
from qrcode.image.styles.colorfills import SolidFillColorMask, RadialGradiantColorMask, SquareGradiantColorMask
from PIL import Image, ImageDraw
import io
import base64
# Page configuration
st.set_page_config(
page_title="QR Code Generator",
page_icon="πŸ“±",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for beautiful styling
st.markdown("""
<style>
.main-header {
text-align: center;
padding: 2rem 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 10px;
margin-bottom: 2rem;
}
.feature-box {
background: #f8f9fa;
padding: 1.5rem;
border-radius: 10px;
border-left: 4px solid #667eea;
margin: 1rem 0;
}
.download-section {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
padding: 2rem;
border-radius: 15px;
text-align: center;
margin: 2rem 0;
}
.sidebar-section {
background: #ffffff;
padding: 1rem;
border-radius: 10px;
margin: 1rem 0;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
""", unsafe_allow_html=True)
def create_qr_code(data, fill_color, back_color, border, box_size, error_correction, style_type):
"""Generate QR code with custom styling"""
# Error correction levels
error_levels = {
'L (~7%)': qrcode.constants.ERROR_CORRECT_L,
'M (~15%)': qrcode.constants.ERROR_CORRECT_M,
'Q (~25%)': qrcode.constants.ERROR_CORRECT_Q,
'H (~30%)': qrcode.constants.ERROR_CORRECT_H
}
# Create QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=error_levels[error_correction],
box_size=box_size,
border=border,
)
qr.add_data(data)
qr.make(fit=True)
# Style configurations
if style_type == "Standard":
img = qr.make_image(fill_color=fill_color, back_color=back_color)
elif style_type == "Rounded":
img = qr.make_image(
image_factory=StyledPilImage,
module_drawer=RoundedModuleDrawer(),
fill_color=fill_color,
back_color=back_color
)
elif style_type == "Circular":
img = qr.make_image(
image_factory=StyledPilImage,
module_drawer=CircleModuleDrawer(),
fill_color=fill_color,
back_color=back_color
)
elif style_type == "Gradient":
img = qr.make_image(
image_factory=StyledPilImage,
color_mask=RadialGradiantColorMask(),
fill_color=fill_color,
back_color=back_color
)
return img
def get_download_link(img, filename):
"""Generate download link for the QR code"""
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
href = f'<a href="data:image/png;base64,{img_str}" download="{filename}" class="download-btn">πŸ“₯ Download QR Code</a>'
return href
# Main header
st.markdown("""
<div class="main-header">
<h1>🎨 Beautiful QR Code Generator</h1>
<p>Create stunning, customizable QR codes with advanced styling options</p>
</div>
""", unsafe_allow_html=True)
# Sidebar for customization
with st.sidebar:
st.markdown("## πŸŽ›οΈ Customization Panel")
# QR Code Content
st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
st.markdown("### πŸ“ Content")
content_type = st.selectbox(
"Content Type",
["Text", "URL", "Email", "Phone", "SMS", "WiFi"]
)
if content_type == "Text":
qr_data = st.text_area("Enter your text:", height=100)
elif content_type == "URL":
qr_data = st.text_input("Enter URL:", placeholder="https://example.com")
elif content_type == "Email":
email = st.text_input("Email address:")
subject = st.text_input("Subject (optional):")
body = st.text_area("Message (optional):", height=80)
qr_data = f"mailto:{email}?subject={subject}&body={body}" if email else ""
elif content_type == "Phone":
qr_data = st.text_input("Phone number:", placeholder="+1234567890")
if qr_data:
qr_data = f"tel:{qr_data}"
elif content_type == "SMS":
phone = st.text_input("Phone number:", placeholder="+1234567890")
message = st.text_area("Message:", height=80)
qr_data = f"sms:{phone}?body={message}" if phone else ""
elif content_type == "WiFi":
ssid = st.text_input("Network Name (SSID):")
password = st.text_input("Password:", type="password")
security = st.selectbox("Security Type", ["WPA", "WEP", "nopass"])
hidden = st.checkbox("Hidden Network")
if ssid:
qr_data = f"WIFI:T:{security};S:{ssid};P:{password};H:{'true' if hidden else 'false'};;"
else:
qr_data = ""
st.markdown('</div>', unsafe_allow_html=True)
# Style Settings
st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
st.markdown("### 🎨 Style Settings")
style_type = st.selectbox(
"QR Style",
["Standard", "Rounded", "Circular", "Gradient"]
)
col1, col2 = st.columns(2)
with col1:
fill_color = st.color_picker("Foreground Color", "#000000")
with col2:
back_color = st.color_picker("Background Color", "#FFFFFF")
st.markdown('</div>', unsafe_allow_html=True)
# Advanced Settings
st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
st.markdown("### βš™οΈ Advanced Settings")
box_size = st.slider("Module Size", 1, 20, 10)
border = st.slider("Border Width", 1, 10, 4)
error_correction = st.selectbox(
"Error Correction Level",
["L (~7%)", "M (~15%)", "Q (~25%)", "H (~30%)"]
)
st.markdown('</div>', unsafe_allow_html=True)
# Main content area
col1, col2 = st.columns([2, 1])
with col1:
if qr_data:
try:
# Generate QR code
qr_img = create_qr_code(
qr_data, fill_color, back_color, border,
box_size, error_correction, style_type
)
# Display QR code
st.markdown("### πŸ“± Your QR Code")
st.image(qr_img, caption="Generated QR Code", use_container_width=True)
# Download section
st.markdown("""
<div class="download-section">
<h3>πŸš€ Ready to Download!</h3>
<p>Your QR code is ready. Click below to download it as PNG.</p>
</div>
""", unsafe_allow_html=True)
# Download button
filename = f"qrcode_{content_type.lower()}.png"
st.markdown(get_download_link(qr_img, filename), unsafe_allow_html=True)
except Exception as e:
st.error(f"Error generating QR code: {str(e)}")
else:
st.info("πŸ‘ˆ Please enter content in the sidebar to generate your QR code")
with col2:
st.markdown("### πŸ“‹ Features")
features = [
"🎨 Multiple visual styles",
"🌈 Custom colors",
"πŸ“± Various content types",
"βš™οΈ Advanced settings",
"πŸ“₯ Instant download",
"πŸ”§ Error correction levels"
]
for feature in features:
st.markdown(f'<div class="feature-box">{feature}</div>', unsafe_allow_html=True)
st.markdown("### πŸ’‘ Tips")
st.info("""
**Higher Error Correction** = More resilient to damage but larger QR code
**Larger Module Size** = Easier to scan but bigger file
**Good Contrast** = Better scanning reliability
""")
# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center; color: #666; padding: 2rem;">
<p>πŸš€ Built with Streamlit | πŸ’ Made with love for QR code enthusiasts</p>
</div>
""", unsafe_allow_html=True)