To be more precise, why this code is not working? I mean why it doesn’t show the image?
import os
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.plotting import figure
from os.path import join, basename
import logging
import base64
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import Button
_upload_js = “”"
function read_file(filename) {
var reader = new FileReader();
reader.onload = load_handler;
reader.onerror = error_handler;
// readAsDataURL represents the file’s data as a base64 encoded string
reader.readAsDataURL(filename);
}
function load_handler(event) {
var b64string = event.target.result;
source.data = {‘contents’ : [b64string], ‘name’:[input.files[0].name]};
source.change.emit()
}
function error_handler(evt) {
if(evt.target.error.name == “NotReadableError”) {
alert(“Can’t read file!”);
}
}
var input = document.createElement(‘input’);
input.setAttribute(‘type’, ‘file’);
input.onchange = function(){
if (window.FileReader) {
read_file(input.files[0]);
} else {
alert(‘FileReader is not supported in this browser’);
}
}
input.click();
“”"
save_path = os.getcwd()
name = ‘temp.esi’
def file_callback(attr, old, new):
raw_contents = source.data[‘contents’][0]
remove the prefix that JS adds
prefix, b64_contents = raw_contents.split(“,”, 1)
file_contents = base64.b64decode(b64_contents)
fname = join(save_path, name)
with open(fname, “wb”) as f:
f.write(file_contents)
logging.info("Success: file uploaded " + fname)
update(fname)
source = ColumnDataSource({‘contents’: , ‘name’: })
source.on_change(‘data’, file_callback)
button = Button(label=“Upload File”, button_type=“success”)
button.callback = CustomJS(args=dict(source=source), code=_upload_js)
Set up plot
p = figure(x_range=(0,1), y_range=(0,1))
def update(fname):
p.image_url(url=[fname], x=0, y=1, w=1, h=1)
update(“temp.esi”)
curdoc().add_root(row(button, p))
curdoc().title = “Image Loader”
``
Thanks
···
On Sunday, April 15, 2018 at 6:40:11 PM UTC-4, Esmaeil Naderi wrote:
Hello Timur,
I’m quite new to these stuff and my question may seem trivial. I used this piece of code which gives me a button to load file, but I would like to know once the file content is loaded in source, how can I use it to plot the image?
Thanks
On Thursday, November 23, 2017 at 1:59:09 AM UTC-5, Timur Zaripov wrote:
Hi Sai Jena,
I could not find the thread that helped me to solve similar problem, so I will share the code.
The code below defines function that creates upload button, that you can add to your layout. When you click the button custom js open file selection dialog. Selected file uploaded to save_path under give name. After upload callback() is called.
from os.path import join, basename
import logging
import base64
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import Button
def new_upload_button(save_path,
callback,
name=“tmp.las”,
label=“Upload file.”):
def file_callback(attr, old, new):
raw_contents = source.data[‘contents’][0]
# remove the prefix that JS adds
prefix, b64_contents = raw_contents.split(“,”, 1)
file_contents = base64.b64decode(b64_contents)
fname = join(save_path, name)
with open(fname, “wb”) as f:
f.write(file_contents)
logging.info("Success: file uploaded " + fname)
callback(fname)
source = ColumnDataSource({'contents': [], 'name': []})
source.on_change('data', file_callback)
button = Button(label=label, button_type="success")
button.callback = CustomJS(args=dict(source=source), code=_upload_js)
return button, source
_upload_js = “”"
function read_file(filename) {
var reader = new FileReader();
reader.onload = load_handler;
reader.onerror = error_handler;
// readAsDataURL represents the file’s data as a base64 encoded string
reader.readAsDataURL(filename);
}
function load_handler(event) {
var b64string = event.target.result;
source.data = {‘contents’ : [b64string], ‘name’:[input.files[0].name]};
source.change.emit()
}
function error_handler(evt) {
if(evt.target.error.name == “NotReadableError”) {
alert(“Can’t read file!”);
}
}
var input = document.createElement(‘input’);
input.setAttribute(‘type’, ‘file’);
input.onchange = function(){
if (window.FileReader) {
read_file(input.files[0]);
} else {
alert(‘FileReader is not supported in this browser’);
}
}
input.click();
“”"
``
With regards,
Timur.
On Thursday, 23 November 2017 09:49:14 UTC+3, sai jena wrote:
Hello Everyone,
I want to load image from local drive for bokeh server. I am facing difficulty in this. Please help me out by giving the proper step like where(path) to store the image and the command needed to import the image.
Thanks and regards
SAI JENA