python - 交互式 Bokeh map

标签 python maps bokeh

我正在使用此代码在 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/

相关文章:

python - 在 Python 的 For 循环中使用 'And' 运算符

javascript - KmlLayer getDefaultViewport() 未定义- Google map

python - 使用 bokeh 或 matplotlib 的 Pandas DataFrame 分层饼图/ donut chart

python - Bokeh 0.12.14 + flask : Broken library or new syntax for AjaxDataSource?

python - 全局安装 PyPy Ubuntu

python - 哈希表解释

python - 无法在终端中使用 pip 或 easy_install 在 Mac 上安装

快速区域监控 - 自定义形状

java - Java 中的 map 和列表

python - 在 Bokeh 0.12 中为 Span 添加标签