Repository navigation
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
90 lines (71 loc) · 3.16 KB
/
Copy pathstreamlit_app.py
File metadata and controls
90 lines (71 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
QRURLs — Streamlit web app
Deploy for free on Streamlit Community Cloud: https://share.streamlit.io
"""
import io
import streamlit as st
from qrurls import QRGenerator, QRURLs, ShorteningError
from qrurls.exceptions import InvalidURLError
st.set_page_config(page_title="QRURLs", page_icon=":link:", layout="centered")
st.title("QRURLs")
st.markdown("Generate QR codes and shorten URLs — powered by open source services.")
url = st.text_input("Enter a URL", placeholder="https://example.com/your/long/url")
col1, col2 = st.columns(2)
with col1:
action = st.selectbox("Action", ["Shorten + QR Code", "Shorten Only", "QR Code Only"])
with col2:
service = st.selectbox("Service", ["tinyurl", "isgd", "vgd"])
with st.expander("QR Code Options"):
box_size = st.slider("Box size", min_value=5, max_value=20, value=10)
border = st.slider("Border", min_value=1, max_value=10, value=4)
if st.button("Generate", type="primary"):
if not url:
st.warning("Please enter a URL.")
else:
try:
qrurls = QRURLs(service=service, box_size=box_size, border=border)
if action == "Shorten + QR Code":
with st.spinner("Shortening URL and generating QR code..."):
short_url, _ = qrurls.process(url, "qr_output.png")
st.success(f"Shortened URL: **{short_url}**")
# Generate image in memory for display
qr_gen = QRGenerator(box_size=box_size, border=border)
img = qr_gen.generate(short_url)
buf = io.BytesIO()
img.save(buf, format="PNG")
buf.seek(0)
st.image(buf, caption=f"QR code for {short_url}", width=300)
st.download_button(
"Download QR Code",
data=buf.getvalue(),
file_name="qrurls_output.png",
mime="image/png",
)
elif action == "Shorten Only":
with st.spinner("Shortening URL..."):
short_url = qrurls.shorten_only(url)
st.success(f"Shortened URL: **{short_url}**")
st.code(short_url, language=None)
elif action == "QR Code Only":
qr_gen = QRGenerator(box_size=box_size, border=border)
img = qr_gen.generate(url)
buf = io.BytesIO()
img.save(buf, format="PNG")
buf.seek(0)
st.image(buf, caption=f"QR code for {url}", width=300)
st.download_button(
"Download QR Code",
data=buf.getvalue(),
file_name="qrurls_output.png",
mime="image/png",
)
except InvalidURLError:
st.error("Invalid URL. Please enter a valid URL starting with http:// or https://")
except ShorteningError as e:
st.error(f"Shortening failed: {e}")
st.divider()
st.caption(
"Built with [QRURLs](https://github.com/priya-gitTest/qrurls) "
"by [Priyanka O](https://orcid.org/0000-0002-6844-6493) · "
"[Install via PyPI](https://pypi.org/project/qrurls/) · MIT License"
)