|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "tags": [] |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "## Serving a Bokeh application in Voila" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import yaml\n", |
| 19 | + "import os\n", |
| 20 | + "\n", |
| 21 | + "from bokeh.layouts import column\n", |
| 22 | + "from bokeh.models import ColumnDataSource, Slider\n", |
| 23 | + "from bokeh.plotting import figure\n", |
| 24 | + "from bokeh.themes import Theme\n", |
| 25 | + "from bokeh.io import show, output_notebook\n", |
| 26 | + "\n", |
| 27 | + "from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature\n", |
| 28 | + "\n", |
| 29 | + "output_notebook()" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "Serving a Bokeh app in Voila is the same as serving it in JupyterLab, see https://docs.bokeh.org/en/latest/docs/user_guide/output/jupyter.html#jupyterlab" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": null, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "def bkapp(doc):\n", |
| 46 | + " df = sea_surface_temperature.copy()\n", |
| 47 | + " source = ColumnDataSource(data=df)\n", |
| 48 | + "\n", |
| 49 | + " plot = figure(x_axis_type='datetime', y_range=(0, 25),\n", |
| 50 | + " y_axis_label='Temperature (Celsius)',\n", |
| 51 | + " title=\"Sea Surface Temperature at 43.18, -70.43\")\n", |
| 52 | + " plot.line('time', 'temperature', source=source)\n", |
| 53 | + "\n", |
| 54 | + " def callback(attr, old, new):\n", |
| 55 | + " if new == 0:\n", |
| 56 | + " data = df\n", |
| 57 | + " else:\n", |
| 58 | + " data = df.rolling('{0}D'.format(new)).mean()\n", |
| 59 | + " source.data = ColumnDataSource.from_df(data)\n", |
| 60 | + "\n", |
| 61 | + " slider = Slider(start=0, end=30, value=0, step=1, title=\"Smoothing by N Days\")\n", |
| 62 | + " slider.on_change('value', callback)\n", |
| 63 | + "\n", |
| 64 | + " doc.add_root(column(slider, plot))\n", |
| 65 | + "\n", |
| 66 | + " doc.theme = Theme(json=yaml.load(\"\"\"\n", |
| 67 | + " attrs:\n", |
| 68 | + " Figure:\n", |
| 69 | + " background_fill_color: \"#DDDDDD\"\n", |
| 70 | + " outline_line_color: white\n", |
| 71 | + " toolbar_location: above\n", |
| 72 | + " height: 500\n", |
| 73 | + " width: 800\n", |
| 74 | + " Grid:\n", |
| 75 | + " grid_line_dash: [6, 4]\n", |
| 76 | + " grid_line_color: white\n", |
| 77 | + " \"\"\", Loader=yaml.FullLoader))" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "**Note**: You need to specify the Voila URL to Bokeh, by passing it to the `show` method:\n", |
| 85 | + "\n", |
| 86 | + "```python\n", |
| 87 | + "import os\n", |
| 88 | + "from urllib.parse import urlparse\n", |
| 89 | + "\n", |
| 90 | + "url = urlparse(os.environ.get(\"VOILA_SERVER_URL\"))\n", |
| 91 | + "\n", |
| 92 | + "show(bkapp, notebook_url=f\"{url.scheme}://{url.netloc}\")\n", |
| 93 | + "```" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": null, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "import os\n", |
| 103 | + "from urllib.parse import urlparse\n", |
| 104 | + "\n", |
| 105 | + "url = urlparse(os.environ.get(\"VOILA_SERVER_URL\"))\n", |
| 106 | + "\n", |
| 107 | + "show(bkapp, notebook_url=f\"{url.scheme}://{url.netloc}\")" |
| 108 | + ] |
| 109 | + } |
| 110 | + ], |
| 111 | + "metadata": { |
| 112 | + "anaconda-cloud": {}, |
| 113 | + "kernelspec": { |
| 114 | + "display_name": "Python 3 (ipykernel)", |
| 115 | + "language": "python", |
| 116 | + "name": "python3" |
| 117 | + }, |
| 118 | + "language_info": { |
| 119 | + "codemirror_mode": { |
| 120 | + "name": "ipython", |
| 121 | + "version": 3 |
| 122 | + }, |
| 123 | + "file_extension": ".py", |
| 124 | + "mimetype": "text/x-python", |
| 125 | + "name": "python", |
| 126 | + "nbconvert_exporter": "python", |
| 127 | + "pygments_lexer": "ipython3", |
| 128 | + "version": "3.11.0" |
| 129 | + } |
| 130 | + }, |
| 131 | + "nbformat": 4, |
| 132 | + "nbformat_minor": 4 |
| 133 | +} |
0 commit comments