Skip to content

Commit

Permalink
Fix various gallery examples (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 10, 2021
1 parent 709ef9b commit 01869de
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/gallery/param/loading_indicator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
" return hv.Points(np.random.rand(100, 2)).opts(\n",
" width=400, height=400, size=5)\n",
"\n",
"pn.Column(button, pn.panel(pn.bind(random_plot, button), loading_indicator=True))"
"pn.Column(button, pn.panel(pn.bind(random_plot, button), loading_indicator=True)).servable()"
]
}
],
Expand Down
120 changes: 96 additions & 24 deletions examples/gallery/simple/color_speech_recognition.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import panel as pn\n",
"import holoviews as hv\n",
"import numpy as np\n",
"\n",
"pn.extension(loading_spinner='dots', loading_color='#00aa41')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every pane, widget and layout provides the `loading` parameter. When set to `True` a spinner will overlay the panel and indicate that the panel is currently loading. When you set `loading` to false the spinner is removed. Using the `pn.extension` or by setting the equivalent parameters on `pn.config` we can select between different visual styles and colors for the loading indicator.\n",
"from panel.widgets import SpeechToText, GrammarList\n",
"\n",
"Using the `pn.param.set_values` context manager we can set the `loading` parameter to `True` while we perform some computation (here simulated using `time.sleep`:"
"pn.extension()"
]
},
{
Expand All @@ -29,23 +19,105 @@
"metadata": {},
"outputs": [],
"source": [
"button = pn.widgets.Button(name=\"Update\", margin=(0, 32, 0, 57))\n",
"speech_to_text_color = SpeechToText(button_type=\"light\", continuous=True)\n",
"\n",
"colors = [\n",
" \"aqua\",\n",
" \"azure\",\n",
" \"beige\",\n",
" \"bisque\",\n",
" \"black\",\n",
" \"blue\",\n",
" \"brown\",\n",
" \"chocolate\",\n",
" \"coral\",\n",
" \"crimson\",\n",
" \"cyan\",\n",
" \"fuchsia\",\n",
" \"ghostwhite\",\n",
" \"gold\",\n",
" \"goldenrod\",\n",
" \"gray\",\n",
" \"green\",\n",
" \"indigo\",\n",
" \"ivory\",\n",
" \"khaki\",\n",
" \"lavender\",\n",
" \"lime\",\n",
" \"linen\",\n",
" \"magenta\",\n",
" \"maroon\",\n",
" \"moccasin\",\n",
" \"navy\",\n",
" \"olive\",\n",
" \"orange\",\n",
" \"orchid\",\n",
" \"peru\",\n",
" \"pink\",\n",
" \"plum\",\n",
" \"purple\",\n",
" \"red\",\n",
" \"salmon\",\n",
" \"sienna\",\n",
" \"silver\",\n",
" \"snow\",\n",
" \"tan\",\n",
" \"teal\",\n",
" \"thistle\",\n",
" \"tomato\",\n",
" \"turquoise\",\n",
" \"violet\",\n",
" \"white\",\n",
" \"yellow\",\n",
"]\n",
"src = \"#JSGF V1.0; grammar colors; public <color> = \" + \" | \".join(colors) + \" ;\"\n",
"grammar_list = GrammarList()\n",
"grammar_list.add_from_string(src, 1)\n",
"\n",
"speech_to_text_color.grammars = grammar_list\n",
"\n",
"colors_html = \", \".join(\n",
" [f\"<span style='background:{color};'>{color}</span>\" for color in colors]\n",
")\n",
"content_html = f\"\"\"\n",
"<h1>Speech Color Changer</h1>\n",
"\n",
"<p>Tap/click the microphone icon and say a color to change the background color of the app. Try {colors_html}\n",
"\"\"\"\n",
"\n",
"def random_plot():\n",
" return hv.Points(np.random.rand(100, 2)).opts(\n",
" width=400, height=400, size=5)\n",
"content_panel = pn.pane.HTML(content_html, sizing_mode=\"stretch_width\")\n",
"\n",
"hv_pane = pn.pane.HoloViews(random_plot())\n",
"app = pn.Column(height=500, width=500, css_classes=[\"color-app\"])\n",
"style_panel = pn.pane.HTML(width=0, height=0, sizing_mode=\"fixed\")\n",
"\n",
"def update(event):\n",
" with pn.param.set_values(hv_pane, loading=True):\n",
" time.sleep(10)\n",
" hv_pane.object = random_plot()\n",
" \n",
"button.on_click(update)\n",
"result_panel = pn.pane.Markdown(sizing_mode=\"stretch_width\")\n",
"\n",
"pn.Column(button, hv_pane)"
"@pn.depends(speech_to_text_color, watch=True)\n",
"def update_result_panel(results_last):\n",
" results_last = results_last.lower()\n",
" if results_last in colors:\n",
" app.background = results_last\n",
" result_panel.object = \"Result received: \" + results_last\n",
" else:\n",
" app.background = \"white\"\n",
" result_panel.object = \"Result received: \" + results_last + \" (Not recognized)\"\n",
"\n",
"app[:] = [\n",
" style_panel,\n",
" content_panel,\n",
" speech_to_text_color,\n",
" result_panel,\n",
"]\n",
"\n",
"app.servable()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
79 changes: 79 additions & 0 deletions examples/gallery/simple/loading_spinner.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import panel as pn\n",
"import holoviews as hv\n",
"import numpy as np\n",
"\n",
"pn.extension(loading_spinner='dots', loading_color='#00aa41')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every pane, widget and layout provides the `loading` parameter. When set to `True` a spinner will overlay the panel and indicate that the panel is currently loading. When you set `loading` to false the spinner is removed. Using the `pn.extension` or by setting the equivalent parameters on `pn.config` we can select between different visual styles and colors for the loading indicator.\n",
"\n",
"Using the `pn.param.set_values` context manager we can set the `loading` parameter to `True` while we perform some computation (here simulated using `time.sleep`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"button = pn.widgets.Button(name=\"Update\", margin=(0, 32, 0, 57))\n",
"\n",
"def random_plot():\n",
" return hv.Points(np.random.rand(100, 2)).opts(\n",
" width=400, height=400, size=5)\n",
"\n",
"hv_pane = pn.pane.HoloViews(random_plot())\n",
"\n",
"def update(event):\n",
" with pn.param.set_values(hv_pane, loading=True):\n",
" time.sleep(10)\n",
" hv_pane.object = random_plot()\n",
" \n",
"button.on_click(update)\n",
"\n",
"pn.Column(button, hv_pane).servable()"
]
}
],
"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.2"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 01869de

Please sign in to comment.