streamlit-redirect
A lightweight Streamlit component for performing page redirects programmatically. This component is helpful when you need to redirect users to external URLs, for instance after a button click, form submission, or successful login.
Features
- 🚀 Simple programmatic redirects in Streamlit apps
- 🎯 Lightweight implementation using HTML meta refresh
- ✅ Easy integration with existing Streamlit workflows
Installation
pip install streamlit-redirect
Quick Start
import streamlit as st
from streamlit_redirect import redirect
st.title("My App")
# Redirect after button click
if st.button("Go to Google"):
redirect("https://www.google.com")
# Redirect based on user input
url = st.text_input("Enter a URL:")
if url:
redirect(url)
API Reference
redirect(url: str)
Redirects the current page to the specified URL.
Parameters:
url(str): The target URL to redirect to. External websites must include the protocol (http://orhttps://)
Note: The redirect function inserts a <meta http-equiv="refresh"> tag in the page header to perform the redirect. Make sure to only use trusted URLs to prevent security issues.
Examples
Basic Button Redirect
import streamlit as st
from streamlit_redirect import redirect
st.title("Welcome to My App")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("🏠 Home", use_container_width=True):
redirect("https://mywebsite.com")
with col2:
if st.button("📧 Contact", use_container_width=True):
redirect("https://mywebsite.com/contact")
with col3:
if st.button("📱 About", use_container_width=True):
redirect("https://mywebsite.com/about")
Conditional Redirect After Form Submission
import streamlit as st
from streamlit_redirect import redirect
st.title("Login Form")
with st.form("login_form"):
username = st.text_input("Username")
password = st.text_input("Password", type="password")
submitted = st.form_submit_button("Login")
if submitted:
# Your authentication logic here
if authenticate_user(username, password):
st.success("Login successful! Redirecting...")
redirect("https://myapp.com/dashboard")
else:
st.error("Invalid credentials")
URL Validator with Redirect
import streamlit as st
from streamlit_redirect import redirect
import re
def is_valid_url(url):
pattern = re.compile(
r'^https?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return pattern.match(url) is not None
st.title("URL Redirector")
url_input = st.text_input(
"Enter a URL to redirect to:",
placeholder="https://example.com"
)
if url_input:
if is_valid_url(url_input):
st.success(f"✅ Valid URL: {url_input}")
if st.button("Redirect Now"):
redirect(url_input)
else:
st.error("❌ Please enter a valid URL with http:// or https://")
Delayed Redirect with Countdown
import streamlit as st
from streamlit_redirect import redirect
import time
st.title("Redirect with Countdown")
if st.button("Start Countdown Redirect"):
placeholder = st.empty()
for i in range(5, 0, -1):
placeholder.info(f"Redirecting to Google in {i} seconds...")
time.sleep(1)
placeholder.success("Redirecting now!")
redirect("https://www.google.com")
Security Considerations
- Always validate URLs before redirecting to prevent malicious redirects
- Only redirect to trusted domains when possible
- Consider implementing a whitelist of allowed domains for production applications
- Be cautious with user-provided URLs
How It Works
The streamlit-redirect component works by injecting a HTML meta refresh tag into the page:
<meta http-equiv="refresh" content="0; url=https://example.com">
This causes the browser to immediately redirect to the specified URL. The component uses Streamlit's st.markdown() with unsafe_allow_html=True to insert this tag.
Requirements
- Python >= 3.11
- Streamlit >= 1.48
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Metadata
Release files for streamlit-redirect 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| streamlit_redirect-0.1.0.tar.gz | 7.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| streamlit_redirect-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / streamlit_redirect-0.1.0.tar.gz
| Download URL | streamlit_redirect-0.1.0.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5abfb3d82bd6b5350d06545c33f24649ee60308347d8191e1df83c6710135c9f
|
|
BLAKE2b-256 checksum How to use checksums |
3899ffe786d9119032d6be3ba799f0771943c1aa0110ab06577dc8c61bbbf604
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.11.4 Linux/5.15.146.1-microsoft-standard-WSL2
|
Release files / streamlit_redirect-0.1.0-py3-none-any.whl
| Download URL | streamlit_redirect-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e929da6f685ab514f52bad14f458ab641f6c3bef88711f499f78f2e31d868826
|
|
BLAKE2b-256 checksum How to use checksums |
011899131891c9715d32358f228871658fd50649ec7eb2c0f3d29ee7b412cbb3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.11.4 Linux/5.15.146.1-microsoft-standard-WSL2
|