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

2024-09-30 00:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个新手,如果我的问题很傻,我很抱歉。 我试图使网络应用程序,将采取客户端输入,然后处理它,然后服务于博凯。这个过程非常繁重,所以我将使用高规格的AWS实例,我想知道的是,如果我嵌入bokeh到我的烧瓶,那么bokeh绘图过程将在客户端或服务器端运行?我知道有两个选项“嵌入”和“运行bokeh服务器”,现在我使用嵌入方法,我想知道是否使用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)

Tags: nameimportplotdefbokehnanfeaturefile
1条回答
网友
1楼 · 发布于 2024-09-30 00:29:45

Python代码将在服务器上运行,而不管您是使用embed还是bokeh服务器。你知道吗

The architecture of Bokeh is such that high-level “model objects” (representing things like plots, ranges, axes, glyphs, etc.) are created in Python, and then converted to a JSON format that is consumed by the client library, BokehJS. - https://docs.bokeh.org/en/latest/docs/user_guide/server.html#running-a-bokeh-server

换句话说,如果您小心地不让embed阻塞,让它在自己的进程上运行,那么性能应该不会有太大的差别。通常情况下,只要使用具有多个进程(如gunicorn)的WSGI服务器就足够了,前提是您没有其他繁重的进程在Flask上运行。如果客户端支持WebGL(大多数浏览器都支持),那么client side rendering could be sped up using ^{}。你知道吗

相关问题 更多 >

    热门问题