我有一个日期选择器护林员,它选择2个日期并返回日期范围之间的文件名表。现在,我想添加一个下载按钮来下载所有已返回的文件。我不确定如何添加下载所有功能帮助。我已经创建了显示特定范围内的所有文件的第一部分,但是添加按钮是我的不足之处。
def generate_table(dataframe, max_rows):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr(html.A([html.Td(dataframe.iloc[i][col]) for col in dataframe.columns],
href=link+dataframe.iloc[i][0]))
for i in range(min(len(dataframe), max_rows))] + [html.Button('Download All', id='button-1')]
)
def convert_to_date(dates):
dates = dates.replace("-", "")
dates = datetime.datetime.strptime(dates, '%Y%m%d').date()
return dates
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config['suppress_callback_exceptions']=True
app.layout = html.Div(children=[
html.H4(children='Select a date'),
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=dt(1995, 1, 1),
max_date_allowed=dt(2030, 12, 30),
initial_visible_month=dt.now(),
with_portal=True,
display_format='Do MMM, YYYY',
),
html.Div(id='table-container'),
html.Ul(id='container')
])
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('my-date-picker-range', 'start_date'),
dash.dependencies.Input('my-date-picker-range', 'end_date')])
def files_in_range(start_date, end_date):
dt = df['Date']
DT = []
s = 'advsot_'
e = '.xlsx'
if start_date is not None and end_date is not None:
start_date = convert_to_date(start_date)
end_date = convert_to_date(end_date)
for i in dt:
if start_date <= i <= end_date:
DT.append(s+'{:%Y%m%d}'.format(i)+e)
DT = pd.DataFrame(DT, columns = ['Filename'])
return generate_table(DT, max_rows)
if __name__ == '__main__':
app.run_server(debug=True)