我正在尝试创建一个简单的散景服务器应用程序,允许用户从<input type="file">
文件选择按钮。然后,应用程序将绘制用户选择的文件中的数据。下面的代码非常简单,我根本不知道如何将文件信息从文件选择器传递到python。我需要使用 python 来处理文件 I/O,而不是 html 或 javascript。
当我跑步时我可以让它正常工作bokeh serve --show example.py path/to/input_file
在命令行,但我不希望用户每次都指定它。我需要他们能够单击按钮来“上传”文件。该应用程序在本地运行,因此无需上传到服务器或类似的东西。
有没有更好的方法<input type="file">
?
from bokeh.plotting import figure
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource, Div
from bokeh.io import curdoc
desc = Div(text="""
<h1>A simple example</h1>
<input type="file">
<br />""", width=800)
# Create Column Data Source that will be used by the plot
source = ColumnDataSource(data=dict(x=[], y=[]))
p = figure(plot_height=550, plot_width=800, title="", toolbar_location='above')
p.line(x="x", y="y", source=source)
def update():
x_data,y_data = read_file_data(input_file_name) # function to read specific file type
source.data = dict(
x=x_data,
y=y_data,
)
sizing_mode = 'fixed' # 'scale_width' also looks nice with this example
l = layout([
[desc],
[p],
], sizing_mode=sizing_mode)
update()
curdoc().add_root(l)
curdoc().title = "Sample"