I am building out a dash app and have attempted to include print and export csv buttons to the app using python and js.
I don’t receive any traceback(s) or any indication there is an issue with the code, but the buttons create no effect when clicked. I have unsuccessfully solved this issue with the below code, and I am unsure how to remedy the situation. Added a few notes below to assist w/understanding context. Any assistance would be greatly appreciated!
################# These are the libraries I have imported. #################
from dash import Dash, html, dash_table, dcc, callback, callback_context, Output, Input, State
import plotly.express as px
import pandas as pd
import numpy as np
import csv
# This is indented due to its location in overall program.
# Print/save button(s) layout.
html.Button('Export .csv', id = 'save-btn',
style = {'display':'inline', 'fontSize': 14, 'color':colors['text'],
'backgroundColor': '#3385ff', 'border':'2px solid black',
'margin':'4px', 'borderRadius':0}),
html.Button('Print PDF', id='print-btn', n_clicks=0,
style = {'display':'inline', 'fontSize': 14, 'color':colors['text'],
'backgroundColor': '#3385ff', 'border':'2px solid black',
'margin':'4px', 'borderRadius':0}),
dcc.ConfirmDialog(
id='confirm',
message='Do you want to print?',
),
################# Included callbacks below. #################
# Save callback
@callback(
Output('save-button', 'n_clicks'),
Input('save-button', 'n_clicks'),
State('table', 'data')
)
def save_data(n_clicks, rows):
if n_clicks is not None and n_clicks > 0:
with open('data.csv', 'w', newline = '') as f:
writer = csv.DictWriter(f, fieldnames = rows[0].keys())
writer.writeheader()
writer.writerows(rows)
return n_clicks
# Print callback
@callback(
Output('print-button', 'n_clicks'),
Input('confirm', 'submit_n_clicks')
)
def print_data(n_clicks):
if n_clicks is not None and n_clicks > 0:
print_function = """
<script type="text/javascript">
window.print();
</script>
"""
return dcc.Markdown(print_function, dangerously_allow_html = True)
return n_clicks