Downloading dynamically generated files from a Dash/Flask app

后端 未结 2 1736
抹茶落季
抹茶落季 2020-12-29 13:22

I tried to build a minimal example of a Dash app that illustrates the problem of dynamically generating a file that can then be downloaded via a download button.

If

2条回答
  •  清酒与你
    2020-12-29 13:52

    Solution here:

    import uuid
    
    import dash
    from dash.dependencies import Input, Output, State
    import flask
    from flask.helpers import send_file
    
    import dash_core_components as dcc
    import dash_html_components as html
    
    stylesheets = [
        "https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css", # Bulma
    ]
    
    server = flask.Flask('app')
    
    # create app
    app = dash.Dash(
        __name__,
        external_stylesheets=stylesheets, 
        server=server                       # <-- do not forget this line
    )
    
    # (...) your code here
    
    @server.route("/downloadable/")
    def download_file (path = None):
        return send_file("downloadable/" + path, as_attachment=True)
    

提交回复
热议问题