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""" {title} """, 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()