我正在使用此代码在 python 上制作交互式 map :
# Define the callback: update_plot
def update_plot(attr, old, new):
# Create a dropdown Select widget for the y data: y_select
N = str(select.value)
map_options = GMapOptions(lat=sites_list_c.loc[sites_list_c['Site Name'] == N,'Latitude Decimal'], lng=sites_list_c.loc[sites_list_c['Site Name'] == N,'Lontitude Decimal'], map_type="roadmap", zoom=4)
plot = gmap(my_key, map_options, title="Test")
source = ColumnDataSource(
data=dict( lat=sites_list_c['Latitude Decimal'].tolist(),
lon=sites_list_c['Longitude Decimal'].tolist()
)
)
plot.circle(x="lon", y="lat", size=15, fill_color='blue', fill_alpha=0.8, source=source)
# Attach the update_plot callback to the 'value' property of y_select
select.on_change('value', update_plot)
# Create layout and add to current document
layout = row(widgetbox(select), plot)
curdoc().add_root(layout)
show(layout)
但是,我收到此警告:
WARNING:bokeh.embed.util:
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.
Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:
http://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html
Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:
http://docs.bokeh.org/en/latest/docs/user_guide/server.html
最佳答案
该消息试图不言自明。为了将真正的 Python 回调连接到 UI 事件,必须运行一个真正的 Python 进程来执行回调代码。该进程是 Bokeh 服务器,要使用它,您将运行类似于以下内容的代码:
bokeh serve --show app.py
更具体地说,您不会只执行python app.py
(请注意,您还需要删除对 show
的调用,因为它无法与 Bokeh 服务器应用程序一起使用。)
否则,如果您只是像常规 python 脚本一样运行它(并使用 show
调用),那么 Bokeh 会生成静态 HTML+JS 输出。在这种情况下,Python 回调无法执行,因为输出仅纯粹显示在 Web 浏览器中,而 Web 浏览器无法运行 Python 代码。唯一可以发挥作用的回调是 JavaScript 回调。
Running a Bokeh Server 中有关于运行 Bokeh 服务器应用程序的大量文档。文档的章节,以及 JavaScript Callbacks 中有关 CustomJS
回调的大量文档。章。
关于python - 交互式 Bokeh map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497441/