11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

streamlitでダウンロードリンクを作る

Posted at

2021年4月現在、file_uploaderはあるのですがダウンロードボタンはないので自分で作る必要があります。

DataFrameの場合

dfというDataFrameをCSVにしてダウンロードさせます。

import base64
csv = df.to_csv(index=False)  
b64 = base64.b64encode(csv.encode()).decode()
href = f'<a href="data:application/octet-stream;base64,{b64}" download="result.csv">download</a>'
st.markdown(f"ダウンロードする {href}", unsafe_allow_html=True)

汎用化した関数

import os
import base64

def file_downloader(filename, file_label='File'):
    with open(filename, 'rb') as f:
        data = f.read()
    
b64 = base64.b64encode(csv.encode()).decode()
href = f'<a href="data:application/octet-stream;base64,{b64}" download="result.csv">download</a>'
st.markdown(f"ダウンロードする {href}", unsafe_allow_html=True)

参考

How to download file in streamlit

11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?