使用 html.Script 将 Dash (Plotly) 连接到 API

Connecting Dash (Plotly) to API with html.Script

希望将内置 dash 的应用程序连接到第三方 API。

连接的html是这样的...

<script src="https://api.website.com/static/api.js" data-app-id="1234" >
</script >

Dash 有一个名为 html.Script 的脚本组件,它替换了原始 html 中的脚本标签。您可以在这里阅读更多相关信息:https://dash.plotly.com/dash-html-components/script

问题在于原始 html api 代码包含唯一属性 data-app-id,以及 Dash 的 html.Script 组件似乎不支持允许在组件中放置 data-app-id="1234" 的关键字参数。

想知道如何将 data-app-id="1234" 放在那里吗?

您实际上可以通过设置 Dashexternal_scripts 属性 来做到这一点:

external_scripts = [
    {
        "src": "https://api.website.com/static/api.js",
        "data-app-id": "1234",
    },
]

app = dash.Dash(__name__, external_scripts=external_scripts)

查看文档 here 了解更多信息。


要验证它是否正常工作:运行 您的应用程序并在浏览器中查看页面源代码,您会看到它显示:

<script src="https://api.website.com/static/api.js" data-app-id="1234"></script>