Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files123123456789678910111213141516171819202122232425262728293031323334353637383940101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256import altair as altimport numpy as npimport pandas as pdimport streamlit as stimport qrcodefrom qrcode.image.styledpil import StyledPilImagefrom qrcode.image.styles.moduledrawers import RoundedModuleDrawer, CircleModuleDrawer, SquareModuleDrawerfrom qrcode.image.styles.colorfills import SolidFillColorMask, RadialGradiantColorMask, SquareGradiantColorMaskfrom PIL import Image, ImageDrawimport ioimport base64
"""# Welcome to Streamlit!
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [communityforums](https://discuss.streamlit.io).
In the meantime, below is an example of what you can do with just a few lines of code:"""
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
indices = np.linspace(0, 1, num_points)theta = 2 * np.pi * num_turns * indicesradius = indices
x = radius * np.cos(theta)y = radius * np.sin(theta)
df = pd.DataFrame({ "x": x, "y": y, "idx": indices, "rand": np.random.randn(num_points),})
st.altair_chart(alt.Chart(df, height=700, width=700) .mark_point(filled=True) .encode( x=alt.X("x", axis=None), y=alt.Y("y", axis=None), color=alt.Color("idx", legend=None, scale=alt.Scale()), size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])), ))# Page configurationst.set_page_config( page_title="QR Code Generator", page_icon="π±", layout="wide", initial_sidebar_state="expanded")
# Custom CSS for beautiful stylingst.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 headerst.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 customizationwith 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 areacol1, 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 """)
# Footerst.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)src/streamlit_app.py+254-38