禁用";提交;用Python+;猛冲
我想阻止用户在下面的脚本中按下“提交”按钮30秒后再按下它。我该怎么做呢?以下是我的代码当前的外观:禁用";提交;用Python+;猛冲,python,plotly-dash,Python,Plotly Dash,我想阻止用户在下面的脚本中按下“提交”按钮30秒后再按下它。我该怎么做呢?以下是我的代码当前的外观: import dash from dash.dependencies import Input, Output, State import dash_core_components as dcc import dash_html_components as html app = dash.Dash() app.layout = html.Div([ dcc.Input(id='my-
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Button('Submit', id='button'),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input('button', 'n_clicks')],
state=[State(component_id='my-id', component_property='value')]
)
def update_output_div(n_clicks, input_value):
return 'You\'ve entered "{}" and clicked {} times'.format(input_value, n_clicks)
if __name__ == '__main__':
app.run_server()
有人知道我如何阻止用户按下按钮30秒吗
先谢谢你
编辑2018年8月15日格林尼治标准时间上午9:30对stevepastelan的回复:
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Button('Submit', id='button'),
html.Div([dcc.Interval(
id='interval-component',
interval=1 * 3000, # in milliseconds
n_intervals=0
)]),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input('button', 'n_clicks')], [Input('interval-component', 'n_intervals')],
state=[State(component_id='my-id', component_property='value')]
)
def update_output_div(n_clicks,n_intervals, input_value):
return 'You\'ve entered "{}" and clicked {} times'.format(input_value, n_clicks)
if __name__ == '__main__':
app.run_server()
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Button('Submit', id='button'),
html.Div([dcc.Interval(
id='interval-component',
interval=1 * 3000, # in milliseconds
n_intervals=0
)]),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input('button', 'n_clicks')], [Input('interval-component', 'n_intervals')],
state=[State(component_id='my-id', component_property='value')]
)
def update_output_div(n_clicks,n_intervals, input_value):
return 'You\'ve entered "{}" and clicked {} times'.format(input_value, n_clicks)
if __name__ == '__main__':
app.run_server()
EDIT 2018年8月15日16:22PM编写了一个带有编辑回调的更简单脚本,但它不起作用:
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Button('Submit', id='button'),
html.Div([dcc.Interval(
id='interval-component',
interval=1 * 3000, # in milliseconds
n_intervals=0
)]),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input('button', 'n_clicks')], [Input('interval-component', 'n_intervals')],
state=[State(component_id='my-id', component_property='value')]
)
def update_output_div(n_clicks,n_intervals, input_value):
return 'You\'ve entered "{}" and clicked {} times'.format(input_value, n_clicks)
if __name__ == '__main__':
app.run_server()
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Button('Submit', id='button'),
html.Div([dcc.Interval(
id='interval-component',
interval=1 * 3000, # in milliseconds
n_intervals=0
)]),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input('button', 'n_clicks')], [Input('interval-component', 'n_intervals')],
state=[State(component_id='my-id', component_property='value')]
)
def update_output_div(n_clicks,n_intervals, input_value):
return 'You\'ve entered "{}" and clicked {} times'.format(input_value, n_clicks)
if __name__ == '__main__':
app.run_server()
最新答案
好吧,我成功地实现了我自己的建议,但它不是微不足道的,而且仍然有一些怪癖
复杂因素包括:
- Dash不允许两次回调以相同的
输出为目标
- 没有很好的方法跟踪哪个
或输入
触发了回调。解决方法通常涉及跟踪每个按钮的点击次数(参见示例)事件
- 通过
或disable=True
禁用计时器似乎是永久性的。一旦我以这种方式停止计时器,我就无法用max_requests=0
或disable=False
重新启动它max\u requests=1000
- 在这个解决方案中,
会被调用两次,但是您可以通过测量按钮单击次数到上一次计数来区分两者之间的差异,这样您就可以防止它两次提交数据update\u output\u div()
- 小于100毫秒的超时将不起作用。为了让我的方法工作,我不得不拆分延迟计时器,所以我选择了
和100
作为两个计时器持续时间。原则上,你可以把它们平均分成两半。我不知道在网络上工作时使用低超时是否有任何问题(我在本地主机上进行了测试)(1000*按钮按下锁定秒)-100
导入json
导入日期时间
导入破折号
从dash.dependencies导入输入、输出、状态和事件
将仪表板核心组件作为dcc导入
将dash_html_组件导入为html
按钮按下锁定秒=10秒
app=dash.dash()
app.config['suppress\u callback\u exceptions']=True
def SERVER_布局():
返回html.Div([
dcc.Input(id='my-id',value='initial value',type=“text”),
按钮('Submit',id='Button'),
html.Div(
[
dcc.Interval(id='Interval-component',disabled=True)
,dcc.Interval(id='Interval-sync-component',disabled=True)
]
,id='interval-container'
),
html.Div(“,id='my-Div'),
Div(json.dumps({'n_clicks':0,'n_previous_clicks':0}),id='local_data'),
html.Div(“??”,id='button-status'),
])
app.layout=服务布局
#轨道按钮点击
@app.callback(
输出=输出(组件\u id='local\u data',组件\u属性='children'),
输入=[Input('button','n_clicks')],
state=[state('local_data','children')],
事件=[事件('interval-sync-component','interval')]
)
def跟踪点击(n点击、本地数据点击):
如果n_单击为无:
n_=0
local\u data=json.load(local\u data\u json)
n_上一次单击=本地数据['n_单击']
#使用新的单击数据更新本地数据
本地\u数据.update(**{'n\u点击:n\u点击,'n\u上一次点击:n\u上一次点击})
#本地\u数据.update(**{'n\u点击:n\u点击,'n\u上一次点击:n\u上一次点击})
返回json.dumps(本地_数据)
#更新按钮点击次数后,提交
@app.callback(
输出=输出(组件\u id='my-div',组件\u属性='children'),
输入=[Input('local_data','children')],
state=[状态(component_id='my-id',component_property='value'),状态('my-div','children')]
)
def update_output_div(本地数据、输入值、当前状态):
local\u data=json.load(local\u data\u json)
n_clicks=本地_数据['n_clicks']
n_上一次单击=本地数据['n_上一次单击']
#真正的服从
如果n_单击>n_上一次单击:
return'您已经输入了“{}”并点击了{}次({})'(
输入值
,n_clicks如果n_clicks不是其他0
,datetime.datetime.now()
)
#不是真正的提交,但函数被称为一个额外的时间,作为下面的计时器废话的副作用。
其他:
返回“*”+当前_状态
#启动(或停止)计时器
@app.callback(
输出=输出('interval-container','children'),
输入=[Input('local_data','children')],
状态=[状态('按钮','禁用')],
事件=[事件('interval-component','interval')]
)
def start_定时器(本地_数据_json,按钮被禁用):
local\u data=json.load(local\u data\u json)
n_clicks=本地_数据['n_clicks']
n_上一次单击=本地数据['n_上一次单击']
儿童=[]
如果n_单击>n_上一次单击:
同步定时器=dcc.间隔(
id='interval-sync-component',
间隔=100,#以毫秒为单位
)
附加(同步计时器)
如果按钮被禁用:
主定时器=dcc.间隔(
id='interval-component',
间隔=(1000*按钮_按下_锁定_秒)-100,#以毫秒为单位
)
附加(主计时器)
返回儿童
#每当触发计时器间隔时启用按钮,或按下按钮时禁用按钮
@app.callback(
输出=输出(“按钮”、“禁用”),
输入=[Input('button','n_clicks')],
state=[state('local_data','children')],
事件=[事件('interval-component','interval')]
)
def切换按钮禁用状态(n次单击,本地数据):
local\u data=json.load(local\u data\u json)
#n_clicks=本地_数据['n_clicks']
如果n_单击为无:
n_=0
n_上一次单击=本地数据['n_上一次单击']
#我们是通过点击按钮到达这里的,所以禁用按钮
如果