On Mar 25, 2019, at 7:11 AM, [email protected] wrote:
Hi Bryan,
More generally, I am using electricity usage patterns data, which is then cross referenced with a geo-data SHP file for the US states / weather zones.
This is academic work and I aim to end up with an interactive notebook showing some data analysis. Core code is in Python/Pandas and I working in Jupiter environment.
I think Bokeh would work great here as I could visually tie between geographic and data analysis
As the end result should be a dynamic one, I now understand I have to work with the Bokeh server so that the Python code and Bokeh presentation would actively communicate, so currently trying to understand how this all work.
so now... I followed this example which you wrote to embed a server in my notebook (using a 'show' command) however I fail to understand how to access parameter values, or print anything in the console, once the app is running.
e.g. below - I have added a couple of 'print' command which result in:
Couldn't process kernel message SyntaxError: Unexpected token c in JSON at position 0
at JSON.parse (<anonymous>)
at html_table (main.js?v=20190322093114:130)
at code_exec_callback (main.js?v=20190322093114:168)
at Kernel._handle_output_message (kernel.js:1196)
at i (jquery.min.js:2)
at Kernel._handle_iopub_message (kernel.js:1223)
at Kernel._finish_ws_message (kernel.js:1015)
at kernel.js:1006
(anonymous) @ kernel.js:1007
Promise.catch (async)
Kernel._handle_ws_message @ kernel.js:1007
i @ jquery.min.js:2
kernel.js:1007 Couldn't process kernel message SyntaxError: Unexpected number in JSON at position 2
at JSON.parse (<anonymous>)
at html_table (main.js?v=20190322093114:130)
at code_exec_callback (main.js?v=20190322093114:168)
at Kernel._handle_output_message (kernel.js:1196)
at i (jquery.min.js:2)
at Kernel._handle_iopub_message (kernel.js:1223)
at Kernel._finish_ws_message (kernel.js:1015)
at kernel.js:1006
(anonymous) @ kernel.js:1007
Promise.catch (async)
Kernel._handle_ws_message @ kernel.js:1007
i @ jquery.min.js:2
Strangely I don't get this in Internet Explorer (but yes on Chrome and Edge).
Many thanks
Noam
def modify_doc(doc):
df = sea_surface_temperature.copy()
source = ColumnDataSource(data=df)
plot = figure(x_axis_type='datetime', y_range=(0, 25),
y_axis_label='Temperature (Celsius)',
title="Sea Surface Temperature at 43.18, -70.43")
plot.line('time', 'temperature', source=source)
def callback(attr, old, new):
print ('callback event')
print (new)
if new == 0:
data = df
else:
data = df.rolling('{0}D'.format(new)).mean()
source.data = ColumnDataSource(data=data).data
slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
slider.on_change('value', callback)
doc.add_root(column(slider, plot))
doc.theme = Theme(json=yaml.load("""
attrs:
Figure:
background_fill_color: "#DDDDDD"
outline_line_color: white
toolbar_location: above
height: 500
width: 800
Grid:
grid_line_dash: [6, 4]
grid_line_color: white
"""))
On Friday, March 22, 2019 at 3:29:20 PM UTC, Bryan Van de ven wrote:
Hi,
Well there seems to be one confusion. The "show" function generates standalone, plain HTML output. It is actively not useful or intended to be used with Bokeh server applications. I can't really say more without a complete code sample to actually run, and instructions on how you are running it.
Thanks,
Bryan
> On Mar 22, 2019, at 2:43 AM, no-r...@forwardemail.net wrote:
>
> That is exactly the issue, I can't seem to be able to print in the console once inside the server routine.
>
> e.g. if I would add a print command to the callback above:
>
> def resetButtonCallback ():
> print ('callback event')
> c = cls (randrange(10), randrange(10))
> resetButton.label=str(c.a) + " " + str(c.b)
> # print (c.a)
>
> the function would run, buth 'callback event' will not be printed anywhere.
> For that line in the code I will get the following error:
>
> kernel.js:1007
>
> Couldn't process kernel message SyntaxError: Unexpected token c in JSON at position 0
> at JSON.parse (<anonymous>)
> at html_table (main.js?v=20190322093114:130)
> at code_exec_callback (main.js?v=20190322093114:168)
> at Kernel._handle_output_message (kernel.js:1196)
> at i (jquery.min.js:2)
> at Kernel._handle_iopub_message (kernel.js:1223)
> at Kernel._finish_ws_message (kernel.js:1015)
> at kernel.js:1006
> (anonymous) @ kernel.js:1007
> Promise.catch (async)
> Kernel._handle_ws_message @ kernel.js:1007
> i @ jquery.min.js:2
>
> this error then applies to any print command that is triggered by the show command
>
>
>
>
> On Friday, March 22, 2019 at 1:03:10 AM UTC, Bryan Van de ven wrote:
>
> Hi,
>
> That's roughly correct. More specifically, the Boker server keeps the JS and Python objects in sync at all times, and also allows python callbacks to be executed whenever those objects change.
>
> I'm not quite sure about your print question. I print from Bokeh server app callbacks all the time. The output is in the console or terminal where you started the Bokeh server, though, perhaps that is what is confusing? If you want something to show up in the browser, you will have to put it there, e.g. by updating the text in a Bokeh Div widget, or changing the text in a plot.
>
> Thanks,
>
> Bryan
>
> > On Mar 21, 2019, at 7:49 AM, no-r...@forwardemail.net wrote:
> >
> > Bryan,
> >
> > Thanks - I have implemented using a Bokeh server (I think) as below.
> >
> > So, to simplify for my poor-junior-programming-skills, once inside the server app, I am essentially in a JS environment, however python code, classes etc. can still be used - hope i got this part right.
> >
> > What I could not understand is how to 'print' values once inside the server app (for debugging) - Is it possible to display variables in the console once in my 'server_doc' function?
> >
> > Thanks
> >
> > from bokeh.models import CustomJS
> > from bokeh.layouts import widgetbox, row, column
> > from bokeh.io import output_notebook, show, output_file
> > import bokeh.io
> > from random import randrange
> > bokeh.io.output_notebook()
> >
> > ##############
> >
> > class cls():
> > def __init__(self, a, b):
> > self.a = a
> > self.b = b
> >
> > #####################
> >
> > def server_doc(doc):
> > c = cls(1,2)
> >
> > resetButton = Button(label="Reset", button_type="success")
> >
> > def resetButtonCallback ():
> > c = cls (randrange(10), randrange(10))
> > resetButton.label=str(c.a) + " " + str(c.b)
> > # print (c.a)
> >
> > resetButton.on_click(resetButtonCallback)
> >
> > doc.add_root(resetButton)
> >
> >
> > show(server_doc)
> >
> >
> > On Wednesday, March 20, 2019 at 3:22:41 PM UTC, Bryan Van de ven wrote:
> > Hi,
> >
> > You have a fundamental misunderstanding here. Once standalone Bokeh content like this (i.e. HTML created with "show") is displayed in abrowser, *there is no more Python process to execute anything*. The browser cannot run your Python code. As the name suggests, js_on_click is only capable of running *JavaScript* code, because the browser is only capable of running JavaScript code. If you want to connect widgets and events to real, actual Python callbacks, that is the purpose of the Bokeh server:
> >
> > Bokeh server — Bokeh 3.3.2 Documentation
> >
> > Thanks,
> >
> > Bryan
> >
> > > On Mar 20, 2019, at 5:46 AM, noa...@gmail.com wrote:
> > >
> > > I have an object class written in Python with some variables. later in the script, there is Bokeh Button with a callback function that should reinitiate the object:
> > >
> > > from bokeh.models import CustomJS
> > > from bokeh.layouts import widgetbox, row, column
> > > from bokeh.io import output_notebook, show, output_file
> > > import bokeh.io
> > >
> > > bokeh.io.reset_output()
> > > bokeh.io.output_notebook()
> > >
> > >
> > > ##############
> > >
> > > class cls():
> > > def __init__(self, a, b):
> > > self.a = a
> > > self.b = b
> > > ##this is infact a much longer and complex function, simplified for the disucssion...
> > >
> > > def reinitiate(self, a, b):
> > > return CustomJS(args=dict(A=a, B=b),code="""
> > > console.log(self)
> > > """)
> > >
> > > #####################
> > >
> > > c = cls(1,2)
> > > resetButton = Button(label="Reset", button_type="success")
> > >
> > > #this bring a ValueError: not all callback values are CustomJS instances
> > > # resetButton.js_on_click(c.__init__(3,4))
> > >
> > > show(resetButton)
> > >
> > > Is there a simple way of doing this without re-writing my initiation function in JS?
> > >
> > > I have also tried a 'reinitiate' funcion, but could not find a way to access the class' other functions through it.
> > >
> > > --
> > > You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> > > To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
> > > To post to this group, send email to bo...@continuum.io.
> > > To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/33e0f045-2ee8-479b-a66b-eaca33453afc%40continuum.io\.
> > > For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
> >
>
--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/f433df26-f9ee-4480-a420-f460537e2354%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.