sameernotes commited on
Commit
11872ae
Β·
verified Β·
1 Parent(s): 1e1d2f3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +254 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,256 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import qrcode
3
+ from qrcode.image.styledpil import StyledPilImage
4
+ from qrcode.image.styles.moduledrawers import RoundedModuleDrawer, CircleModuleDrawer, SquareModuleDrawer
5
+ from qrcode.image.styles.colorfills import SolidFillColorMask, RadialGradiantColorMask, SquareGradiantColorMask
6
+ from PIL import Image, ImageDraw
7
+ import io
8
+ import base64
9
 
10
+ # Page configuration
11
+ st.set_page_config(
12
+ page_title="QR Code Generator",
13
+ page_icon="πŸ“±",
14
+ layout="wide",
15
+ initial_sidebar_state="expanded"
16
+ )
17
+
18
+ # Custom CSS for beautiful styling
19
+ st.markdown("""
20
+ <style>
21
+ .main-header {
22
+ text-align: center;
23
+ padding: 2rem 0;
24
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
25
+ color: white;
26
+ border-radius: 10px;
27
+ margin-bottom: 2rem;
28
+ }
29
+
30
+ .feature-box {
31
+ background: #f8f9fa;
32
+ padding: 1.5rem;
33
+ border-radius: 10px;
34
+ border-left: 4px solid #667eea;
35
+ margin: 1rem 0;
36
+ }
37
+
38
+ .download-section {
39
+ background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
40
+ padding: 2rem;
41
+ border-radius: 15px;
42
+ text-align: center;
43
+ margin: 2rem 0;
44
+ }
45
+
46
+ .sidebar-section {
47
+ background: #ffffff;
48
+ padding: 1rem;
49
+ border-radius: 10px;
50
+ margin: 1rem 0;
51
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
52
+ }
53
+ </style>
54
+ """, unsafe_allow_html=True)
55
+
56
+ def create_qr_code(data, fill_color, back_color, border, box_size, error_correction, style_type):
57
+ """Generate QR code with custom styling"""
58
+
59
+ # Error correction levels
60
+ error_levels = {
61
+ 'L (~7%)': qrcode.constants.ERROR_CORRECT_L,
62
+ 'M (~15%)': qrcode.constants.ERROR_CORRECT_M,
63
+ 'Q (~25%)': qrcode.constants.ERROR_CORRECT_Q,
64
+ 'H (~30%)': qrcode.constants.ERROR_CORRECT_H
65
+ }
66
+
67
+ # Create QR code instance
68
+ qr = qrcode.QRCode(
69
+ version=1,
70
+ error_correction=error_levels[error_correction],
71
+ box_size=box_size,
72
+ border=border,
73
+ )
74
+
75
+ qr.add_data(data)
76
+ qr.make(fit=True)
77
+
78
+ # Style configurations
79
+ if style_type == "Standard":
80
+ img = qr.make_image(fill_color=fill_color, back_color=back_color)
81
+ elif style_type == "Rounded":
82
+ img = qr.make_image(
83
+ image_factory=StyledPilImage,
84
+ module_drawer=RoundedModuleDrawer(),
85
+ fill_color=fill_color,
86
+ back_color=back_color
87
+ )
88
+ elif style_type == "Circular":
89
+ img = qr.make_image(
90
+ image_factory=StyledPilImage,
91
+ module_drawer=CircleModuleDrawer(),
92
+ fill_color=fill_color,
93
+ back_color=back_color
94
+ )
95
+ elif style_type == "Gradient":
96
+ img = qr.make_image(
97
+ image_factory=StyledPilImage,
98
+ color_mask=RadialGradiantColorMask(),
99
+ fill_color=fill_color,
100
+ back_color=back_color
101
+ )
102
+
103
+ return img
104
+
105
+ def get_download_link(img, filename):
106
+ """Generate download link for the QR code"""
107
+ buffered = io.BytesIO()
108
+ img.save(buffered, format="PNG")
109
+ img_str = base64.b64encode(buffered.getvalue()).decode()
110
+ href = f'<a href="data:image/png;base64,{img_str}" download="{filename}" class="download-btn">πŸ“₯ Download QR Code</a>'
111
+ return href
112
+
113
+ # Main header
114
+ st.markdown("""
115
+ <div class="main-header">
116
+ <h1>🎨 Beautiful QR Code Generator</h1>
117
+ <p>Create stunning, customizable QR codes with advanced styling options</p>
118
+ </div>
119
+ """, unsafe_allow_html=True)
120
+
121
+ # Sidebar for customization
122
+ with st.sidebar:
123
+ st.markdown("## πŸŽ›οΈ Customization Panel")
124
+
125
+ # QR Code Content
126
+ st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
127
+ st.markdown("### πŸ“ Content")
128
+
129
+ content_type = st.selectbox(
130
+ "Content Type",
131
+ ["Text", "URL", "Email", "Phone", "SMS", "WiFi"]
132
+ )
133
+
134
+ if content_type == "Text":
135
+ qr_data = st.text_area("Enter your text:", height=100)
136
+ elif content_type == "URL":
137
+ qr_data = st.text_input("Enter URL:", placeholder="https://example.com")
138
+ elif content_type == "Email":
139
+ email = st.text_input("Email address:")
140
+ subject = st.text_input("Subject (optional):")
141
+ body = st.text_area("Message (optional):", height=80)
142
+ qr_data = f"mailto:{email}?subject={subject}&body={body}" if email else ""
143
+ elif content_type == "Phone":
144
+ qr_data = st.text_input("Phone number:", placeholder="+1234567890")
145
+ if qr_data:
146
+ qr_data = f"tel:{qr_data}"
147
+ elif content_type == "SMS":
148
+ phone = st.text_input("Phone number:", placeholder="+1234567890")
149
+ message = st.text_area("Message:", height=80)
150
+ qr_data = f"sms:{phone}?body={message}" if phone else ""
151
+ elif content_type == "WiFi":
152
+ ssid = st.text_input("Network Name (SSID):")
153
+ password = st.text_input("Password:", type="password")
154
+ security = st.selectbox("Security Type", ["WPA", "WEP", "nopass"])
155
+ hidden = st.checkbox("Hidden Network")
156
+ if ssid:
157
+ qr_data = f"WIFI:T:{security};S:{ssid};P:{password};H:{'true' if hidden else 'false'};;"
158
+ else:
159
+ qr_data = ""
160
+
161
+ st.markdown('</div>', unsafe_allow_html=True)
162
+
163
+ # Style Settings
164
+ st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
165
+ st.markdown("### 🎨 Style Settings")
166
+
167
+ style_type = st.selectbox(
168
+ "QR Style",
169
+ ["Standard", "Rounded", "Circular", "Gradient"]
170
+ )
171
+
172
+ col1, col2 = st.columns(2)
173
+ with col1:
174
+ fill_color = st.color_picker("Foreground Color", "#000000")
175
+ with col2:
176
+ back_color = st.color_picker("Background Color", "#FFFFFF")
177
+
178
+ st.markdown('</div>', unsafe_allow_html=True)
179
+
180
+ # Advanced Settings
181
+ st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
182
+ st.markdown("### βš™οΈ Advanced Settings")
183
+
184
+ box_size = st.slider("Module Size", 1, 20, 10)
185
+ border = st.slider("Border Width", 1, 10, 4)
186
+ error_correction = st.selectbox(
187
+ "Error Correction Level",
188
+ ["L (~7%)", "M (~15%)", "Q (~25%)", "H (~30%)"]
189
+ )
190
+
191
+ st.markdown('</div>', unsafe_allow_html=True)
192
+
193
+ # Main content area
194
+ col1, col2 = st.columns([2, 1])
195
+
196
+ with col1:
197
+ if qr_data:
198
+ try:
199
+ # Generate QR code
200
+ qr_img = create_qr_code(
201
+ qr_data, fill_color, back_color, border,
202
+ box_size, error_correction, style_type
203
+ )
204
+
205
+ # Display QR code
206
+ st.markdown("### πŸ“± Your QR Code")
207
+ st.image(qr_img, caption="Generated QR Code", use_container_width=True)
208
+
209
+ # Download section
210
+ st.markdown("""
211
+ <div class="download-section">
212
+ <h3>πŸš€ Ready to Download!</h3>
213
+ <p>Your QR code is ready. Click below to download it as PNG.</p>
214
+ </div>
215
+ """, unsafe_allow_html=True)
216
+
217
+ # Download button
218
+ filename = f"qrcode_{content_type.lower()}.png"
219
+ st.markdown(get_download_link(qr_img, filename), unsafe_allow_html=True)
220
+
221
+ except Exception as e:
222
+ st.error(f"Error generating QR code: {str(e)}")
223
+ else:
224
+ st.info("πŸ‘ˆ Please enter content in the sidebar to generate your QR code")
225
+
226
+ with col2:
227
+ st.markdown("### πŸ“‹ Features")
228
+
229
+ features = [
230
+ "🎨 Multiple visual styles",
231
+ "🌈 Custom colors",
232
+ "πŸ“± Various content types",
233
+ "βš™οΈ Advanced settings",
234
+ "πŸ“₯ Instant download",
235
+ "πŸ”§ Error correction levels"
236
+ ]
237
+
238
+ for feature in features:
239
+ st.markdown(f'<div class="feature-box">{feature}</div>', unsafe_allow_html=True)
240
+
241
+ st.markdown("### πŸ’‘ Tips")
242
+ st.info("""
243
+ **Higher Error Correction** = More resilient to damage but larger QR code
244
+
245
+ **Larger Module Size** = Easier to scan but bigger file
246
+
247
+ **Good Contrast** = Better scanning reliability
248
+ """)
249
+
250
+ # Footer
251
+ st.markdown("---")
252
+ st.markdown("""
253
+ <div style="text-align: center; color: #666; padding: 2rem;">
254
+ <p>πŸš€ Built with Streamlit | πŸ’ Made with love for QR code enthusiasts</p>
255
+ </div>
256
+ """, unsafe_allow_html=True)