A custom streamlit component for performing a page redirect programmatically. This is helpful since it is often required to perform a redirect, for instance after a button click or login. The streamlit-redirect component solves this by inserting a <meta> tag which directly triggers a browser redirect
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamlit_redirect-0.1.0.tar.gz.
File metadata
- Download URL: streamlit_redirect-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.11.4 Linux/5.15.146.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5abfb3d82bd6b5350d06545c33f24649ee60308347d8191e1df83c6710135c9f
|
|
| MD5 |
ac6cd1a1e1f88b0612ed8bb9823a0f60
|
|
| BLAKE2b-256 |
3899ffe786d9119032d6be3ba799f0771943c1aa0110ab06577dc8c61bbbf604
|
File details
Details for the file streamlit_redirect-0.1.0-py3-none-any.whl.
File metadata
- Download URL: streamlit_redirect-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.11.4 Linux/5.15.146.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e929da6f685ab514f52bad14f458ab641f6c3bef88711f499f78f2e31d868826
|
|
| MD5 |
f3773a5033b84a80a7e2ba79bb89ed5b
|
|
| BLAKE2b-256 |
011899131891c9715d32358f228871658fd50649ec7eb2c0f3d29ee7b412cbb3
|