I have a bokeh app along the lines of stock_app.py. My users have requested the ability to get a download of some of the data used in the plots. I’m not too familiar with Flask, but it seems that downloading a file goes something like this (assuming the existence of a DataFrame called “df”):
from flask import Flask, make_response
app = Flask(name)
@app.route(’/download’)
def download():
dfbuffer = StringIO()
df.to_csv(dfbuffer, encoding=‘utf-8’, index=False)
dfbuffer.seek(0)
values = dfbuffer.getvalue()
output = make_response(values)
output.headers[“Content-Disposition”] = ‘download.csv’
output.headers[“Content-type”] = “text/csv”
return output
app.debug = True
app.run()
``
In my naiveté, I tried including a method in my Bokeh app that looks like this (set as a callback on a Button widget):
def download_csv(self, obj, attr, old, new):
dfbuffer = StringIO()
df = self.df
df.to_csv(dfbuffer, encoding=‘utf-8’, index=False)
dfbuffer.seek(0)
values = dfbuffer.getvalue()
output = make_response(values)
output.headers[“Content-Disposition”] = ‘download.csv’
output.headers[“Content-type”] = “text/csv”
return output
``
When I click the button, I get no errors, but also no download, so that’s clearly not the right way to do it. Is there a way to let a user download a csv file through a Bokeh app?
