In the use case under consideration, a file that a user wants to upload to Bokeh server is only accessible on the user's local system, and the data in that file are serialized as a series of bytes to be sent to the Bokeh server application. From within the application, those bytes must be decoded and assembled into whatever data structure(s) are needed to proceed with computation on the server. Below is a small code example that enables a user to choose a csv file for plotting using the Bokeh FileInput widget, takes the uploaded data and uses it to create a Pandas DataFrame, and makes a Bokeh line plot of the x and y columns in the data.

The FileInput widget carries out a base64 encoding to convert the bytes within the chosen data file to ASCII characters for transmission to the Bokeh server. The code below uses two modules from the Python Standard Library (base64, io) to make the data accessible to Pandas (pd), in order to create a DataFrame. First, the base64.b64decode function is used to decode the encoded data (line 18), then the io.BytesIO function is used to create an in-memory bytes buffer (line 19). While one usually calls the pd.read_csv function with a string input that represents a filename or path, it can also be passed a "file-like object", which an in-memory bytes buffer is an instance of. So we can create a DataFrame by passing that buffer as an input (line 20). One the DataFrame is constructed, it can be used to populate a Bokeh plot (line 23).

If you were to copy and paste that code into a file named "bokeh_read_file.py" and then run it using Bokeh server via bokeh serve --show bokeh_read_file.py, you would see a FileInput widget prompting you to "Choose File", along with an empty plot window. If you were to choose a csv file with two columns (representing the x and y values to be plotted, with no header row in this simple example), you might see a plot like that in the figure below.

Example of FileInput widget
A screenshot demonstrating the use of the FileInput widget to plot the x-y data in a file named "Scan4.csv".
Note: processing multiple files at once

The FileInput widget has an optional parameter multiple, which — if set to True — enables users to select multiple files within the FileInput chooser, which then each get serialized as separate elements of a Python list. It is up to the developer how to deal with those multiple files, but this can be useful for plotting multiple datasets simultaneously, or for processing sets of files that are connected to one another (e.g., data and metadata files).

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement