Skip to content

Instantly share code, notes, and snippets.

@philippjfr
Forked from xavArtley/TaskControler_FixServer.ipynb
Created December 23, 2018 19:08
Show Gist options
  • Save philippjfr/59660de703df1fb548ee8a6de2ea1f2f to your computer and use it in GitHub Desktop.
Save philippjfr/59660de703df1fb548ee8a6de2ea1f2f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import holoviews as hv\n",
"import panel as pn\n",
"import numpy as np\n",
"import param\n",
"import time\n",
"pn.extension()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class TaskControllerBase(param.Parameterized):\n",
" \"\"\" Base class for widgets of type loading\n",
" add method control to create start and stop buttons\n",
" \"\"\"\n",
" t=param.Number(default=0., precedence=-1)\n",
" interval = param.Number(default=0.1, precedence=-1, doc=\"\"\"\n",
" Minimal time interval in seconds between two updates of the progress bar.\"\"\")\n",
" start = param.Action(lambda inst : inst._start_update())\n",
" stop = param.Action(lambda inst : inst._stop_update())\n",
"\n",
" def __init__(self, **params):\n",
" super(TaskControllerBase,self).__init__(**params)\n",
" self._stop = True\n",
" self._running = False\n",
" \n",
" def _update(self):\n",
" while not self._stop:\n",
" time.sleep(self.interval)\n",
" self.t = time.time()\n",
" self.t = time.time()\n",
" self._running = False\n",
" return\n",
" \n",
" def _start_update(self):\n",
" import threading\n",
" if self._running:\n",
" raise RuntimeError('Previous thread already running. Stop it before relaunching')\n",
" self._stop = False\n",
" self._running = True\n",
" self._thread = threading.Thread(target=self._update)\n",
" self._thread.start()\n",
" self.param.trigger('start')\n",
" \n",
" def _stop_update(self):\n",
" self._stop = True\n",
" self.param.trigger('stop')\n",
" \n",
" def controls(self):\n",
" return pn.Row(pn.Param(self,parameters=['start'],show_name=False),\n",
" pn.Param(self,parameters=['stop'],show_name=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class ControllerWithBar(TaskControllerBase):\n",
" count = param.Number(default=0., precedence=-1)\n",
" \n",
" @param.depends('t')\n",
" def _update_text(self):\n",
" if not self._stop:\n",
" self.count += 1\n",
" return pn.pane.Markdown('####Working' + ' .'*int(self.t%6), height=0, width=200)\n",
" else:\n",
" self.count = 0\n",
" return pn.pane.Markdown('####Idle', height=0, width=200)\n",
" \n",
" @param.depends('count')\n",
" def _make_pb(self):\n",
" return hv.Points(((0.1*self.count)%1.1,0.5)).opts(height=25,xaxis=None,yaxis=None,toolbar=None,xlim=(0.,1.),size=15)\n",
" \n",
" def view(self):\n",
" pb = hv.DynamicMap(self._make_pb)\n",
" return pn.Row(pn.Spacer(width=150), pn.Column(self._update_text, pb))\n",
" \n",
" def layout(self):\n",
" return pn.Column(self.controls(), self.view())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"controller = ControllerWithBar()\n",
"controller.layout()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment