python - 在 plotly dash 中,如何将本地镜像文件提供给 dash.html.Img?

标签 python html image plotly

这是将本地镜像文件来源到 <img> 的方法html 中的元素:

<html>
    <h1>This is an image</h1>
    <img src="file:///C:/Users/MyUser/Desktop/Plotly_Dash_logo.png" alt="image"></img>
</html>

这会按预期显示图像。但是当我尝试使用 plotly dash 包装元素制作相同的页面时,它不起作用:

import dash
from dash import html, dcc

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H1('This is an image'),
    html.Img(src=r'file:///C:/Users/MyUser/Desktop/Plotly_Dash_logo.png', alt='image'),
    ])

if __name__ == '__main__':
    app.run_server(host='0.0.0.0', port=8080, debug=False, use_reloader=False)

本地镜像文件不显示。但是如果我用来自互联网的文件替换源代码,比如 'https://rapids.ai/assets/images/Plotly_Dash_logo.png' , 它工作得很好。

这是怎么回事?

最佳答案

经过更多搜索,我发现我可以将我的图像文件放在一个名为“assets/”的文件夹中,然后相对于应用程序文件夹引用它。

html.Img(src=r'assets/Plotly_Dash_logo.png', alt='image')

我还可以使用应用实例 dash.Dash.get_asset_url() 的特殊方法。

html.Img(src=app.get_asset_url('my-image.png'))

来源:https://dash.plotly.com/dash-enterprise/static-assets

关于python - 在 plotly dash 中,如何将本地镜像文件提供给 dash.html.Img?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72323871/

相关文章:

java - 如何导出带有图像和声音的可运行 Jar?

javascript - 使用ajax时Django不渲染数据

python - 使用 pip 与 apt-get 安装软件包有什么区别?

ios - 代码的特定部分未在 iOS 中执行

javascript - 尝试用 html5 canvas 绘制一个分成几个部分的图案

image - 在 asp.net mvc 4 项目中引用图像的正确方法是什么?

html - 社交图标在移动设备中位于中心,在桌面设备中位于右侧

python - 将日期时间字段转换为特定时区

python - Matplotlib/SNS : plot histogram but with average of another variable on the y-axis

javascript - Selenium 网络驱动程序 : Element is not currently visible