How to add download pptx from link

Hi All,

I am adding click functionality to download pptx file from given link but every time it is creating a blank pptx file below is sample code:

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.exceptions import PreventUpdate
from dash.dependencies import Input, Output
from os import path
from flask import send_file
from pptx import Presentation
import io

app = dash.Dash(__name__,external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = html.Div([html.Div([html.A("Download PPT",href="/download_ppt/")],id = 'excel')])

#download cluster report
@app.server.route("/download_ppt/")
def download_excel():
    if path.exists("insights.pptx"):
        # Create DF
        f=open('insights.pptx','rb')
        prs=Presentation(f)
        f.close()
        #filename = io.BytesIO()
        filename = io.StringIO()         
        prs.save(filename)
        filename.seek(0)
        return send_file(
            filename,
            mimetype = 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 
            attachment_filename="download.pptx",
            as_attachment=True,
        )
    else:
        raise PreventUpdate

if __name__ == "__main__":
    app.run_server()

Have you tried the Download component from dash_extensions? Here is a small example demonstrating how to download files.

import dash
import dash_html_components as html  
from dash.dependencies import Output, Input
from dash_extensions import Download
from dash_extensions.snippets import send_file

app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download", id="btn"), Download(id="download")])

@app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
def func(n_clicks):
    return send_file("/home/emher/Documents/Untitled.png")

if __name__ == '__main__':
    app.run_server()
1 Like

Thanks it is working!! Thanks for creating this extension and make our life easy.