File size: 1,664 Bytes
9fa6f15
 
 
3f5a8e6
9fa6f15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f5a8e6
 
729e9f3
9fa6f15
 
 
 
 
 
 
729e9f3
9fa6f15
 
 
 
 
 
 
978660b
729e9f3
9fa6f15
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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.")