How to download file in streamlit

@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!')
    """

As you may see in the download_button function, there is an if alse statement if picle_it, inside else statement there is another if else statement in which it checks the distance.

        elif isinstance(object_to_download, pd.DataFrame):
            object_to_download = object_to_download.to_csv(index=False)

you can see that there is written that if instance is pd.DataFrame, then you should use method ‘to_csv’
you need to change it to: pd.dataframe.to_excel() for more info check it here:pandas.DataFrame.to_excel — pandas 1.2.4 documentation

I hope it will work for you :slight_smile:

1 Like

I did those changes you said but didn’t worked. Instead, returned the following error:

elif isinstance(object_to_download, pd.DataFrame):
     object_to_download = object_to_download.to_excel(index=False)

i also changed the donwload_button extesion:

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

Try to do this:

hope it will help :slight_smile:

How can I download Image . I didn’t get that.
please anyone can provide code for that. @czubert

I just now saw this thread. Maybe this post from me could be of help?

Did @jgieseler solution work for you? :speak_no_evil: