Skip to main content

Streamlit component to download a file programmatically, without user input.

Project description

Streamlit Downloader

A Streamlit component that triggers an automatic download of one or more files without requiring user interaction.

Installation

pip install streamlit-downloader

Usage

Use the downloader function to initiate a download. The component will automatically trigger the browser's download dialog when the page loads or when the component is rendered.

Downloading a Single File

import streamlit as st
from streamlit_downloader import downloader

# The `downloader` function returns True when the download is complete.
st.subheader("Download a single file")

data_to_download = b'Hello, this is the content of the file.'
filename = "my_text_file.txt"
content_type = "text/plain"

if st.button("Start Download"):
    download_complete = downloader(
        data=data_to_download,
        filename=filename,
        content_type=content_type
    )

    if download_complete:
        st.success(f"File '{filename}' has been downloaded!")

Downloading Multiple Files

The downloader component can handle multiple files by being called multiple times.

import streamlit as st
from streamlit_downloader import downloader

st.subheader("Download multiple files")

# File 1 details
data_file1 = b"This is the first file."
filename_file1 = "file1.txt"
content_type_file1 = "text/plain"

# File 2 details
data_file2 = b"This is the second file."
filename_file2 = "file2.csv"
content_type_file2 = "text/csv"

if st.button("Download Both Files"):
    # The component must be called for each file you want to download.
    download_complete_1 = downloader(
        data=data_file1,
        filename=filename_file1,
        content_type=content_type_file1,
    )
    
    download_complete_2 = downloader(
        data=data_file2,
        filename=filename_file2,
        content_type=content_type_file2,
    )

    if download_complete_1 and download_complete_2:
        st.success("All files have been downloaded!")

Parameters

The downloader function accepts the following parameters:

  • data: The raw data of the file to be downloaded. This can be a string, bytes, or a similar data type.
  • filename: The name the downloaded file will have.
  • content_type: The MIME type of the file (e.g., text/csv, application/pdf).
  • key: (Optional) A unique key for the component, required when you have multiple downloaders on the same page.

How It Works

The component leverages the browser's ability to trigger downloads using a hidden link element. When the Streamlit component is rendered, it uses JavaScript to create a Blob from the data, creates a temporary <a> element, sets its href to a URL created from the Blob, and simulates a click to initiate the download. The component is visually hidden to provide a seamless user experience.


Development

To run the example app and test changes:

  1. Navigate to the streamlit_downloader directory.
  2. Run streamlit run example.py.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_downloader-0.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_downloader-0.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_downloader-0.0.1.tar.gz.

File metadata

  • Download URL: streamlit_downloader-0.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for streamlit_downloader-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8a7c5d0f671d93527d09e2054cf17a44d602cf8d3dd6baf9ae8c5422d1fc5329
MD5 f85688985c77bb18b0ab1505ae4f0cad
BLAKE2b-256 afecfcee01b0da32c28120486314647e73c2b0bbe6ebc043c93b3a381afd76b4

See more details on using hashes here.

File details

Details for the file streamlit_downloader-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_downloader-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 94faeaf58f752aabfe545e8b04521f4819fd5dec5117ac5659ca04f127b07918
MD5 2a50b41ca6d22adbadf3f9962ffe8d85
BLAKE2b-256 94092f03792e97c02d23623334eba73d6313a3e7c9784b870657900c1996d70d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page