html - 如何动态更改破折号中的 html.Button 文本?
问题描述
我尝试了不同的属性(标题、数据、值等),但它们没有成功。下面是直接取自 dash 文档的示例代码。我只是希望能够按下按钮并将按钮文本更改为“提交”之外的其他内容,最好是输入文本。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(dcc.Input(id='input-on-submit', type='text')),
html.Button('Submit', id='submit-val', n_clicks=0),
html.Div(id='container-button-basic',
children='Enter a value and press submit')
])
@app.callback(
Output('container-button-basic', 'children'),
Input('submit-val', 'n_clicks'),
dash.dependencies.State('input-on-submit', 'value')
)
def update_output(n_clicks, value):
return ('The input value was "{}" and the button has been clicked {} times'.format(
value,
n_clicks
))
if __name__ == '__main__':
app.run_server(debug=True)
解决方案
好吧,我发现我需要更新“children”属性。下面是执行我正在寻找的功能的代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(dcc.Input(id='input-on-submit', type='text')),
html.Button('Submit', id='submit-val', n_clicks=0),
html.Div(id='container-button-basic',
children='Enter a value and press submit')
])
@app.callback(
Output('container-button-basic', 'children'),
Output ('submit-val','children'),
Input('submit-val', 'n_clicks'),
dash.dependencies.State('input-on-submit', 'value')
)
def update_output(n_clicks, value):
if n_clicks==0:
return
else:
buttonText = value
return ('The input value was "{}" and the button has been clicked {} times'.format(
value,
n_clicks
), buttonText)
if __name__ == '__main__':
app.run_server(debug=True)
推荐阅读
- python-3.x - Python3 plotly:向连接坐标(经度,纬度)的线添加箭头
- c# - 我们需要在控制器中使用 async/await 关键字吗?
- python - 如何在不重复代码的情况下覆盖多个类(Python)
- java - 任务“:amplify_core:compileDebugJavaWithJavac”执行失败
- java - 如何在安装应用程序时将应用程序的快捷方式添加到主屏幕?(不使用谷歌游戏商店)
- node.js - 如何让我的函数在地图中返回一个值而不是一个待处理的 Promise?
- reactjs - 如何在 chakra UI 中为 css 变量添加自定义颜色
- caching - Varnish 默认宽限行为
- selenium - Selenium Web 驱动程序在调试 UI 测试时将文本写入其他应用程序,而不是写入所需的 Web 表单
- node.js - 如何使用新的 URL API 获取请求详细信息?