Downloading a PDF

I am trying to download a PDF. I am doing this:

@app.server.route('/dash/urlToDownload')
def download_file(file):
    return send_file(file,
                     mimetype='application/pdf',
                     attachment_filename='report.pdf',
                     as_attachment=True)

It blows up with:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.5/dist-packages/dash/dash.py", line 556, in dispatch
    return self.callback_map[target_id]['callback'](*args)
  File "/usr/local/lib/python3.5/dist-packages/dash/dash.py", line 524, in add_context
    cls=plotly.utils.PlotlyJSONEncoder),
  File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/local/lib/python3.5/dist-packages/plotly/utils.py", line 136, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python3.5/dist-packages/plotly/utils.py", line 204, in default
    return _json.JSONEncoder.default(self, obj)
  File "/usr/lib/python3.5/json/encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Response streamed [200 OK]> is not JSON serializable

Anyone know what the issue is and how I can fix it?

Hi, and thanks for posting.

Do you mind creating a toy app that reproduces this issue? From the code I’m not sure where the error is coming from. I’m not sure why send_file would give a JSON error, making me think something else might be going on.

I will try and create an example program, but we decided to move from dash/flask to django. dash/flask just does not seem robust and full featured enough for our app. But I would still like to solve this, as I do have to support and probably enhance the existing app until the new one is ready. I traced the code to all the way through to flask/helpers.py(602)send_file and I can see that it returns an object who’s str method yields <Response streamed [200 OK]> so for some reason the dash code is trying to serialize that.

Interestingly, if I use a csv file I create manually it works. In the failure case the file was the output of wkhtmltopdf which got its data from a website.