如何使用对话框在破折号中获取文本?
How to get text in dash using dialog box?
我想在 javascript 中使用类似提示功能的破折号对话框来询问情节的名称,但是当我使用 {html.Script("prompt('text');", type='text/javascript')
时,我在 return 中什么也没有。
我不想使用文本输入或文本区域。
感谢您的帮助。
Dash 文档在 html.Script
上说了以下内容:
CAUTION: is included for completeness, but you cannot execute JavaScript code by providing it to a element. Use a clientside callback for this purpose instead.
https://dash.plotly.com/dash-html-components/script
正如此处的文档所述,一种选择是改用客户端回调。
展示这个想法的最小例子:
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Div(id="content"),
html.Button(id="button", children="Show name of plot prompt"),
]
)
app.clientside_callback(
"""
function(n_clicks) {
if (n_clicks) {
nameOfPlot = prompt('name of plot')
return nameOfPlot
}
return "Name of plot placeholder"
}
""",
Output("content", "children"),
Input("button", "n_clicks"),
)
Button
点击是这个回调的输入。如果什么都没有被点击,我 return 一个占位符文本设置了一个 div 的 children
属性 和 id content
.
如果按钮被点击,我们可以调用 prompt
并用它做一些事情。
使用这种方法,您需要输入和输出才能使回调正常工作。您还可以将 Javascript 代码放入根应用程序目录中 assets
中的 .js
文件中。有关客户端回调的更多信息,请参阅文档 here.
另一种方法是在您的 assets
文件夹中有一个 .js
文件,并将您的提示代码放在那里。您可能还想设置一些事件侦听器以等待页面加载 and/or 以侦听按钮单击事件或其他可用于在适当时刻触发提示的事件。
我想在 javascript 中使用类似提示功能的破折号对话框来询问情节的名称,但是当我使用 {html.Script("prompt('text');", type='text/javascript')
时,我在 return 中什么也没有。
我不想使用文本输入或文本区域。
感谢您的帮助。
Dash 文档在 html.Script
上说了以下内容:
CAUTION: is included for completeness, but you cannot execute JavaScript code by providing it to a element. Use a clientside callback for this purpose instead.
https://dash.plotly.com/dash-html-components/script
正如此处的文档所述,一种选择是改用客户端回调。
展示这个想法的最小例子:
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Div(id="content"),
html.Button(id="button", children="Show name of plot prompt"),
]
)
app.clientside_callback(
"""
function(n_clicks) {
if (n_clicks) {
nameOfPlot = prompt('name of plot')
return nameOfPlot
}
return "Name of plot placeholder"
}
""",
Output("content", "children"),
Input("button", "n_clicks"),
)
Button
点击是这个回调的输入。如果什么都没有被点击,我 return 一个占位符文本设置了一个 div 的 children
属性 和 id content
.
如果按钮被点击,我们可以调用 prompt
并用它做一些事情。
使用这种方法,您需要输入和输出才能使回调正常工作。您还可以将 Javascript 代码放入根应用程序目录中 assets
中的 .js
文件中。有关客户端回调的更多信息,请参阅文档 here.
另一种方法是在您的 assets
文件夹中有一个 .js
文件,并将您的提示代码放在那里。您可能还想设置一些事件侦听器以等待页面加载 and/or 以侦听按钮单击事件或其他可用于在适当时刻触发提示的事件。