Socket
Book a DemoInstallSign in
Socket

streamlit-redirect

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

streamlit-redirect

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

pipPyPI
Version
0.1.0
Maintainers
1

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:// or https://)

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.

Keywords

streamlit

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.