Suggestions on creating server from Python script with a structure

Hello all,
I am new to Bokeh. I would like to discuss a particular application scenario.

I have a class TopClass. Its constructor initializes the document instance. In the constructor, some more objects are initialized, each of them represents a plot to be added in the Doc. Once, TopClass is initialized, I would call TopClass.run(). It will run a simulation and will update the data through the stream. Now, this TopClass is defined in a Python file which has main. Now, I would like to start a server from the Python file itself. Please see the example below:

class TopClass ():

def __init__(self, doc):

    self.doc = doc

    self.session = push_session(self.doc)

    print self.session.id

    self.plot1 = plot1(range1, frequency1)

    self.plot2 = plot2(range2, frequency2)

def main():

tb = TopClass(doc)

tb.start() ## This is not a blocking function. This is to show the sequence.

tb.stop()

tb.wait()

``

So, now I’d like to run only this Python file. It should initialize an Application, with a server and all. I think we can do something like this:

def main():

loop = IOLoop.current()

handler = FunctionHandler(TopClass.__init__)

app = Application(handler)

server = Server({"/":app}, io_loop = loop)

loop.add_callback(tb.start)

loop.add_callback(server.show, "/")

loop.start()

tb.stop()

tb.wait()

``

Now, as expected, this should fail. What I am interested in: python temp.py should start the server and set up the plots and Documents. If I call localhost:5006/?session=___, it should display the live plots. Through this previous example of main function, I’d like to suggest the use of IOloop, Server, and all. I need a blocking function like loop.start or server.run_until_shutdown. As soon as it stops with Ctrl+C, the simulation will be stopped. I tried some try and error with lots of ways, but not successful. Can anyone suggest me a more appropriate way to implement this? Maybe an example of some reference will be good, too.

Thank you.

After some more research, I’d like to add one more question:
Can I define on_server_load function of an application from the python file itself? Because, if it is possible, then I can add the required tasks directly into the on_server_load function.

···

On Tuesday, May 16, 2017 at 12:46:42 AM UTC+5:30, Kartik Patel wrote:

Hello all,
I am new to Bokeh. I would like to discuss a particular application scenario.

I have a class TopClass. Its constructor initializes the document instance. In the constructor, some more objects are initialized, each of them represents a plot to be added in the Doc. Once, TopClass is initialized, I would call TopClass.run(). It will run a simulation and will update the data through the stream. Now, this TopClass is defined in a Python file which has main. Now, I would like to start a server from the Python file itself. Please see the example below:

class TopClass ():

def __init__(self, doc):
    self.doc = doc
    self.session = push_session(self.doc)
    print [self.session.id](http://self.session.id)
    self.plot1 = plot1(range1, frequency1)
    self.plot2 = plot2(range2, frequency2)

def main():

tb = TopClass(doc)

tb.start() ## This is not a blocking function. This is to show the sequence.

tb.stop()
tb.wait()

``

So, now I’d like to run only this Python file. It should initialize an Application, with a server and all. I think we can do something like this:

def main():

loop = IOLoop.current()
handler = FunctionHandler(TopClass.__init__)
app = Application(handler)
server = Server({"/":app}, io_loop = loop)
loop.add_callback(tb.start)
loop.add_callback(server.show, "/")
loop.start()
tb.stop()
tb.wait()

``

Now, as expected, this should fail. What I am interested in: python temp.py should start the server and set up the plots and Documents. If I call localhost:5006/?session=___, it should display the live plots. Through this previous example of main function, I’d like to suggest the use of IOloop, Server, and all. I need a blocking function like loop.start or server.run_until_shutdown. As soon as it stops with Ctrl+C, the simulation will be stopped. I tried some try and error with lots of ways, but not successful. Can anyone suggest me a more appropriate way to implement this? Maybe an example of some reference will be good, too.

Thank you.

Hi,

Yes, this is possible. An "app" is really just a collection of handlers. There are default sets of handlers for reading files or functions, etc. But you can add handlers to the list too, including the handler that provides lifecycle hooks. See this GitHub comment for details:

  Passing objects from server to session · Issue #4978 · bokeh/bokeh · GitHub

But the gist is:

  class SessionHandler(Handler):

          def on_session_created(self, session_context):
          # do stuff

  bkapp.add(SessionHandler())

  server = Server({'/bkapp': bokeh_app}, io_loop=io_loop)
  
  server.start()
  
Thanks,

Bryan

···

On May 16, 2017, at 01:16, Kartik Patel <[email protected]> wrote:

After some more research, I'd like to add one more question:
Can I define `on_server_load` function of an application from the python file itself? Because, if it is possible, then I can add the required tasks directly into the `on_server_load` function.

On Tuesday, May 16, 2017 at 12:46:42 AM UTC+5:30, Kartik Patel wrote:
Hello all,
I am new to Bokeh. I would like to discuss a particular application scenario.

I have a class TopClass. Its constructor initializes the document instance. In the constructor, some more objects are initialized, each of them represents a plot to be added in the Doc. Once, TopClass is initialized, I would call TopClass.run(). It will run a simulation and will update the data through the stream. Now, this TopClass is defined in a Python file which has __main__. Now, I would like to start a server from the Python file itself. Please see the example below:

class TopClass ():
    def __init__(self, doc):
        self.doc = doc
        self.session = push_session(self.doc)
        print self.session.id

        self.plot1 = plot1(range1, frequency1)
        self.plot2 = plot2(range2, frequency2)

def main():
    tb = TopClass(doc)
    tb.start() ## This is not a blocking function. This is to show the sequence.
    tb.stop()
    tb.wait()

So, now I'd like to run only this Python file. It should initialize an Application, with a server and all. I think we can do something like this:

def main():
    loop = IOLoop.current()
    handler = FunctionHandler(TopClass.__init__)
    app = Application(handler)

    server = Server({"/":app}, io_loop = loop)

    loop.add_callback(tb.start)
    loop.add_callback(server.show, "/")

    loop.start()
    tb.stop()
    tb.wait()

Now, as expected, this should fail. What I am interested in: python temp.py should start the server and set up the plots and Documents. If I call localhost:5006/?session=___, it should display the live plots. Through this previous example of main function, I'd like to suggest the use of IOloop, Server, and all. I need a blocking function like loop.start or server.run_until_shutdown. As soon as it stops with Ctrl+C, the simulation will be stopped. I tried some try and error with lots of ways, but not successful. Can anyone suggest me a more appropriate way to implement this? Maybe an example of some reference will be good, too.

Thank you.

--
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/dd5332c2-d34a-4eb3-8793-1a26d5600324%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Dear Bryan,

Thank you for your reply.

I have found a workaround to this: Basically, I was starting a server through the external process and then used sessions and Documents. Now, I have created a Blank application (using Application()) and passed it to the server and turned on the server. Then I used all kind of sessions and Documents that I was previously using.

Hence. updated snippet looks like this:

# Define a blank application

app = Application()

# Starting server at port 5006

srv = Server({'/':app},io_loop=loop)

# Start server process

srv.start()

# Define the document instance

doc = curdoc()

session = push_session(doc, session_id="test", io_loop=loop, url='http://localhost:5006/')

# Create Top Block instance

tb = top_block_cls(doc)

# Start simulations as soon as the server starts

loop.add_callback(tb.start)

loop.start()

``