{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# File Download Example\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import panel as pn\n", "from io import BytesIO\n", "\n", "pn.extension(sizing_mode=\"stretch_width\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Source: Pandas DataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data=pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Target: `.csv`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_csv():\n", " return BytesIO(data.to_csv().encode())\n", "\n", "file_download_csv = pn.widgets.FileDownload(filename=\"data.csv\", callback=get_csv, button_type=\"primary\")\n", "file_download_csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Target: `.csv.zip`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_csv_zip():\n", " output = BytesIO()\n", " output.name = \"data.csv\"\n", " data.to_csv(output, compression=\"zip\")\n", " output.seek(0)\n", " return output\n", "\n", "file_download_csv_zip = pn.widgets.FileDownload(filename=\"data.csv.zip\", callback=get_csv_zip, button_type=\"primary\")\n", "file_download_csv_zip" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Target: `.xlsx`\n", "\n", "Please note you need to install one of the packages\n", "\n", "- [XlsxWriter](https://xlsxwriter.readthedocs.io/index.html) or\n", "- [OpenPyXL](https://openpyxl.readthedocs.io/en/stable/)\n", "\n", "to be able to use the `.to_excel` method of a DataFrame." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_xlsx():\n", " output = BytesIO()\n", " writer = pd.ExcelWriter(output, engine='xlsxwriter')\n", " data.to_excel(writer, sheet_name=\"Data\")\n", " writer.save() # Important!\n", " output.seek(0) # Important!\n", " return output\n", "\n", "file_download_xlsx = pn.widgets.FileDownload(filename=\"data.xlsx\", callback=get_xlsx, button_type=\"primary\")\n", "file_download_xlsx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Target: `.json`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_json():\n", " return BytesIO(data.to_json(orient=\"records\").encode())\n", "\n", "file_download_json = pn.widgets.FileDownload(filename=\"data.json\", callback=get_json, button_type=\"primary\")\n", "file_download_json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Target: `.parquet`\n", "\n", "Please note you need to have the `pyarrow` package installed for this to work." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_parquet():\n", " output = BytesIO()\n", " output.name = \"data.parquet\"\n", " data.to_parquet(output)\n", " output.seek(0)\n", " return output\n", "\n", "file_download_parquet = pn.widgets.FileDownload(filename=\"data.parquet\", callback=get_parquet, button_type=\"primary\")\n", "file_download_parquet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contributions\n", "\n", "Example Contributions are very welcome. For example for `DataFrame` to `html` or `pdf`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## App\n", "\n", "Lets wrap it into nice template that can be served via `panel serve file_download_examples.ipynb`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.template.FastListTemplate(\n", " site=\"Panel\", \n", " title=\"File Download\", \n", " main=[\n", " \"This app demonstrates how to **download a Pandas DataFrame** using different formats.\",\n", " pn.Column(\n", " pn.pane.HTML(\"
🐼
\", height=75, margin=(50,5,10,5)),\n", " file_download_csv, file_download_csv_zip, file_download_xlsx, file_download_json, file_download_parquet),\n", " ], main_max_width=\"768px\",\n", ").servable();" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }