【问题标题】:Connecting Dash (Plotly) to API with html.Script使用 html.Script 将 Dash (Plotly) 连接到 API
【发布时间】:2021-05-17 18:42:53
【问题描述】:

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

连接的html是这样的……

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

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

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

任何想法如何将 data-app-id="1234" 放在那里?

【问题讨论】:

    标签: plotly-dash


    【解决方案1】:

    您实际上可以通过设置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>
    

    【讨论】:

    • 谢谢,我正在尝试连接到api.memberstack.io/static/memberstack.js,但它不起作用...
    • 也许将协议添加到src(即https)
    • 啊,那个 memberstack.js 文件是本地的。它与您的代码无关。因此,当执行破折号代码时,外部脚本实际上并没有出现在源代码中。
    • 是的,对不起,脚本实际上是用完整的 https URL 编写的。堆栈溢出正在将 URL 转换为这些 cmets 中的链接。
    • 在我将更改的样式表设置为脚本后它工作了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2021-12-24
    • 2019-01-21
    • 2020-11-21
    • 2019-05-16
    • 1970-01-01
    相关资源
    最近更新 更多