File download examples

Download this notebook from GitHub (right-click to download).


File Download Example

The purpose of this notebook is to provide code examples and code snippets that enable you to quickly add FileDownload to your Panel dashboard or application.

This notebook was originally contributed because I had problems creating the right code to enable downloading DataFrames in xlsx format. See FileDownload widget produces empty file for dataframe.

In [ ]:
import pandas as pd
import panel as pn
from io import BytesIO

pn.extension()

Source: DataFrame

File: XLSX

Please note you need to have the packages

installed for being able to use the .to_excel method of a DataFrame.

In [ ]:
data=pd.DataFrame({"a": [1]})

def get_file():
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    data.to_excel(writer, sheet_name="Data")
    writer.save() # Important!
    output.seek(0) # Important!
    return output

file_download = pn.widgets.FileDownload(filename="data.xlsx", callback=get_file)
file_download

Contributions

Example Contributions are very welcome. For example for DataFrame to csv, parquet and json.


Download this notebook from GitHub (right-click to download).