How to download file in streamlit

It works, appreciate for your kindly help.

you are very welcome :wink: I’m glad that it works for you now

For me, it is working perfectly with ppt file! Thanks a lot!

didnt’ worked. Instead showed this bunch of data from my dataframe and no button to be clicked and download…

1 1 0.223607 2 2 0.087706 3 3 0.111803 4 4 0.141421 5 5 0.182574 6 6 0.223607 7 7 0.182574 8 8 0.119523 9 9 0.087706 10 10 0.223607 11 11 0.111803 12 12 0.141421 13 13 0.182574 14 14 0.119523 15 15 0.100000 16 16 0.076696 17 17 0.141421 18 18 0.091287 19 19 0.081650 20 20 0.111803.csv" class= "" id="efbcacbabbfddfcee" href="data:file/txt;base64,U2FtcGxlcyxDZyBJbmRleA0KMSwwLjIyMzYwNjc5Nzc0OTg2NDU4DQoyLDAuMDg3NzA1ODAxOTMwNzAzNg0KMywwLjExMTgwMzM5ODg3NDk3Njk5DQo0LDAuMTQxNDIxMzU2MjM3Mjg3MzgNCjUsMC4xODI1NzQxODU4MzUwOTE3DQo2LDAuMjIzNjA2Nzk3NzUwMDIzNDgNCjcsMC4xODI1NzQxODU4MzQ5NjE5Ng0KOCwwLjExOTUyMjg2MDkzMzQxNDYyDQo5LDAuMDg3NzA1ODAxOTMwNzAxMg0KMTAsMC4yMjM2MDY3OTc3NDk4NjQ1OA0KMTEsMC4xMTE4MDMzOTg4NzQ5NjcwNg0KMTIsMC4xNDE0MjEzNTYyMzcyODczOA0KMTMsMC4xODI1NzQxODU4MzUwOTE3DQoxNCwwLjExOTUyMjg2MDkzMzQyNjczDQoxNSwwLjA5OTk5OTk5OTk5OTk4NDM3DQoxNiwwLjA3NjY5NjQ5ODg4NDczNDY4DQoxNywwLjE0MTQyMTM1NjIzNzI4NzM4DQoxOCwwLjA5MTI4NzA5MjkxNzUyMTUzDQoxOSwwLjA4MTY0OTY1ODA5Mjc2OTUNCjIwLDAuMTExODAzMzk4ODc0OTY3MDQNCg==">Donwload Spreedsheet

Did you write this code in the place where you want to have the button?

    tmp_download_link = download_button(df_to_save.reset_index(), f'{file_name}.csv',
                                            button_text='Click here to download your text!')
    st.markdown(tmp_download_link, unsafe_allow_html=True)

Did you create folder .streamlit in your project and then config.toml in it with following code?

[theme]
# should look like a light theme
primaryColor  = '#f43365'
backgroundColor = '#000000'
secondaryBackgroundColor = '#f1f3f6'
textColor = '#000000'
font = 'sans serif'

And the code that is describing the button from here?

I don’t understand what do you mean

@czubert
I didn’t know was need to create this folder. But now i did and still didn’t worked.

12

and also put the code from here streamlit download button ($2106399) · Snippets · Snippets · GitLab and the part where i want located the donwload_button at the main code:

        tmp_download_link = download_button(df4,f'{cg_result}.csv', button_text='Donwload Cg Dataframe')
        st.markdown(tmp_download_link, unsafe_allow_html=True)

where:

df4 is the dataframe i want to able the user donwload
cg_result the filename i want to show to the user

you have file “config.toml.txt” and it should be “config.toml”

Try it

didn’t worked!

121221

Button code:

tmp_download_link = download_button(df4,f'{file_name}.csv', button_text='Donwload Cg Dataframe')
st.markdown(tmp_download_link, unsafe_allow_html=True)

returned the following error. Seems like where the file_name (which is a string) should be the name of the file and is getting a problem, idk why.

it is not a string, when you use {} inside of f ’ ’ string it is a variable that becomes a string it might be also an int

for example
file_name = 2312312313

print(f"{file_name}.csv")

prints “2312312313.csv”

filename is a variable that you should define earlier, or just write the name of the file without curly braces :slight_smile:

Worked as expected :grin:. BTW how can i change the button colors and put the same style as streamlit standard button have in light mode?

1 Like

in the snippet, part of the code looks like this:


    prim_color = st.config.get_option('theme.primaryColor') or '#F43365'
    bg_color = st.config.get_option('theme.backgroundColor') or '#000000'
    sbg_color = st.config.get_option('theme.secondaryBackgroundColor') or '#f1f3f6'
    txt_color = st.config.get_option('theme.textColor') or '#000000' 
    font = st.config.get_option('theme.font') or 'sans serif'  

st.config.get… takes colors from the file ‘config.toml’, so if you define colors in config toml, your button will get the same colors as the rest of the layout (there are some differences in streamlit, and for example upload files button have different colors from other buttons)

Ok, but when i do some changes at colors didn’t bring the same expect as i have in streamlit stardand style button (also the border color in grey i think)

12121

My donwload button, as you can see, don’t have the border color…

1221212

you need to check which part of this code:

    prim_color = st.config.get_option('theme.primaryColor') or '#F43365'
    bg_color = st.config.get_option('theme.backgroundColor') or '#000000'
    sbg_color = st.config.get_option('theme.secondaryBackgroundColor') or '#f1f3f6'
    txt_color = st.config.get_option('theme.textColor') or '#000000' 
    font = st.config.get_option('theme.font') or 'sans serif'  

is responsible for what. you can only change sets of colors.

primaryColor='#6C9BC0'  # radiobuttons etc
backgroundColor="#FFFFFF"  # Main color
secondaryBackgroundColor="#d3d4db"  # sidebar color
textColor="#262730"
font="sans serif"

Ok, thanks a lot for your help!

No worries :slight_smile: happy to help!

1 Like

@czubert Hi again mate, is there way to able the user download a file with xlsx extension?

yes, you can change “f’{file_name}.csv’” into “f’{file_name}.xlsx” , in that way, you can download xlsx file.

@BeyondMyself I tried this but when i press the button to download, and go to excel to open the file, i get a error saying the file are corrupted or are in different extension

tmp_download_link = download_button(cg_result_dataframe, f'cg_result_dataframe.xlsx', button_text='Donwload Cg Dataframe 💾')

and the code @czubert make available, only let the user download the file in csv or txt extension. As shows here:

 """
    Generates a link to download the given object_to_download.
    Params:
    ------
    object_to_download:  The object to be downloaded.

    download_filename (str): filename and extension of file. e.g. mydata.csv,
    some_txt_output.txt 

    download_link_text (str): Text to display for download
    link.

    button_text (str): Text to display on download button (e.g. 'click here to download file')

    pickle_it (bool): If True, pickle file.
    Returns:
    -------
    (str): the anchor tag to download object_to_download
    Examples:
    --------
    download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
    download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')
    """