I'm trying to create a simple bokeh server application that allows a user to load a file from a <input type="file">
file selection button. The app will then plot the data from the file that the user selected. The code below is very simplistic, and I simply don't know how to pass the file information from the file selector to python. I need to use python to handle the file I/O and not html or javascript.
I can get it to work just fine when I run bokeh serve --show example.py path/to/input_file
at the command line, but I don't want the user to specify this each time. I need them to be able to click a button to "upload" the file. This application is running locally, so there is no uploading to a server or anything like that.
Is there a better method than <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"