如何将交互式地图(python代码)上传到网站

时间:2019-08-27 10:39:04

标签: python pandas plot bokeh interactive

我使用python代码创建一个交互式地图。我可以使用bokeh服务器在笔记本电脑上本地运行它。但是,我必须将交互式地图上传到网站。你能给我一个提示吗? 谢谢

这是我的代码。

import geopandas as gpd
shapefile = '/ne_110m_admin_0_countries.shp'
gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]
gdf.columns = ['country', 'country_code', 'geometry']
gdf.head()


print(gdf[gdf['country'] == 'Antarctica'])
#Drop row corresponding to 'Antarctica'
gdf = gdf.drop(gdf.index[159])


import pandas as pd
datafile1 = '/data_final_clean_collapsed.dta'
#Read csv file using pandas
df1 = pd.read_stata(datafile1)
df1 =df1[['cname_iso', 'ccode_cow', 'ccode_iso', 'ccode_state', 
'year','CWI_basic']]
df1.head()
import json


from bokeh.io import output_notebook, show, output_file
from bokeh.plotting import figure
from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar
from bokeh.palettes import brewer

#df_2016 = df[df['year'] == 2016]
#Perform left merge to preserve every row in gdf.
#merged = gdf.merge(df_yr, left_on = 'country_code', right_on = 'code', 
how = 'left')


from bokeh.io import curdoc, output_notebook
from bokeh.models import Slider, HoverTool
from bokeh.layouts import widgetbox, row, column
#Define function that returns json_data for year selected by user.

def json_data(selectedYear):
   yr = selectedYear
   df1_yr = df1[df1['year'] == yr]
   a_yr=df1_yr.groupby(['cname_iso']).mean()
   a_yr=a_yr.reset_index()
merged = gdf.merge(a_yr, left_on = 'country', right_on ='cname_iso')
merged.fillna('No data', inplace = True)
merged_json = json.loads(merged.to_json())
json_data = json.dumps(merged_json)
return json_data
#Input GeoJSON source that contains features for plotting.
geosource = GeoJSONDataSource(geojson = json_data(2017))
#Define a sequential multi-hue color palette.
palette = brewer['YlGnBu'][8]
#Reverse color order so that dark blue is highest obesity.
palette = palette[::-1]
#Instantiate LinearColorMapper that linearly maps numbers in a range, 
into a 
sequence of colors. Input nan_color.
color_mapper = LinearColorMapper(palette = palette, low = 0, high = 0.40, 
nan_color = '#d9d9d9')
#Define custom tick labels for color bar.
tick_labels = {'0': '0.001', '1': '0.01','5':'0.05', '10':'0.1', 
'15':'0.15', 
'20':'0.20', '25':'0.25', '30':'0.30','35':'0.35', '40': '>0.40'}
#Add hover tool
hover = HoverTool(tooltips = [ ('Country/region','@country'),('% 
CWI_basic', '@CWI_basic')])
#Create color bar. 
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 
500, 
height = 20,
                 border_line_color=None,location = (0,0), orientation = 
'horizontal', major_label_overrides = tick_labels)
#Create figure object.
p = figure(title = 'CWI_basic, 2016', plot_height = 600 , plot_width = 
950, 
toolbar_location = None, tools = [hover])
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
#Add patch renderer to figure. 
p.patches('xs','ys', source = geosource,fill_color = {'field' 
:'CWI_basic', 
'transform' : color_mapper},
          line_color = 'black', line_width = 0.25, fill_alpha = 1)
#Specify layout
p.add_layout(color_bar, 'below')
# Define the callback function: update_plot
def update_plot(attr, old, new):
    yr = slider.value
    new_data = json_data(yr)
    geosource.geojson = new_data
    p.title.text = 'CWI_basic, %d' %yr

# Make a slider object: slider 
slider = Slider(title = 'Year',start = 1950, end = 2017, step = 1, value 
= 
2016)
slider.on_change('value', update_plot)
# Make a column layout of widgetbox(slider) and plot, and add it to the 
current document
layout = column(p,widgetbox(slider))
curdoc().add_root(layout)
#Display plot inline in Jupyter notebook
output_notebook()
#Display plot
show(layout)  

上面是创建我的交互式地图的代码。我不知道如何将代码嵌入到html网站中,以便可以将交互式地图发布到该网站上

0 个答案:

没有答案