4. Modules¶
4.2. Progress bar¶
The module « ProgressBar » facilitate the communication between the server-side and client-side of a progression bar.
Server-side:
- An easier communication with client-side ProgressBar module
- Handle init, update and done events,
- Update current progression value with
tick()
orreset()
Client-side:
- An easier communication with server-side ProgressBar module,
- Handle init, update and done events,
- Rendering a progression bar by using HTML5 or Bootstrap rendering.
4.2.1. Server-side¶
4.2.1.1. Construction¶
-
class
tornado_websockets.modules.
ProgressBar
(name='', min=0, max=100, indeterminate=False)[source]¶ Initialize a new ProgressBar module instance.
If
min
andmax
values are equal, this progress bar has its indeterminate state set toTrue
.Parameters: - min (int) – Minimum value
- max (int) – Maximum value
4.2.1.2. Methods¶
-
ProgressBar.
tick
(label=None)[source]¶ Increments progress bar’s _current by
1
and emitupdate
event. Can also emitdone
event if progression is done.Call
emit_update()
method each time this method is called. Callemit_done()
method if progression is done.Parameters: label (str) – A label which can be displayed on the client screen
4.2.1.3. Events¶
-
ProgressBar.
on
(callback)¶ Shortcut for
tornado_websockets.websocket.WebSocket.on()
decorator, but with a specific prefix for each module.Parameters: callback (Callable) – function or a class method. Returns: callback
parameter.
-
ProgressBar.
emit_init
()[source]¶ Emit
before_init
,init
andafter_init
events to initialize a client-side progress bar.If progress bar is not indeterminate,
min
,max
andvalue
values are sent withinit
event.
4.2.1.4. Example¶
from tornado import gen
from tornado_websockets.modules import ProgressBar
from tornado_websockets.websocket import WebSocket
ws = WebSocket('module_progressbar')
progressbar = ProgressBar('foo', min=0, max=100)
ws.bind(progressbar)
@progressbar.on
def reset():
progressbar.reset()
@progressbar.on
@gen.engine # Make this function asynchronous for Tornado's IOLoop
def start():
for value in range(0, progressbar.max):
yield gen.sleep(.1) # like time.sleep(), but asynchronous with @gen.engine
progressbar.tick(label="[%d/%d] Tâche %d terminée" % (progressbar.current + 1, progressbar.max, value))