Bokeh执行server.start无输出 bokeh serve端口异常页面空白求助
解决方案
1. 直接运行脚本server立即终止的修复
直接运行脚本时仅调用server.start()启动服务,未保持主线程运行,程序执行完所有代码后直接退出,导致服务终止。
修复方法:在代码最后一行添加如下代码,启动I/O循环保持服务运行:
server.io_loop.start()
修复完成后直接运行python flight.py,访问http://localhost:8084即可进入应用。
2. bokeh serve运行端口不匹配、页面空白的修复
2.1 端口不匹配问题
使用bokeh serve命令启动服务时,会忽略脚本内定义的Server实例配置,默认使用5006端口。
修复方法:启动时添加--port参数指定端口:
bokeh serve --port 8084 flight.py
同时可以删除脚本末尾的三行Server初始化代码,避免逻辑冲突:
# 删掉以下冗余代码 apps = {'/': Application(FunctionHandler(flight_tracking))} server = Server(apps, port=8084) server.start()
2.2 页面空白问题
绝大多数情况是接口请求失败或回调报错导致的,按以下步骤排查修复:
- 首先验证OpenSky API可用性:将你拼接的
url_data中的用户名和密码替换为实际值,直接在浏览器访问该链接,确认能正常返回JSON格式的航班数据。如果返回401说明账号密码错误,返回超时说明网络无法访问OpenSky接口。 - 给update函数添加异常捕获,避免接口请求失败导致整个回调崩溃,修改后的update函数示例:
def update(): try: response=requests.get(url_data, timeout=10).json() if 'states' not in response or not response['states']: return # 无数据时直接返回不更新 col_name=['icao24','callsign','origin_country','time_position','last_contact','long','lat','baro_altitude','on_ground','velocity', 'true_track','vertical_rate','sensors','geo_altitude','squawk','spi','position_source'] flight_data=response['states'] flight_df=pd.DataFrame(flight_data,columns=col_name) wgs84_to_web_mercator(flight_df) flight_df=flight_df.fillna('No Data') flight_df['rot_angle']=flight_df['true_track']*-1 icon_url='https://www.logolynx.com/images/logolynx/4c/4c507d3961382eefd8a48f5c387a0ee3.png' flight_df['url']=icon_url n_roll=len(flight_df.index) flight_source.stream(flight_df.to_dict(orient='list'),n_roll) except Exception as e: print(f"数据更新失败: {e}")
- 当前代码的坐标范围默认是美国本土区域,如果你需要查看其他区域的航班,调整
lon_min,lat_min,lon_max,lat_max四个参数的取值即可。
内容的提问来源于stack exchange,提问作者user15966297
相关产品推荐
相关产品推荐

