我正在开发一个绘图破折号应用程序,其中显示许多本地图像。我希望能够以更大的尺寸显示它们,因此我尝试使用 clientside_callback 打开一个新窗口。下面显示的代码运行良好,除非我更改图像的 url;然后会触发以下错误:
Cannot read properties of undefined (reading 'apply')
如果我重新加载页面,错误就会消失,但这是我想避免的。你有什么建议吗?
MWE:
import dash
from dash import dcc, html, Input, Output, State
app = dash.Dash(__name__)
image_url = 'https://source.unsplash.com/user/c_v_r/1900x800'
# image_url = 'https://source.unsplash.com/user/c_v_r/100x100'
app.layout = html.Div([
html.Button('Show Image in New Window', id='show-image-button'),
html.Div(id='hidden-content', style={'display': 'none'}),
])
app.clientside_callback(
"""
function(n_clicks) {
if (n_clicks > 0) {
var imageUrl = '%s';
var myWindow = window.open("", "", "width=600,height=400");
myWindow.document.write("<img src='" + imageUrl + "' alt='Image'>");
myWindow.focus();
}
return "";
}
""" % image_url,
Output('hidden-content', 'children'),
Input('show-image-button', 'n_clicks'),
prevent_initial_call=True
)
if __name__ == "__main__":
app.run_server(debug=True, port=8051)
# Error when changing image_url: Cannot read properties of undefined (reading 'apply')
SO 上有一篇类似的帖子,但我不知道所提供的答案如何(以及是否)适用于我的案例。谢谢