python bokeh在服务器端或客户端运行

时间:2019-12-22 10:26:40

标签: python flask bokeh

我是新手,如果我的问题很傻,请您抱歉。 我试图使网络应用程序将接受客户的输入,然后对其进行处理,然后将其提供给bokeh。这个过程非常繁琐,所以我要使用高规格的AWS实例,我想知道的是,如果我将bokeh嵌入到我的烧瓶中,那么bokeh绘制过程将在客户端或服务器端运行?据我所知,有两个选项“嵌入式”和“运行bokeh服务器”,现在我正在使用embed方法,并且我想知道是否使用bokeh服务器会使其更快

from bokeh.plotting import figure, output_file, show
from bokeh.models import LinearColorMapper, LogTicker, ColorBar
import numpy as np
import rasterio
import io

def read_DN(fname):
    inRaster = rasterio.open(fname)
    arr_DN = inRaster.read(1)
    arr_DN = arr_DN.astype('float32')
    arr_DN[arr_DN == 0] = 'nan'
    arr_DN_nan = np.isnan(arr_DN)
    return (arr_DN,arr_DN_nan)

def calculate_ndvi (fname4,fname5):
    arr_surf4,arr_DN4_nan = read_DN(fname4)
    arr_surf5,arr_DN4_nan = read_DN(fname5)

    ndvi = (arr_surf5 - arr_surf4) / (arr_surf5 + arr_surf4)

    return ndvi



def plottt(fname4,fname5,feature):
    ndvi = calculate_ndvi (fname4,fname5)
    ndvi = ndvi[3000:3500,3000:3500]
    ndvi = np.flipud(ndvi)
    if feature == 'lst' :
        plot = figure(x_range=(0,1), y_range=(0,1), toolbar_location="right")
        color_mapper =  LinearColorMapper(palette="Spectral11", low=-1, high=1, nan_color="white")
        plot.image(image=[ndvi], color_mapper=color_mapper,
                   dh=[1.0], dw=[1.0], x=[0], y=[0])

        color_bar = ColorBar(color_mapper=color_mapper,
                             label_standoff=12, border_line_color=None, location=(0,0))

        plot.add_layout(color_bar, 'right')
    elif feature == 'ndvi' :
        plot = figure(x_range=(0,1), y_range=(0,1), toolbar_location="right")
        color_mapper =  LinearColorMapper(palette="YlGn9", low=-1, high=1, nan_color="white")
        plot.image(image=[ndvi], color_mapper=color_mapper,
                   dh=[1.0], dw=[1.0], x=[0], y=[0])

        color_bar = ColorBar(color_mapper=color_mapper,
                             label_standoff=12, border_line_color=None, location=(0,0))

        plot.add_layout(color_bar, 'right')

    return plot
@app.route('/hasil')
def correlation_matrix():
    current_feature_name = request.args.get("feature_name")
    if current_feature_name == None:
        current_feature_name = "lst"

    for root, dirs, files in os.walk(app.config['UPLOAD_FOLDER']):
        for file in files:
            if file.endswith('B4.TIF'):
                fname4 = os.path.join(app.config['UPLOAD_FOLDER'], file)
            if file.endswith('B5.TIF'):
                fname5 = os.path.join(app.config['UPLOAD_FOLDER'], file)

    hasil = plottt(fname4,fname5,current_feature_name)
    script, div = components(hasil)

    return render_template("index1.html", script=script, div=div)

1 个答案:

答案 0 :(得分:0)

无论您使用嵌入服务器还是bokeh服务器,Python代码都将在服务器上运行。

  

Bokeh的体系结构使得高级“模型对象”(代表图形,范围,轴,字形等)在Python中创建,然后转换为客户端使用的JSON格式库,BokehJS。 -https://docs.bokeh.org/en/latest/docs/user_guide/server.html#running-a-bokeh-server

换句话说,如果您小心不要通过让嵌入块在其自己的进程上运行来让嵌入块,则性能应该不会有太大差异。通常,假设您没有在Flask上运行过多的其他东西,仅使用具有多个进程(例如gunicorn)的WSGI服务器就足够了。如果客户端支持WebGL(大多数浏览器都支持),则client side rendering could be sped up using output_backend="webgl"