title: how to download excel file in python and streamlit?
body: <p>I have a Python script that read files and convert it to dataframe using Python and streamlit. Then I want to create a function to allows the user to download this dataframe as an Excel file with extension <code>.xls</code>.</p>
  <p>So I tried to read the dataframe and convert it to an Excel file using these two functions:</p>
  <pre><code>pd.ExcelWriter
  df.to_excel
  </code></pre>
  <p>But when I try to download the file using a link the file doesn't download and displays this error:</p>
  <pre><code>Failed-Network error
  </code></pre>
  <p>Code:</p>
  <pre><code>import pandas as pd 
  import streamlit as st
  
  writer = pd.ExcelWriter('update2.xlsx')
  df.to_excel(writer, index = False, header=True,encoding='utf-8')
  with open(writer,'rb') as f:
      b64 = base64.b64encode(f.read())
      href = f'&lt;a href=&quot;data:file/xls;base64,{b64}&quot; download=&quot;new_file.{extension}&quot;&gt;Download {extension}&lt;/a&gt;'
  
  st.write(href, unsafe_allow_html=True)
  </code></pre>