|
# Bokeh Server on CF: https://github.com/snowch/bokeh-cloud-foundry |
|
|
|
from tornado.ioloop import IOLoop |
|
import yaml |
|
|
|
from bokeh.application.handlers import FunctionHandler |
|
from bokeh.application import Application |
|
from bokeh.server.server import Server |
|
from bokeh.themes import Theme |
|
|
|
import pandas as pd |
|
|
|
import os |
|
|
|
# if running locally, listen on port 5000 |
|
PORT = int(os.getenv('PORT', '5000')) |
|
HOST = "0.0.0.0" |
|
|
|
# this is set in the manifest |
|
try: |
|
ALLOW_WEBSOCKET_ORIGIN = os.getenv("ALLOW_WEBSOCKET_ORIGIN").split(',') |
|
except: |
|
ALLOW_WEBSOCKET_ORIGIN = ['localhost:{0}'.format(PORT)] |
|
|
|
io_loop = IOLoop.current() |
|
|
|
|
|
def get_db_connection(): |
|
"""write your DB conn here""" |
|
return conn |
|
|
|
|
|
def get_data(conn): |
|
data = pd.read_sql("select * from table", conn) |
|
return data |
|
|
|
|
|
# Create the main plot |
|
def create_map_figure(data): |
|
from colorcet import bgy |
|
from holoviews.operation.datashader import datashade |
|
import geoviews.tile_sources as gts |
|
import holoviews as hv |
|
renderer = hv.renderer('bokeh') |
|
from datashader.geo import lnglat_to_meters |
|
shade_pd = data[["localtime", "from_lon", "from_lat", "to_lon", "to_lat"]] |
|
# datashader and bokeh work with mercator proj |
|
shade_pd.loc[:, 'from_lon'], shade_pd.loc[:, 'from_lat'] = lnglat_to_meters(shade_pd["from_lon"], |
|
shade_pd["from_lat"]) |
|
shade_pd.loc[:, 'to_lon'], shade_pd.loc[:, 'to_lat'] = lnglat_to_meters(shade_pd["to_lon"], shade_pd["to_lat"]) |
|
plot_width = 800 |
|
plot_height = 400 |
|
tile_opts = dict(width=plot_width, height=plot_height, xaxis=None, yaxis=None, show_grid=False) |
|
map_tiles = gts.CartoLight.opts(style=dict(alpha=1), plot=tile_opts) |
|
lines = hv.Curve(shade_pd[["from_lon", "from_lat", "to_lon", "to_lat"]], ["from_lon", "from_lat"], |
|
["to_lon", "to_lat"]) |
|
routes = datashade(lines, x_sampling=2, y_sampling=2, cmap=bgy, alpha=50) |
|
return map_tiles*routes |
|
|
|
|
|
def modify_doc(doc): |
|
import holoviews as hv |
|
|
|
conn = get_db_connection() |
|
df = get_data(conn) |
|
plot_map = create_map_figure(df) |
|
renderer = hv.renderer('bokeh').instance(mode='server') |
|
plot_map_rendered = renderer.get_plot(plot_map, doc) |
|
|
|
doc.add_root(plot_map_rendered.state) |
|
|
|
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 |
|
""")) |
|
|
|
|
|
bokeh_app = Application(FunctionHandler(modify_doc)) |
|
|
|
|
|
server = Server( |
|
{'/': bokeh_app}, |
|
io_loop=io_loop, |
|
allow_websocket_origin=ALLOW_WEBSOCKET_ORIGIN, |
|
**{'port': PORT, 'address': HOST} |
|
) |
|
server.start() |
|
|
|
if __name__ == '__main__': |
|
io_loop.add_callback(server.show, "/") |
|
io_loop.start() |
Hi Andreas, I am struggling to get this cf push to work. Is this dependent on a specific version of CF, or a particular version of the python buildpack?
I am using cf version 6.46.0+29d6257f1.2019-07-09
I am running this: cf push -b https://github.com/cloudfoundry/python-buildpack.git#v1.7.8 -s cflinuxfs3
I am using requirements.txt and letting the buildpack grab the latest
here is the error I am getting:
Collecting cartopy>=0.16.0 (from geoviews->-r /tmp/app/requirements.txt (line 6))
Downloading https://files.pythonhosted.org/packages/e5/92/fe8838fa8158931906dfc4f16c5c1436b3dd2daf83592645b179581403ad/Cartopy-0.17.0.tar.gz (8.9MB)
Installing build dependencies: started
Installing build dependencies: finished with status 'done'
Getting requirements to build wheel: started
Getting requirements to build wheel: finished with status 'error'
ERROR: Command errored out with exit status 1:
command: /tmp/contents101194754/deps/0/bin/python /tmp/contents101194754/deps/0/python/lib/python3.8/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /tmp/tmpxhkesfis
cwd: /tmp/pip-install-4ihvp4ef/cartopy
Complete output (3 lines):
setup.py:168: UserWarning: Unable to determine GEOS version. Ensure you have 3.3.3 or later installed, or installation may fail.
warnings.warn(
Proj 4.9.0 must be installed.
----------------------------------------
ERROR: Command errored out with exit status 1: /tmp/contents101194754/deps/0/bin/python /tmp/contents101194754/deps/0/python/lib/python3.8/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /tmp/tmpxhkesfis Check the logs for full command output.
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
ERROR Could not install pip packages: could not run pip: exit status 1
Failed to compile droplet: Failed to run all supply scripts: exit status 14
Exit status 223
Cell 35701fdb-a8bb-428a-8aa0-baea347fe61a stopping instance 53ffc075-6f0b-4158-b957-06d4bafd4837
Cell 35701fdb-a8bb-428a-8aa0-baea347fe61a destroying container for instance 53ffc075-6f0b-4158-b957-06d4bafd4837
Cell 35701fdb-a8bb-428a-8aa0-baea347fe61a successfully destroyed container for instance 53ffc075-6f0b-4158-b957-06d4bafd4837
FAILED
Error restarting application: BuildpackCompileFailed