How to download file in streamlit

Hi, if I make an excel file downloadable and if my excel downloadable file values are in float of 4 decimal place, how to make it to 2 decimal place? i.e while download the file values should come with 2 decimal places.

import streamlit as st
import base64
from io import BytesIO

def to_excel(df):
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    df.to_excel(writer, index = False, sheet_name='Sheet1')
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']
    format1 = workbook.add_format({'num_format': '0.00'}) # Tried with '0%' and '#,##0.00' also.
    worksheet.set_column('A:A', None, format1) # Say Data are in column A
    writer.save()
    processed_data = output.getvalue()
    return processed_data

def get_table_download_link(df):
    """Generates a link allowing the data in a given panda dataframe to be downloaded
    in:  dataframe
    out: href string
    """
    val = to_excel(df)
    b64 = base64.b64encode(val)  # val looks like b'...'
    return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="Your_File.xlsx">Download Excel file</a>' # decode b'abc' => abc

st.markdown(get_table_download_link(df), unsafe_allow_html=True)

df = my dataframe

Position
5.7680
2.7680
5.7680
5.7680
7.2680
5.7680
5.7680
5.7680
5.7680
5.7680
5.7680
5.7680
5.9680
5.7680
2.7680
5.7680
5.7680
5.7680
5.7680
5.7680
1 Like