当用户更改DatePicker值时,我试图通过URL传递DatePicker值,并且看到异常行为。当我更改另一个DatePicker时,DatePicker日期格式在字符串和纪元之间切换。最佳示例显示,这是当我更改每个DatePicker时生成的两个URL,
http://127.0.0.1:6800/?start_date=1529424998666&end_date=Tue%20Jun%2019%202018 http://127.0.0.1:6800/?start_date=Fri%20Jun%2008%202018&end_date=1529512006618
from datetime import datetime, timedelta
from bokeh.embed import components
from bokeh.models.widgets.inputs import DatePicker
from bokeh.models.callbacks import CustomJS
from bokeh.layouts import column
from flask import Flask
app = Flask(__name__)
@app.route("/", methods=['GET'])
def test():
text = """<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://cdn.pydata.org/bokeh/release/bokeh-0.12.15.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.pydata.org/bokeh/release/bokeh-0.12.15.min.js"></script>
<link href="https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.15.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.15.min.js"></script>
<meta charset="UTF-8">
</head>
<body>"""
d_yesterday = datetime.today() - timedelta(days=1)
d_year_ago = datetime.today() - timedelta(days=365)
datepicker_start = DatePicker(title="Start Date", min_date=d_year_ago, max_date=datetime.now(), value=d_yesterday)
datepicker_end = DatePicker(title="End Date", min_date=d_year_ago, max_date=datetime.today(), value=datetime.today())
all_callback = CustomJS(args=dict(d_start=datepicker_start, d_end=datepicker_end), code="""
function encodeQueryData(data) {
let ret = [];
for (let d in data)
ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
return ret.join('&');
}
var params = {'start_date': d_start.value, 'end_date': d_end.value }
var url = '/?' + encodeQueryData(params)
window.location.replace(url);
""")
datepicker_start.callback = all_callback
datepicker_end.callback = all_callback
layout = column(datepicker_start, datepicker_end)
_script, _div = components(layout)
text += "{}{}".format(_script, _div)
text += """
</body></html>
"""
return text
app.run(host="0.0.0.0", port=6800)
这是预期的吗?我是否想阻止这种行为?