Plotly / Dash - Python 如何在时间后停止执行?
Plotly / Dash - Python how to stop execution after time?
我想在一定时间后停止执行我的 Dash 程序(当我关闭浏览器时更好 window,尽管我怀疑这是可能的)。有没有办法通过python中断它?
我已经试过放一个
sys.exit()
调用后 app.run_server。据我所知很难
app.run_server
处于无限循环中,因此我永远无法到达 sys.exit()
if __name__ == '__main__':
app.title = 'foo'
app.run_server(debug=False)
sys.exit("Bye!")
因为 plotly
使用 flask
作为服务器。所以你的代码 sys.exit("Bye!")
实际上永远不会到达,因此你的服务器永远不会停止。
所以有两种方法可以停止你的服务器,
Ctrl + c,我假设你现在会这样做
现在你也可以使用代码来完成,所以如果你真的需要在一段时间后停止代码,你应该停止 flask 服务器。要停止 flask 服务器,您需要创建一个路由。所以每当你点击 url 服务器就会停止。
以下是Flask的代码,需要转换成等价的plotly代码:
from flask import request
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
现在你可以通过调用这个函数来关闭服务器:
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
更新:
对于 plotly,您可以按以下方式编写代码。
import dash
import dash_core_components as dcc
import dash_html_components as html
from flask import request
print(dcc.__version__) # 0.6.0 or above is required
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
# represents the URL bar, doesn't render anything
dcc.Location(id='url', refresh=False),
dcc.Link('Navigate to "/"', href='/'),
html.Br(),
dcc.Link('Navigate to "/page-2"', href='/page-2'),
# content will be rendered in this element
html.Div(id='page-content')
])
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.callback(dash.dependencies.Output('page-content', 'children'),
[dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
if pathname =='/shutdown':
shutdown()
return html.Div([
html.H3('You are on page {}'.format(pathname))
])
if __name__ == '__main__':
app.run_server(debug=True)
def button():
button_reply = QMessageBox.question(MainWindow, "Bank Management System", "Deposited Successfully", QMessageBox.Ok)
if button_reply == QMessageBox.Ok:
Deposit()//execute deposite function first
threading.Timer(5.0,clearData).start()// clrarData function will execute after 5 seconds
我想在一定时间后停止执行我的 Dash 程序(当我关闭浏览器时更好 window,尽管我怀疑这是可能的)。有没有办法通过python中断它?
我已经试过放一个
sys.exit()
调用后 app.run_server。据我所知很难
app.run_server
处于无限循环中,因此我永远无法到达 sys.exit()
if __name__ == '__main__':
app.title = 'foo'
app.run_server(debug=False)
sys.exit("Bye!")
因为 plotly
使用 flask
作为服务器。所以你的代码 sys.exit("Bye!")
实际上永远不会到达,因此你的服务器永远不会停止。
所以有两种方法可以停止你的服务器,
Ctrl + c,我假设你现在会这样做
现在你也可以使用代码来完成,所以如果你真的需要在一段时间后停止代码,你应该停止 flask 服务器。要停止 flask 服务器,您需要创建一个路由。所以每当你点击 url 服务器就会停止。
以下是Flask的代码,需要转换成等价的plotly代码:
from flask import request
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
现在你可以通过调用这个函数来关闭服务器:
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
更新: 对于 plotly,您可以按以下方式编写代码。
import dash
import dash_core_components as dcc
import dash_html_components as html
from flask import request
print(dcc.__version__) # 0.6.0 or above is required
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
# represents the URL bar, doesn't render anything
dcc.Location(id='url', refresh=False),
dcc.Link('Navigate to "/"', href='/'),
html.Br(),
dcc.Link('Navigate to "/page-2"', href='/page-2'),
# content will be rendered in this element
html.Div(id='page-content')
])
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.callback(dash.dependencies.Output('page-content', 'children'),
[dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
if pathname =='/shutdown':
shutdown()
return html.Div([
html.H3('You are on page {}'.format(pathname))
])
if __name__ == '__main__':
app.run_server(debug=True)
def button():
button_reply = QMessageBox.question(MainWindow, "Bank Management System", "Deposited Successfully", QMessageBox.Ok)
if button_reply == QMessageBox.Ok:
Deposit()//execute deposite function first
threading.Timer(5.0,clearData).start()// clrarData function will execute after 5 seconds