From 7563effdddb931252a8a30246dde8883e59aec3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renato=20C=C3=A9sar?= Date: Mon, 31 May 2021 01:34:29 -0300 Subject: [PATCH 1/2] Make build previewer accept host params and create example notebook --- ...example-mapshader-with-bokeh-jupyter.ipynb | 680 ++++++++++++++++++ mapshader/flask_app.py | 112 ++- mapshader/utils.py | 46 +- 3 files changed, 774 insertions(+), 64 deletions(-) create mode 100644 examples/example-mapshader-with-bokeh-jupyter.ipynb diff --git a/examples/example-mapshader-with-bokeh-jupyter.ipynb b/examples/example-mapshader-with-bokeh-jupyter.ipynb new file mode 100644 index 0000000..b184434 --- /dev/null +++ b/examples/example-mapshader-with-bokeh-jupyter.ipynb @@ -0,0 +1,680 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "745d20e1", + "metadata": {}, + "outputs": [], + "source": [ + "import dask.array as da\n", + "import datashader as ds\n", + "import noise\n", + "import numpy as np\n", + "import pandas as pd\n", + "import xarray as xr\n", + "\n", + "from mapshader.colors import colors\n", + "from mapshader.sources import MapSource, TileService, elevation_source\n", + "from mapshader.utils import build_previewer" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2f7202d2", + "metadata": {}, + "outputs": [], + "source": [ + "def make_terrain(\n", + " shape=(2**14, 2**14),\n", + " scale=100.0,\n", + " octaves=6,\n", + " persistence=0.5,\n", + " lacunarity=2.0,\n", + " chunks=(8192, 8192)\n", + "):\n", + " \"\"\"\n", + " Generate a pseudo-random terrain data dask array.\n", + " Parameters\n", + " ----------\n", + " shape : int or tuple of int, default=(2**14, 2**14)\n", + " Output array shape.\n", + " scale : float, default=100.0\n", + " Noise factor scale.\n", + " octaves : int, default=6\n", + " Number of waves when generating the noise.\n", + " persistence : float, default=0.5\n", + " Amplitude of each successive octave relative.\n", + " lacunarity : float, default=2.0\n", + " Frequency of each successive octave relative.\n", + " chunks : int or tuple of int, default=(8192, 8192)\n", + " Number of samples on each block.\n", + " Returns\n", + " -------\n", + " terrain : xarray.DataArray\n", + " 2D array of generated terrain values.\n", + " \"\"\"\n", + " def _func(arr, block_id=None):\n", + " block_ystart = block_id[0] * arr.shape[0]\n", + " block_xstart = block_id[1] * arr.shape[1]\n", + " out = np.zeros(arr.shape)\n", + " for i in range(out.shape[0]):\n", + " for j in range(out.shape[1]):\n", + " out[i][j] = noise.pnoise2(\n", + " (block_ystart + i)/scale,\n", + " (block_xstart + j)/scale,\n", + " octaves=octaves,\n", + " persistence=persistence,\n", + " lacunarity=lacunarity,\n", + " repeatx=1024,\n", + " repeaty=1024,\n", + " base=42,\n", + " )\n", + " return out\n", + " data = (\n", + " da.zeros(shape=shape, chunks=chunks, dtype=np.float32)\n", + " .map_blocks(_func, dtype=np.float32)\n", + " )\n", + " cvs = ds.Canvas(\n", + " x_range=(-20e6, 20e6),\n", + " y_range=(-20e6, 20e6),\n", + " plot_width=500,\n", + " plot_height=500,\n", + " )\n", + " hack_agg = cvs.points(pd.DataFrame({'x': [], 'y': []}), 'x', 'y')\n", + " agg = xr.DataArray(\n", + " data,\n", + " name='terrain',\n", + " coords=hack_agg.coords,\n", + " dims=hack_agg.dims,\n", + " attrs={'res': 1},\n", + " )\n", + " return agg" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b8cbfa53", + "metadata": {}, + "outputs": [], + "source": [ + "data = make_terrain(shape=(500, 500))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2ca3814f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dask.array.core.Array" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(data.data)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d8629e1f", + "metadata": {}, + "outputs": [], + "source": [ + "raster_src = MapSource.from_obj(elevation_source())" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "b95531a6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# ----------------------\n", + "# APPLYING TRANSFORMS Elevation\n", + "# ----------------------\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "raster_src.data = data\n", + "raster_src.transforms = []\n", + "raster_src.cmap = colors['viridis']\n", + "raster_src.span = (-0.349882, 0.43575)\n", + "raster_src.load()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "1fc77634", + "metadata": {}, + "outputs": [], + "source": [ + "service = TileService(source=raster_src)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ac2fc0c3", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " * Serving Flask app 'mapshader.flask_app' (lazy loading)\n", + " * Environment: production\n", + "\u001b[31m WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n", + "\u001b[2m Use a production WSGI server instead.\u001b[0m\n", + " * Debug mode: off\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n", + "127.0.0.1 - - [31/May/2021 01:06:09] \"GET / HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:09] \"GET /favicon.ico HTTP/1.1\" 404 -\n", + "127.0.0.1 - - [31/May/2021 01:06:11] \"GET /elevation-tile HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:15] \"GET /psutil HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:16] \"GET /elevation-tile/tile/2/0/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:16] \"GET /elevation-tile/tile/2/1/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:16] \"GET /elevation-tile/tile/1/1/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:16] \"GET /elevation-tile/tile/1/0/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:16] \"GET /elevation-tile/tile/1/0/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:16] \"GET /elevation-tile/tile/1/1/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:17] \"GET /psutil HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:18] \"GET /elevation-tile/tile/2/3/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:18] \"GET /elevation-tile/tile/2/2/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:18] \"GET /elevation-tile/tile/2/2/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:18] \"GET /elevation-tile/tile/2/0/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:18] \"GET /elevation-tile/tile/2/1/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:18] \"GET /elevation-tile/tile/2/3/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:20] \"GET /psutil HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:21] \"GET /elevation-tile/tile/2/2/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:21] \"GET /elevation-tile/tile/2/3/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:21] \"GET /elevation-tile/tile/2/0/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:21] \"GET /elevation-tile/tile/2/1/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:21] \"GET /elevation-tile/tile/2/0/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:21] \"GET /elevation-tile/tile/2/1/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:22] \"GET /elevation-tile/tile/2/2/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:22] \"GET /elevation-tile/tile/2/3/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:23] \"GET /psutil HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:06:25] \"GET /psutil HTTP/1.1\" 200 -\n" + ] + } + ], + "source": [ + "from mapshader.flask_app import *\n", + "\n", + "start_flask_app_jupyter([service, ])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5f4cde82", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "
\n", + " \n", + " Loading BokehJS ...\n", + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "\n", + "(function(root) {\n", + " function now() {\n", + " return new Date();\n", + " }\n", + "\n", + " var force = true;\n", + "\n", + " if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n", + " root._bokeh_onload_callbacks = [];\n", + " root._bokeh_is_loading = undefined;\n", + " }\n", + "\n", + " var JS_MIME_TYPE = 'application/javascript';\n", + " var HTML_MIME_TYPE = 'text/html';\n", + " var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", + " var CLASS_NAME = 'output_bokeh rendered_html';\n", + "\n", + " /**\n", + " * Render data to the DOM node\n", + " */\n", + " function render(props, node) {\n", + " var script = document.createElement(\"script\");\n", + " node.appendChild(script);\n", + " }\n", + "\n", + " /**\n", + " * Handle when an output is cleared or removed\n", + " */\n", + " function handleClearOutput(event, handle) {\n", + " var cell = handle.cell;\n", + "\n", + " var id = cell.output_area._bokeh_element_id;\n", + " var server_id = cell.output_area._bokeh_server_id;\n", + " // Clean up Bokeh references\n", + " if (id != null && id in Bokeh.index) {\n", + " Bokeh.index[id].model.document.clear();\n", + " delete Bokeh.index[id];\n", + " }\n", + "\n", + " if (server_id !== undefined) {\n", + " // Clean up Bokeh references\n", + " var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", + " cell.notebook.kernel.execute(cmd, {\n", + " iopub: {\n", + " output: function(msg) {\n", + " var id = msg.content.text.trim();\n", + " if (id in Bokeh.index) {\n", + " Bokeh.index[id].model.document.clear();\n", + " delete Bokeh.index[id];\n", + " }\n", + " }\n", + " }\n", + " });\n", + " // Destroy server and session\n", + " var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", + " cell.notebook.kernel.execute(cmd);\n", + " }\n", + " }\n", + "\n", + " /**\n", + " * Handle when a new output is added\n", + " */\n", + " function handleAddOutput(event, handle) {\n", + " var output_area = handle.output_area;\n", + " var output = handle.output;\n", + "\n", + " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", + " if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n", + " return\n", + " }\n", + "\n", + " var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n", + "\n", + " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", + " toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n", + " // store reference to embed id on output_area\n", + " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", + " }\n", + " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", + " var bk_div = document.createElement(\"div\");\n", + " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", + " var script_attrs = bk_div.children[0].attributes;\n", + " for (var i = 0; i < script_attrs.length; i++) {\n", + " toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", + " toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n", + " }\n", + " // store reference to server id on output_area\n", + " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", + " }\n", + " }\n", + "\n", + " function register_renderer(events, OutputArea) {\n", + "\n", + " function append_mime(data, metadata, element) {\n", + " // create a DOM node to render to\n", + " var toinsert = this.create_output_subarea(\n", + " metadata,\n", + " CLASS_NAME,\n", + " EXEC_MIME_TYPE\n", + " );\n", + " this.keyboard_manager.register_events(toinsert);\n", + " // Render to node\n", + " var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", + " render(props, toinsert[toinsert.length - 1]);\n", + " element.append(toinsert);\n", + " return toinsert\n", + " }\n", + "\n", + " /* Handle when an output is cleared or removed */\n", + " events.on('clear_output.CodeCell', handleClearOutput);\n", + " events.on('delete.Cell', handleClearOutput);\n", + "\n", + " /* Handle when a new output is added */\n", + " events.on('output_added.OutputArea', handleAddOutput);\n", + "\n", + " /**\n", + " * Register the mime type and append_mime function with output_area\n", + " */\n", + " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", + " /* Is output safe? */\n", + " safe: true,\n", + " /* Index of renderer in `output_area.display_order` */\n", + " index: 0\n", + " });\n", + " }\n", + "\n", + " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", + " if (root.Jupyter !== undefined) {\n", + " var events = require('base/js/events');\n", + " var OutputArea = require('notebook/js/outputarea').OutputArea;\n", + "\n", + " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", + " register_renderer(events, OutputArea);\n", + " }\n", + " }\n", + "\n", + " \n", + " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", + " root._bokeh_timeout = Date.now() + 5000;\n", + " root._bokeh_failed_load = false;\n", + " }\n", + "\n", + " var NB_LOAD_WARNING = {'data': {'text/html':\n", + " \"
\\n\"+\n", + " \"

\\n\"+\n", + " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", + " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", + " \"

\\n\"+\n", + " \"
    \\n\"+\n", + " \"
  • re-rerun `output_notebook()` to attempt to load from CDN again, or
  • \\n\"+\n", + " \"
  • use INLINE resources instead, as so:
  • \\n\"+\n", + " \"
\\n\"+\n", + " \"\\n\"+\n", + " \"from bokeh.resources import INLINE\\n\"+\n", + " \"output_notebook(resources=INLINE)\\n\"+\n", + " \"\\n\"+\n", + " \"
\"}};\n", + "\n", + " function display_loaded() {\n", + " var el = document.getElementById(\"1064\");\n", + " if (el != null) {\n", + " el.textContent = \"BokehJS is loading...\";\n", + " }\n", + " if (root.Bokeh !== undefined) {\n", + " if (el != null) {\n", + " el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n", + " }\n", + " } else if (Date.now() < root._bokeh_timeout) {\n", + " setTimeout(display_loaded, 100)\n", + " }\n", + " }\n", + "\n", + "\n", + " function run_callbacks() {\n", + " try {\n", + " root._bokeh_onload_callbacks.forEach(function(callback) {\n", + " if (callback != null)\n", + " callback();\n", + " });\n", + " } finally {\n", + " delete root._bokeh_onload_callbacks\n", + " }\n", + " console.debug(\"Bokeh: all callbacks have finished\");\n", + " }\n", + "\n", + " function load_libs(css_urls, js_urls, callback) {\n", + " if (css_urls == null) css_urls = [];\n", + " if (js_urls == null) js_urls = [];\n", + "\n", + " root._bokeh_onload_callbacks.push(callback);\n", + " if (root._bokeh_is_loading > 0) {\n", + " console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", + " return null;\n", + " }\n", + " if (js_urls == null || js_urls.length === 0) {\n", + " run_callbacks();\n", + " return null;\n", + " }\n", + " console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", + " root._bokeh_is_loading = css_urls.length + js_urls.length;\n", + "\n", + " function on_load() {\n", + " root._bokeh_is_loading--;\n", + " if (root._bokeh_is_loading === 0) {\n", + " console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n", + " run_callbacks()\n", + " }\n", + " }\n", + "\n", + " function on_error(url) {\n", + " console.error(\"failed to load \" + url);\n", + " }\n", + "\n", + " for (let i = 0; i < css_urls.length; i++) {\n", + " const url = css_urls[i];\n", + " const element = document.createElement(\"link\");\n", + " element.onload = on_load;\n", + " element.onerror = on_error.bind(null, url);\n", + " element.rel = \"stylesheet\";\n", + " element.type = \"text/css\";\n", + " element.href = url;\n", + " console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n", + " document.body.appendChild(element);\n", + " }\n", + "\n", + " const hashes = {\"https://cdn.bokeh.org/bokeh/release/bokeh-2.3.2.min.js\": \"XypntL49z55iwGVUW4qsEu83zKL3XEcz0MjuGOQ9SlaaQ68X/g+k1FcioZi7oQAc\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.3.2.min.js\": \"bEsM86IHGDTLCS0Zod8a8WM6Y4+lafAL/eSiyQcuPzinmWNgNO2/olUF0Z2Dkn5i\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.3.2.min.js\": \"TX0gSQTdXTTeScqxj6PVQxTiRW8DOoGVwinyi1D3kxv7wuxQ02XkOxv0xwiypcAH\"};\n", + "\n", + " for (let i = 0; i < js_urls.length; i++) {\n", + " const url = js_urls[i];\n", + " const element = document.createElement('script');\n", + " element.onload = on_load;\n", + " element.onerror = on_error.bind(null, url);\n", + " element.async = false;\n", + " element.src = url;\n", + " if (url in hashes) {\n", + " element.crossOrigin = \"anonymous\";\n", + " element.integrity = \"sha384-\" + hashes[url];\n", + " }\n", + " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", + " document.head.appendChild(element);\n", + " }\n", + " };\n", + "\n", + " function inject_raw_css(css) {\n", + " const element = document.createElement(\"style\");\n", + " element.appendChild(document.createTextNode(css));\n", + " document.body.appendChild(element);\n", + " }\n", + "\n", + " \n", + " var js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.3.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.3.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.3.2.min.js\"];\n", + " var css_urls = [];\n", + " \n", + "\n", + " var inline_js = [\n", + " function(Bokeh) {\n", + " Bokeh.set_log_level(\"info\");\n", + " },\n", + " function(Bokeh) {\n", + " \n", + " \n", + " }\n", + " ];\n", + "\n", + " function run_inline_js() {\n", + " \n", + " if (root.Bokeh !== undefined || force === true) {\n", + " \n", + " for (var i = 0; i < inline_js.length; i++) {\n", + " inline_js[i].call(root, root.Bokeh);\n", + " }\n", + " if (force === true) {\n", + " display_loaded();\n", + " }} else if (Date.now() < root._bokeh_timeout) {\n", + " setTimeout(run_inline_js, 100);\n", + " } else if (!root._bokeh_failed_load) {\n", + " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", + " root._bokeh_failed_load = true;\n", + " } else if (force !== true) {\n", + " var cell = $(document.getElementById(\"1064\")).parents('.cell').data().cell;\n", + " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", + " }\n", + "\n", + " }\n", + "\n", + " if (root._bokeh_is_loading === 0) {\n", + " console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", + " run_inline_js();\n", + " } else {\n", + " load_libs(css_urls, js_urls, function() {\n", + " console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", + " run_inline_js();\n", + " });\n", + " }\n", + "}(window));" + ], + "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n var NB_LOAD_WARNING = {'data': {'text/html':\n \"
\\n\"+\n \"

\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"

\\n\"+\n \"
    \\n\"+\n \"
  • re-rerun `output_notebook()` to attempt to load from CDN again, or
  • \\n\"+\n \"
  • use INLINE resources instead, as so:
  • \\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"
\"}};\n\n function display_loaded() {\n var el = document.getElementById(\"1064\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n\n function on_error(url) {\n console.error(\"failed to load \" + url);\n }\n\n for (let i = 0; i < css_urls.length; i++) {\n const url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n }\n\n const hashes = {\"https://cdn.bokeh.org/bokeh/release/bokeh-2.3.2.min.js\": \"XypntL49z55iwGVUW4qsEu83zKL3XEcz0MjuGOQ9SlaaQ68X/g+k1FcioZi7oQAc\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.3.2.min.js\": \"bEsM86IHGDTLCS0Zod8a8WM6Y4+lafAL/eSiyQcuPzinmWNgNO2/olUF0Z2Dkn5i\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.3.2.min.js\": \"TX0gSQTdXTTeScqxj6PVQxTiRW8DOoGVwinyi1D3kxv7wuxQ02XkOxv0xwiypcAH\"};\n\n for (let i = 0; i < js_urls.length; i++) {\n const url = js_urls[i];\n const element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\n element.async = false;\n element.src = url;\n if (url in hashes) {\n element.crossOrigin = \"anonymous\";\n element.integrity = \"sha384-\" + hashes[url];\n }\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n };\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n \n var js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.3.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.3.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.3.2.min.js\"];\n var css_urls = [];\n \n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n function(Bokeh) {\n \n \n }\n ];\n\n function run_inline_js() {\n \n if (root.Bokeh !== undefined || force === true) {\n \n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\n if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"1064\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(css_urls, js_urls, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "(function(root) {\n", + " function embed_document(root) {\n", + " \n", + " var docs_json = {\"8be4786c-e602-40f2-9dd3-4ab7b29fe534\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"background_fill_color\":\"black\",\"below\":[{\"id\":\"1074\"}],\"center\":[{\"id\":\"1077\"},{\"id\":\"1081\"}],\"left\":[{\"id\":\"1078\"}],\"renderers\":[{\"id\":\"1090\"},{\"id\":\"1093\"}],\"sizing_mode\":\"stretch_both\",\"title\":{\"id\":\"1095\"},\"toolbar\":{\"id\":\"1085\"},\"toolbar_location\":\"above\",\"x_range\":{\"id\":\"1066\"},\"x_scale\":{\"id\":\"1070\"},\"y_range\":{\"id\":\"1068\"},\"y_scale\":{\"id\":\"1072\"}},\"id\":\"1065\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"1084\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1102\",\"type\":\"AllLabels\"},{\"attributes\":{\"max_zoom\":15,\"url\":\"http://127.0.0.1:5000/elevation-tile/tile/{z}/{x}/{y}\"},\"id\":\"1092\",\"type\":\"WMTSTileSource\"},{\"attributes\":{},\"id\":\"1083\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1072\",\"type\":\"LinearScale\"},{\"attributes\":{\"alpha\":0.1,\"tile_source\":{\"id\":\"1089\"}},\"id\":\"1090\",\"type\":\"TileRenderer\"},{\"attributes\":{\"render_parents\":false,\"tile_source\":{\"id\":\"1092\"}},\"id\":\"1093\",\"type\":\"TileRenderer\"},{\"attributes\":{\"axis\":{\"id\":\"1074\"},\"grid_line_alpha\":0,\"ticker\":null},\"id\":\"1077\",\"type\":\"Grid\"},{\"attributes\":{\"formatter\":{\"id\":\"1097\"},\"major_label_policy\":{\"id\":\"1099\"},\"ticker\":{\"id\":\"1075\"},\"visible\":false},\"id\":\"1074\",\"type\":\"LinearAxis\"},{\"attributes\":{\"axis\":{\"id\":\"1078\"},\"dimension\":1,\"grid_line_alpha\":0,\"ticker\":null},\"id\":\"1081\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1075\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1079\",\"type\":\"BasicTicker\"},{\"attributes\":{\"attribution\":\"Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.\",\"url\":\"https://stamen-tiles.a.ssl.fastly.net/toner-background/{Z}/{X}/{Y}.png\"},\"id\":\"1089\",\"type\":\"WMTSTileSource\"},{\"attributes\":{\"formatter\":{\"id\":\"1100\"},\"major_label_policy\":{\"id\":\"1102\"},\"ticker\":{\"id\":\"1079\"},\"visible\":false},\"id\":\"1078\",\"type\":\"LinearAxis\"},{\"attributes\":{\"end\":20037508.3427892,\"start\":-20037508.3427892},\"id\":\"1066\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"1097\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"end\":20037508.3427892,\"start\":-20037508.3427892},\"id\":\"1068\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"1100\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1099\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1082\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1070\",\"type\":\"LinearScale\"},{\"attributes\":{\"active_multi\":null,\"tools\":[{\"id\":\"1082\"},{\"id\":\"1083\"},{\"id\":\"1084\"}]},\"id\":\"1085\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1095\",\"type\":\"Title\"}],\"root_ids\":[\"1065\"]},\"title\":\"Bokeh Application\",\"version\":\"2.3.2\"}};\n", + " var render_items = [{\"docid\":\"8be4786c-e602-40f2-9dd3-4ab7b29fe534\",\"root_ids\":[\"1065\"],\"roots\":{\"1065\":\"da34db32-daf6-49aa-8ff6-2231c5555f93\"}}];\n", + " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", + "\n", + " }\n", + " if (root.Bokeh !== undefined) {\n", + " embed_document(root);\n", + " } else {\n", + " var attempts = 0;\n", + " var timer = setInterval(function(root) {\n", + " if (root.Bokeh !== undefined) {\n", + " clearInterval(timer);\n", + " embed_document(root);\n", + " } else {\n", + " attempts++;\n", + " if (attempts > 100) {\n", + " clearInterval(timer);\n", + " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", + " }\n", + " }\n", + " }, 10, root)\n", + " }\n", + "})(window);" + ], + "application/vnd.bokehjs_exec.v0+json": "" + }, + "metadata": { + "application/vnd.bokehjs_exec.v0+json": { + "id": "1065" + } + }, + "output_type": "display_data" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "127.0.0.1 - - [31/May/2021 01:07:57] \"GET /elevation-tile/tile/1/0/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:07:57] \"GET /elevation-tile/tile/1/1/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:07:57] \"GET /elevation-tile/tile/2/3/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:07:57] \"GET /elevation-tile/tile/2/2/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:07:57] \"GET /elevation-tile/tile/1/0/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:07:57] \"GET /elevation-tile/tile/1/1/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:00] \"GET /elevation-tile/tile/2/2/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:00] \"GET /elevation-tile/tile/2/3/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:00] \"GET /elevation-tile/tile/2/3/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:00] \"GET /elevation-tile/tile/2/3/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:00] \"GET /elevation-tile/tile/2/2/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:00] \"GET /elevation-tile/tile/2/2/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:02] \"GET /elevation-tile/tile/2/0/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:02] \"GET /elevation-tile/tile/2/0/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:02] \"GET /elevation-tile/tile/2/1/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:02] \"GET /elevation-tile/tile/2/1/1 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:02] \"GET /elevation-tile/tile/2/0/0 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:02] \"GET /elevation-tile/tile/2/1/3 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:03] \"GET /elevation-tile/tile/2/0/2 HTTP/1.1\" 200 -\n", + "127.0.0.1 - - [31/May/2021 01:08:03] \"GET /elevation-tile/tile/2/1/2 HTTP/1.1\" 200 -\n" + ] + } + ], + "source": [ + "from bokeh.plotting import show\n", + "from bokeh.io import output_notebook\n", + "\n", + "\n", + "output_notebook()\n", + "\n", + "\n", + "show(build_previewer(service, host='http://127.0.0.1:5000'))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/mapshader/flask_app.py b/mapshader/flask_app.py index c7a8432..83d8365 100644 --- a/mapshader/flask_app.py +++ b/mapshader/flask_app.py @@ -1,12 +1,8 @@ from functools import partial -from mapshader.utils import psutil_fetching, psutils_html +from typing import List import sys -from bokeh.plotting import figure -from bokeh.models.tiles import WMTSTileSource from bokeh.embed import components -from bokeh.tile_providers import STAMEN_TONER_BACKGROUND -from bokeh.tile_providers import get_provider from jinja2 import Template @@ -29,6 +25,10 @@ from mapshader.sources import MapSource from mapshader.sources import MapService +from mapshader.utils import build_previewer +from mapshader.utils import psutil_fetching +from mapshader.utils import psutils_html + def flask_to_tile(source: MapSource, z=0, x=0, y=0): @@ -83,39 +83,13 @@ def flask_to_legend(source: MapSource): return resp -def build_previewer(service: MapService): - '''Helper function for creating a simple Bokeh figure with - a WMTS Tile Source. - Notes - ----- - - if you don't supply height / width, stretch_both sizing_mode is used. - - supply an output_dir to write figure to disk. - ''' - - xmin, ymin, xmax, ymax = service.default_extent - - p = figure(sizing_mode='stretch_both', - x_range=(xmin, xmax), - y_range=(ymin, ymax), - toolbar_location='above', - tools="pan,wheel_zoom,reset") - tile_provider = get_provider(STAMEN_TONER_BACKGROUND) - p.add_tile(tile_provider, alpha=.1) - - p.background_fill_color = 'black' - p.grid.grid_line_alpha = 0 - p.axis.visible = True - - if service.service_type == 'tile': - - tile_source = WMTSTileSource(url=service.client_url, - min_zoom=0, - max_zoom=15) - - p.add_tile(tile_source, render_parents=False) - - p.axis.visible = False - return p +VIEW_FUNC_CREATORS = { + 'tile': flask_to_tile, + 'image': flask_to_image, + 'wms': flask_to_wms, + 'geojson': flask_to_geojson, + 'legend': flask_to_legend, +} def service_page(service: MapService): @@ -233,37 +207,32 @@ def index_page(services): return html -def configure_app(app: Flask, user_source_filepath=None, contains=None): +def add_service_urls(app, service): + view_func = VIEW_FUNC_CREATORS[service.service_type] - CORS(app) + # add operational endpoint + app.add_url_rule(service.service_url, + service.name, + partial(view_func, source=service.source)) - view_func_creators = { - 'tile': flask_to_tile, - 'image': flask_to_image, - 'wms': flask_to_wms, - 'geojson': flask_to_geojson, - 'legend': flask_to_legend, - } + # add legend endpoint + app.add_url_rule(service.legend_url, + service.legend_name, + partial(VIEW_FUNC_CREATORS['legend'], source=service.source)) - services = [] - for service in get_services(config_path=user_source_filepath, contains=contains): - services.append(service) + # add service page endpoint + app.add_url_rule(service.service_page_url, + service.service_page_name, + partial(service_page, service=service)) - view_func = view_func_creators[service.service_type] - # add operational endpoint - app.add_url_rule(service.service_url, - service.name, - partial(view_func, source=service.source)) - # add legend endpoint - app.add_url_rule(service.legend_url, - service.legend_name, - partial(view_func_creators['legend'], source=service.source)) +def configure_app(app: Flask, user_source_filepath=None, contains=None): + CORS(app) - # add service page endpoint - app.add_url_rule(service.service_page_url, - service.service_page_name, - partial(service_page, service=service)) + services = [] + for service in get_services(config_path=user_source_filepath, contains=contains): + services.append(service) + add_service_urls(app, service) app.add_url_rule('/', 'home', partial(index_page, services=services)) app.add_url_rule('/psutil', 'psutil', psutil_fetching) @@ -273,6 +242,23 @@ def configure_app(app: Flask, user_source_filepath=None, contains=None): return app +def start_flask_app_jupyter(services: List[MapService]): + import threading + + def handler(): + app = Flask(__name__) + CORS(app) + + for service in services: + add_service_urls(app, service) + + app.add_url_rule('/', 'home', partial(index_page, services=services)) + app.add_url_rule('/psutil', 'psutil', psutil_fetching) + app.run() + + threading.Thread(target=handler).start() + + def create_app(user_source_filepath=None, contains=None): app = Flask(__name__) return configure_app(app, user_source_filepath, contains) diff --git a/mapshader/utils.py b/mapshader/utils.py index 7f69c57..a8d719a 100644 --- a/mapshader/utils.py +++ b/mapshader/utils.py @@ -1,5 +1,14 @@ -import numpy as np import psutil +import numpy as np + +from bokeh.plotting import figure +from bokeh.models.tiles import WMTSTileSource +from bokeh.embed import components +from bokeh.tile_providers import STAMEN_TONER_BACKGROUND +from bokeh.tile_providers import get_provider + +from mapshader.sources import MapService + def find_and_set_categoricals(df): ''' @@ -203,3 +212,38 @@ def psutils_html(): setInterval(fetchAndPopulate, 2000); ''' + + +def build_previewer(service: MapService, host: str=''): + '''Helper function for creating a simple Bokeh figure with + a WMTS Tile Source. + Notes + ----- + - if you don't supply height / width, stretch_both sizing_mode is used. + - supply an output_dir to write figure to disk. + ''' + xmin, ymin, xmax, ymax = service.default_extent + + p = figure(sizing_mode='stretch_both', + x_range=(xmin, xmax), + y_range=(ymin, ymax), + toolbar_location='above', + tools="pan,wheel_zoom,reset") + tile_provider = get_provider(STAMEN_TONER_BACKGROUND) + p.add_tile(tile_provider, alpha=.1) + + p.background_fill_color = 'black' + p.grid.grid_line_alpha = 0 + p.axis.visible = True + + url = service.client_url if not host else host + service.client_url + + if service.service_type == 'tile': + tile_source = WMTSTileSource(url=url, + min_zoom=0, + max_zoom=15) + + p.add_tile(tile_source, render_parents=False) + + p.axis.visible = False + return p From e4e7d21c550a293ca62132774ea85e0aef07f90a Mon Sep 17 00:00:00 2001 From: Giancarlo Castro Date: Tue, 13 Jul 2021 11:32:14 -0300 Subject: [PATCH 2/2] fix pep8 code format --- mapshader/flask_app.py | 6 ------ mapshader/utils.py | 5 ++--- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/mapshader/flask_app.py b/mapshader/flask_app.py index 9327caa..17489da 100644 --- a/mapshader/flask_app.py +++ b/mapshader/flask_app.py @@ -3,10 +3,6 @@ import sys from bokeh.embed import components -from bokeh.models.sources import GeoJSONDataSource -from bokeh.plotting import figure -from bokeh.models.tiles import WMTSTileSource - from jinja2 import Environment, FileSystemLoader @@ -29,7 +25,6 @@ from mapshader.sources import MapSource from mapshader.utils import build_previewer from mapshader.utils import psutil_fetching -from mapshader.utils import psutils_html jinja2_env = Environment(loader=FileSystemLoader("mapshader/templates/")) @@ -87,7 +82,6 @@ def flask_to_legend(source: MapSource): return resp - VIEW_FUNC_CREATORS = { 'tile': flask_to_tile, 'image': flask_to_image, diff --git a/mapshader/utils.py b/mapshader/utils.py index 106fa17..fed628f 100644 --- a/mapshader/utils.py +++ b/mapshader/utils.py @@ -3,11 +3,10 @@ from bokeh.plotting import figure from bokeh.models.tiles import WMTSTileSource -from bokeh.embed import components from bokeh.tile_providers import STAMEN_TONER_BACKGROUND from bokeh.tile_providers import get_provider -from mapshader.sources import MapService +from mapshader.services import MapService def find_and_set_categoricals(df): @@ -76,7 +75,7 @@ def psutil_fetching(): return log -def build_previewer(service: MapService, host: str=''): +def build_previewer(service: MapService, host: str = ''): '''Helper function for creating a simple Bokeh figure with a WMTS Tile Source. Notes