如何在代码中返回数据表,以便在bokeh中使用?

问题描述 投票:0回答:1
import numpy as np
import pandas as pd
from bokeh.plotting import figure, gridplot
from bokeh.io import output_file, show, curdoc
from bokeh.models.widgets import FileInput, DataTable, DateFormatter, TableColumn
from pybase64 import b64decode
from bokeh.models import ColumnDataSource
import io

# output to static HTML file
output_file("./OUTPUT/001_koreksi.html", title='Koreksi Gayaberat')


def upload_fit_data(attr, old, new):
    print("fit data upload succeeded")
    decoded = b64decode(new)
    f = io.BytesIO(decoded)
    new_df = pd.read_excel(f, sheet_name='data')
    # print(new_df)
    data = dict(utmx=new_df['UTM X'],
                utmy=new_df['UTM Y'],
                elev=new_df['Elevasi'],
                lat=new_df['Latitude'],
                lon=new_df['Longitude'],
                ta=new_df['Tinggi Alat'],
                N=new_df['N'],
                E=new_df['E'],
                S=new_df['S'],
                W=new_df['W'],
                time=new_df['Time'],
                kt=new_df['Koreksi Tide'],
                rdgrav=new_df['Bacaan'])
    source = ColumnDataSource(data)

    columns = [TableColumn(field="utmx", title="UTM X"), TableColumn(field="utmy", title="UTM Y"),
               TableColumn(field="elev", title="Elevasi"), TableColumn(field="lat", title="Latitude"),
               TableColumn(field="lon", title="Longitude"), TableColumn(field="ta", title="Tinggi Alat"),
               TableColumn(field="N", title="North"), TableColumn(field="E", title="East"),
               TableColumn(field="S", title="South"), TableColumn(field="W", title="West"),
               TableColumn(field="time", title="Waktu Pengukuran"), TableColumn(field="kt", title="Koreksi tide"),
               TableColumn(field="rdgrav", title="Bacaan Alat")]
    data_table = DataTable(source=source, columns=columns, width=500, height=300)
    global data_table

    # return data_table


# data_table = DataTable(source=source, columns=columns, width=500, height=300)
file_input = FileInput(accept=".xlsx")
file_input.on_change('value', upload_fit_data)


p = gridplot([file_input, data_table])
doc = curdoc()
doc.add_root(p)

问题:如何在bokeh应用程序中使用和显示“ data_table”值?如果我运行此代码,则在bokeh界面中不显示“ data_table”]

将numpy作为np导入,将熊猫从bokeh.plotting导入图形,从bokeh.io导入gridplot从bokeh.io导入output_file,显示,从bokeh.models.widgets导入curdoc。

除了Z4层对您的问题的评论中提到的问题之外,还有其他问题:

    [output_file不能与bokeh serve一起使用-并且bokeh serve正是您所需要的,因为您想运行Python代码来响应Web UI中用户的操作
  • gridplot接受一个元组列表,这些元组不仅指定项目本身,而且指定放置每个项目的位置。由于您只有两个小部件,因此column应该足够
  • 考虑此工作代码:
  • import io from base64 import b64decode import pandas as pd from bokeh.io import curdoc from bokeh.layouts import column from bokeh.models import ColumnDataSource from bokeh.models.widgets import FileInput, DataTable, TableColumn column_map = dict(utmx='UTM X', utmy='UTM Y', elev='Elevasi', lat='Latitude', lon='Longitude', ta='Tinggi Alat', N='N', E='E', S='S', W='W', time='Time', kt='Koreksi Tide', rdgrav='Bacaan') source = ColumnDataSource(data={c: [] for c in column_map}) columns = [TableColumn(field="utmx", title="UTM X"), TableColumn(field="utmy", title="UTM Y"), TableColumn(field="elev", title="Elevasi"), TableColumn(field="lat", title="Latitude"), TableColumn(field="lon", title="Longitude"), TableColumn(field="ta", title="Tinggi Alat"), TableColumn(field="N", title="North"), TableColumn(field="E", title="East"), TableColumn(field="S", title="South"), TableColumn(field="W", title="West"), TableColumn(field="time", title="Waktu Pengukuran"), TableColumn(field="kt", title="Koreksi tide"), TableColumn(field="rdgrav", title="Bacaan Alat")] data_table = DataTable(source=source, columns=columns, width=500, height=300) def upload_fit_data(attr, old, new): f = io.BytesIO(b64decode(new)) new_df = pd.read_excel(f, sheet_name='data') source.data = {ds_c: new_df[df_c] for ds_c, df_c in column_map.items()} file_input = FileInput(accept=".xlsx") file_input.on_change('value', upload_fit_data) curdoc().add_root(column(file_input, data_table))

    请注意,如果您不想拥有一个交互式网页,而只想拥有一个静态HTML文件,那么您将无法使用任何Python回调。您将通过CLI指定XLSX文件,并在脚本运行时立即处理数据。
    python pandas user-interface bokeh
    1个回答
    0
    投票
    除了Z4层对您的问题的评论中提到的问题之外,还有其他问题:
    © www.soinside.com 2019 - 2024. All rights reserved.