I have a dash app, where a Print button executes a window print in Javascript using a client-side callback. I would like the Print button to trigger a python call-back BEFORE it triggers the Javascipt window-print so that I can hide a layout feature and prevent it from being displayed. No matter what I try, the javascript print window opens first and only on closing that window does the other python call-back get triggered. Is there a work around? Thx
dbc.Button('Print Record', id='printing', outline=True, className="d-print-none",
n_clicks=0,)
@app.callback(
[,
Output("printing", "n_clicks"),
],
[Input("printing", "n_clicks")],
)
def toggle_sidebarnclick(n):
"code toggles a layout feature so it doesn't appear on the screen"
return n
app.clientside_callback(
"""
function(clicks) {
if (clicks > 0) {
try {
document.execCommand('print', false, null);
}
catch(e) {
window.print();
}
}
return
}
""",
Output('hidden-content', 'children'),
Input('printing', 'n_clicks')
)
The solution is simple:
{
setTimeout(window.print, 1000)
}