
I have been struggling with uploading files to the bokeh server as I did not understand very well how the base64 encoding/decoding works. I only managed to upload a .csv file by decoding and then reconstructing it. This is probably not the simplest way to do and can hardly be repeated for a more complex file structure (how to go form “message” back to a .shp file for instance in the code below), which makes me believe there is a better way to do it and that could upload any kind of file…
Here is what I did for the .csv file:
file_input = FileInput(accept=".csv")
def upload_csv_to_server(attr, old, new):
#decode base64 format (from python base24 website)
base64_message = file_input.value
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
#convert string to csv and save it on the server side
message_list = message.splitlines()
with open('data/' + file_input.filename, 'w', newline='') as file:
writer = csv.writer(file)
for i in range(len(message_list)):
writer.writerow(message_list[i].split(','))
file_input.on_change('value', upload_csv_to_server)
Thanks!

