whitphx's picture
whitphx HF Staff
Duplicate from cansik/streamlit-download-experiment
212dc54
import base64
import os
from typing import Any
import streamlit as st
import streamlit.components.v1 as components
def is_on_huggingface() -> bool:
return os.environ.get("SPACE_REPO_NAME") is not None
def download_link(title: str, data: Any, file_name: str, mime_type: str):
b64_bytes = base64.urlsafe_b64encode(data)
b64_string = b64_bytes.decode()
file_string = f"data:{mime_type};base64,{b64_string}"
components.html(f"""
<style>
a {{
font-family: "Source Sans Pro", sans-serif;
color: rgb(250, 250, 250);
}}
</style>
<a download="{file_name}" href="{file_string}">{title}</a>
""", height=30)
def main():
if is_on_huggingface():
st.title("Example on HuggingFace")
else:
st.title("Example Local")
with st.form("Generate"):
x = st.slider('Select a value')
submitted = st.form_submit_button("Generate")
if submitted:
st.write("File has been created")
data = bytes(f"value squared: {x}", 'utf-8')
st.download_button("Download", data, "test.txt", "text/plain")
download_link("Download", data, "test.txt", "text/plain")
if __name__ == "__main__":
main()