import streamlit as st import requests from io import BytesIO import os # Streamlitアプリのタイトル st.title("Web Content Downloader") # [Client] URLを指定 url_to_download = st.text_input("Enter the URL to download:") # [Server] URLの内容をGet if url_to_download: response = requests.get(url_to_download) if response.status_code == 200: st.success("Content successfully fetched from the URL.") else: st.error(f"Failed to fetch content from the URL. Status code: {response.status_code}") # [Server] URLをダウンロード可能にする if url_to_download and response.status_code == 200: # URLからファイル名を抽出 file_name = os.path.basename(url_to_download) # ダウンロードリンクを生成 st.markdown( f"**[Download Content]({url_to_download})** - Right-click and choose 'Save link as...' to download" ) # ダウンロードした内容をサーバーサイドで保存 file_bytes = BytesIO(response.content) st.session_state["downloaded_file"] = {"file_bytes": file_bytes, "file_name": file_name} # [Client] サーバーサイドでダウンロードした内容をダウンロードする if st.button("Download Content"): if "downloaded_file" in st.session_state: downloaded_file = st.session_state["downloaded_file"] st.download_button( label="Click to download", data=downloaded_file["file_bytes"].read(), file_name=downloaded_file["file_name"], key="download_button", ) else: st.warning("No content available for download. Please fetch content from a URL.")