CustomJS to open excel file using Button within browser with Bokeh Server

Hi,
if I use this code

···

from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import Figure, show
from bokeh.models.widgets import Button
x = [x*0.005 for x in range(0, 200)]
y = x
nomeFileExcel=“file:/C:/Eclipse/eclipse-workspace/BiDia/src/mauro.xlsx”
source = ColumnDataSource(data=dict(x=x, y=y))
bottoneCallback = CustomJS(args=dict(source=source),
code="""
var A= ‘{0}’;
window.open(A);
“”" .format (str(nomeFileExcel)))

bottone=Button(label=“testo”,button_type=“success”,name=“nome”,callback=bottoneCallback)
layout = column(bottone)
show(layout)

All work very fine

BUT if I try to use the same code with Bokeh Server

from os.path import dirname, join
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import Figure, show
from bokeh.models.widgets import Button
from bokeh.plotting import curdoc
x = [x*0.005 for x in range(0, 200)]
y = x
nomeFileExcel=“file:/C:/Eclipse/eclipse-workspace/BiDia/src/mauro.xlsx”
source = ColumnDataSource(data=dict(x=x, y=y))
bottoneCallback = CustomJS(args=dict(source=source),
code="""
var A= ‘{0}’;
window.open(A);
“”" .format (str(nomeFileExcel)))

bottone=Button(label=“testo”,button_type=“success”,name=“nome”,callback=bottoneCallback)
layout = column(bottone)
curdoc().add_root(layout)

http://localhost:5006/testWindowOpenServe

bokeh serve testWindowOpenServe


The file in not open and the console web tell me this errror:

Error: Access to ‘file:///C:/Eclipse/eclipse-workspace/BiDia/src/mauro.xlsx’ from script denied

In wich way I can do a window.open usinf Bokeh Server?

Kind regards

Mauro

Hi Mauro,

Such access is forbidden because it’s a security concern - otherwise, random websites would be able to just download data from you computer.

You can use the directory format with “bokeh serve”: https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#directory-format

With it, you can make your Excel file available via the webserver itself, so you would access it via URL similar to http://localhost:5006/static/mauro.xlsx

Regards,

Eugene

···

On Tuesday, February 6, 2018 at 4:53:45 AM UTC+7, [email protected] wrote:

Hi,
if I use this code

from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import Figure, show
from bokeh.models.widgets import Button
x = [x*0.005 for x in range(0, 200)]
y = x
nomeFileExcel=“file:/C:/Eclipse/eclipse-workspace/BiDia/src/mauro.xlsx”
source = ColumnDataSource(data=dict(x=x, y=y))
bottoneCallback = CustomJS(args=dict(source=source),
code=“”"
var A= ‘{0}’;
window.open(A);
“”" .format (str(nomeFileExcel)))

bottone=Button(label=“testo”,button_type=“success”,name=“nome”,callback=bottoneCallback)
layout = column(bottone)
show(layout)

All work very fine

BUT if I try to use the same code with Bokeh Server

from os.path import dirname, join
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import Figure, show
from bokeh.models.widgets import Button
from bokeh.plotting import curdoc
x = [x*0.005 for x in range(0, 200)]
y = x
nomeFileExcel=“file:/C:/Eclipse/eclipse-workspace/BiDia/src/mauro.xlsx”
source = ColumnDataSource(data=dict(x=x, y=y))
bottoneCallback = CustomJS(args=dict(source=source),
code=“”"
var A= ‘{0}’;
window.open(A);
“”" .format (str(nomeFileExcel)))

bottone=Button(label=“testo”,button_type=“success”,name=“nome”,callback=bottoneCallback)
layout = column(bottone)
curdoc().add_root(layout)

http://localhost:5006/testWindowOpenServe

bokeh serve testWindowOpenServe


The file in not open and the console web tell me this errror:

Error: Access to ‘file:///C:/Eclipse/eclipse-workspace/BiDia/src/mauro.xlsx’ from script denied

In wich way I can do a window.open usinf Bokeh Server?

Kind regards

Mauro

Hi Eugene

Hello
thanks for your support.
Excuse me but I could not fully understand your suggestion.

In this folder
C:\Eclipse\eclipse-workspace\BiDia\src\static
exists a file with name mauro.xlsx

I open a cmd session and i do
cd C:\Eclipse\eclipse-workspace\BiDia

then I call Bokeh in this way
bokeh serve testWindowOpenServe.py

If I try to do in the program testWindowsOpenServe.py a
windows.open('http://localhost:5006/testWindowOpenServe/static/mauro.xlsx')
or a
windows.open('http://localhost:5006/testWindowOpenServe/mauro.xlsx)
or a
windows.open('http://localhost:5006/static/mauro.xlsx)

I see allways this error: 404 not found

Where am I doing wrong?
Thanks again for your kindness
Mauro

Hi Mauro,

Sorry for the waiting.

In order for the directory-based layout to work, you have to use “bokeh serve %dir_name%”, where “%dir_name%” is the relative path to the directory that contains “main.py” file.

The app name, in this case, would be the directory name, so the URL would be http://localhost:5006/dir_name/static/mauro.xlsx

Note that you don’t have to specify the absolute URL in “window.open”, you can just write “window.open(‘dir_name/static/mauro.xlsx’)”.

Regards,

Eugene

···

On Wednesday, February 7, 2018 at 5:48:16 AM UTC+7, [email protected] wrote:

Hi Eugene

Hello
thanks for your support.
Excuse me but I could not fully understand your suggestion.

In this folder
C:\Eclipse\eclipse-workspace\BiDia\src\static
exists a file with name mauro.xlsx

I open a cmd session and i do
cd C:\Eclipse\eclipse-workspace\BiDia

then I call Bokeh in this way
bokeh serve testWindowOpenServe.py

If I try to do in the program testWindowsOpenServe.py a
windows.open('[http://localhost:5006/testWindowOpenServe/static/mauro.xlsx](http://localhost:5006/testWindowOpenServe/static/mauro.xlsx)')
or a
windows.open('[http://localhost:5006/testWindowOpenServe/mauro.xlsx](http://localhost:5006/testWindowOpenServe/mauro.xlsx))
or a
windows.open('[http://localhost:5006/static/mauro.xlsx](http://localhost:5006/static/mauro.xlsx))

I see allways this error: 404 not found


Where am I doing wrong?
Thanks again for your kindness
Mauro


Hi there,

it seems to me that only files in the “static” folder are available via the URL. Is that correct?

Thanks!

···

Am Samstag, 10. Februar 2018 22:19:36 UTC+1 schrieb Eugene Pakhomov:

Hi Mauro,

Sorry for the waiting.

In order for the directory-based layout to work, you have to use “bokeh serve %dir_name%”, where “%dir_name%” is the relative path to the directory that contains “main.py” file.

The app name, in this case, would be the directory name, so the URL would be http://localhost:5006/dir_name/static/mauro.xlsx

Note that you don’t have to specify the absolute URL in “window.open”, you can just write “window.open(‘dir_name/static/mauro.xlsx’)”.

Regards,

Eugene

On Wednesday, February 7, 2018 at 5:48:16 AM UTC+7, [email protected] wrote:

Hi Eugene

Hello
thanks for your support.
Excuse me but I could not fully understand your suggestion.

In this folder
C:\Eclipse\eclipse-workspace\BiDia\src\static
exists a file with name mauro.xlsx

I open a cmd session and i do
cd C:\Eclipse\eclipse-workspace\BiDia

then I call Bokeh in this way
bokeh serve testWindowOpenServe.py

If I try to do in the program testWindowsOpenServe.py a
windows.open('[http://localhost:5006/testWindowOpenServe/static/mauro.xlsx](http://localhost:5006/testWindowOpenServe/static/mauro.xlsx)')
or a
windows.open('[http://localhost:5006/testWindowOpenServe/mauro.xlsx](http://localhost:5006/testWindowOpenServe/mauro.xlsx))
or a
windows.open('[http://localhost:5006/static/mauro.xlsx](http://localhost:5006/static/mauro.xlsx))

I see allways this error: 404 not found


Where am I doing wrong?
Thanks again for your kindness
Mauro


That is correct.

  • Eugene
···

On Wednesday, February 14, 2018 at 7:29:09 PM UTC+7, Joris Nettelstroth wrote:

Hi there,

it seems to me that only files in the “static” folder are available via the URL. Is that correct?

Thanks!

Am Samstag, 10. Februar 2018 22:19:36 UTC+1 schrieb Eugene Pakhomov:

Hi Mauro,

Sorry for the waiting.

In order for the directory-based layout to work, you have to use “bokeh serve %dir_name%”, where “%dir_name%” is the relative path to the directory that contains “main.py” file.

The app name, in this case, would be the directory name, so the URL would be http://localhost:5006/dir_name/static/mauro.xlsx

Note that you don’t have to specify the absolute URL in “window.open”, you can just write “window.open(‘dir_name/static/mauro.xlsx’)”.

Regards,

Eugene

On Wednesday, February 7, 2018 at 5:48:16 AM UTC+7, [email protected] wrote:

Hi Eugene

Hello
thanks for your support.
Excuse me but I could not fully understand your suggestion.

In this folder
C:\Eclipse\eclipse-workspace\BiDia\src\static
exists a file with name mauro.xlsx

I open a cmd session and i do
cd C:\Eclipse\eclipse-workspace\BiDia

then I call Bokeh in this way
bokeh serve testWindowOpenServe.py

If I try to do in the program testWindowsOpenServe.py a
windows.open('[http://localhost:5006/testWindowOpenServe/static/mauro.xlsx](http://localhost:5006/testWindowOpenServe/static/mauro.xlsx)')
or a
windows.open('[http://localhost:5006/testWindowOpenServe/mauro.xlsx](http://localhost:5006/testWindowOpenServe/mauro.xlsx))
or a
windows.open('[http://localhost:5006/static/mauro.xlsx](http://localhost:5006/static/mauro.xlsx))

I see allways this error: 404 not found


Where am I doing wrong?
Thanks again for your kindness
Mauro


Hi ,

If I use the [http://localhost:5006 all work, but if I use
–allow-websocket-origin 192.168.56.115:5006
I allway have the 404 error.
How I can use windows.open in bokeh with --allow-websocket-origin option?
Kind regards
Mauro Putzu

@maurogigliotti7 You will need to describe your situation and what it is you are actually trying to accomplish more. It’s not clear if you even need to set --allow-websocket-origin or not. A rule of thumb is that the HTTP ORIGIN header represents “what is in the browser URL address bar”. i.e.

  • If you are embedding a Bokeh app in a page that is served at foo.com then the ORIGIN is "foo.com" and that’s what you need to tell Bokeh to allow

  • You are navigating directly Bokeh server at IP:PORT Then that ip and port comprise the ORIGIN and that’s what you need to tell bokeh to allow.

If you want to be able to connect locally (e.g from “localhost”) and remotely to an IP address, then you would need to add flags for both. You can add as many --allow-websocket-origin flags as you need. If you don’t specify anything at all then Bokeh uses "localhost:5006" as a default allowed origin, but as soon as you specify any value explicitly, then only the values you set explicity are used (i.e. you would have to manually add back "localhost:5006" if you still want it).

I try to explain myself

I am using
bokeh serves

The server in wich is running bokeh is connect to a LAN with several PCs.

I am using the DataTable widget and I have written a function that starts from a DataTable
builds an excel file that is written in the static folder of bokeh.

I would like to open the excel file that goes into the static folder in all the PC browsers that are
connected to the LAN.

If I use localhost with the command
window.open
I can open the file.

But if I use localhost I cannot connect to other PC over the LAN.

If I use --allow-websocket-origin I can connect other PC on the LAN
but i can’t open the static folder file anymore.
The browser gives me a 404 error

How can I do?

Kind regards

@maurogigliotti7 I answered that right above:

If you want to be able to connect locally (e.g from “localhost”) and remotely to an IP address, then you would need to add flags for both . You can add as many --allow-websocket-origin flags as you need.