Bokeh server entry point

I provide a tool as part of my python package that renders a parameter space using bokeh. The usual way to start it:

$ bokeh serve --show my_package/tools/my_tool.py

Opens a browser and displays an interactive plot. However, when I install this package using pip / PyPI users, you cannot easily access this folder, so I would like to specify an entry point for this in my setup.py.

Package Layout:
folder
|
|--- my_package
|     |
|     |- __init__.py
|     |- __main__.py
|     |- some_code.py
|     |
|     +--- tools
|           |
|           +--- my_tool.py
|
+--setup.py

In my setup.py, I already specify an entry point to my main method:

setup.py
from setuptools import setup, find_packages

setup(
    name = "my_package",
    packages = find_packages(),
    entry_points = {
        'console_scripts': [
            'my_package = my_package.__main__:main'
          ]
    },
    [...],
)

However, the only way to start the bokeh server and show the interface is to create another python script like this

import os
from subprocess import call

def main():
    p = os.path.realpath(__file__)
    prefix, _ = os.path.split(p)
    bokeh_server_file = os.path.join(prefix, "my_tool.py")
    call(["bokeh", "serve", "--show", bokeh_server_file])

if __name__ == "__main__":
    main()

put it in a folder toolsand create an entry point for the main scripting method. * shudder * There must be a better way to do this.

​​ setuptools ?

+4
2

, , Embedding Bokeh Server . bokeh api . , , , .

+1

@jxramos, . tornado IO loop , bokeh script python script, .

from tornado.ioloop import IOLoop
from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.server.server import Server

def modify_doc(doc):
    """Function that modifies a document."""
    # [...] create a plot
    doc.add_root(plot)
    doc.title = "Test Plot"

def main():
    """Launch the server and connect to it."""
    io_loop = IOLoop.current()
    bokeh_app = Application(FunctionHandler(modify_doc)) # pass the function that assembles your document here.
    server = Server({"/": bokeh_app}, io_loop=io_loop)
    server.start()
    print("Opening Bokeh application on http://localhost:5006/")

    io_loop.add_callback(server.show, "/")
    io_loop.start()

main()

script

$ python my_tool.py

, , , :

[...]
entry_points = {
    'console_scripts': [
        'my_package = my_package.tools.my_tool:main'
      ]
},
[...]

, , . , , .

bokeh

bokeh .py , IOloop , . bokeh, , script setup.py. bokeh >= 0,12.4.

bokeh

local_server.py:

from tornado.ioloop import IOLoop

from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.server.server import Server


def modify_doc(doc):
    """Add a plotted function to the document.

    Arguments:
        doc: A bokeh document to which elements can be added.
    """
    x_values = range(10)
    y_values = [x ** 2 for x in x_values]
    data_source = ColumnDataSource(data=dict(x=x_values, y=y_values))
    plot = figure(title="f(x) = x^2",
                  tools="crosshair,pan,reset,save,wheel_zoom",)
    plot.line('x', 'y', source=data_source, line_width=3, line_alpha=0.6)
    doc.add_root(plot)
    doc.title = "Test Plot"


def main():
    """Launch the server and connect to it.
    """
    print("Preparing a bokeh application.")
    io_loop = IOLoop.current()
    bokeh_app = Application(FunctionHandler(modify_doc))

    server = Server({"/": bokeh_app}, io_loop=io_loop)
    server.start()
    print("Opening Bokeh application on http://localhost:5006/")

    io_loop.add_callback(server.show, "/")
    io_loop.start()


main()

$ python local_server.py

.

setup.py

script, setup.py. :

project
├── setup.py
└── my_package
    ├── __init__.py
    └── local_server.py

setup.py:

from setuptools import setup

setup(
    name = "my_package",
    entry_points={
        "console_scripts": ["my_script = my_package.local_server:main"],
    },
)

$ python setup.py install

$ my_script

bokeh , .

+4

Source: https://habr.com/ru/post/1672947/


All Articles