4. Modules¶
4.1. Progress bar¶
Provide an interface to easily handle a progress bar on both server and client sides.
4.1.1. Example¶
4.1.1.1. Server-side¶
import time
import threading
from tornado_websockets.modules.progress_bar import ProgressBar
ws_pb = ProgressBar('/my_progress_bar', min=0, max=100)
# Client emitted ``start_progression`` event
@ws_pb.on
def start_progression():
def my_func():
for value in range(ws_pb.min, ws_pb.max):
time.sleep(.1) # Emulate a slow task :^)
ws_pb.tick(label="[%d/%d] Task #%d is done" % (ws_pb.value, ws_pb.max, value))
threading.Thread(None, my_func, None).start()
4.1.1.2. Client-side¶
Read client-side documentation on https://docs.kocal.fr/dtws-client-module-progressbar.
4.1.2. Usage¶
4.1.2.1. Construction¶
-
class
tornado_websockets.modules.progress_bar.ProgressBar(path, min=0, max=100, add_to_handlers=True)[source]¶ Initialize a new ProgressBar module instance.
If
minandmaxvalues are equal, this progress bar has its indeterminate state set toTrue.Parameters: - path (str) – WebSocket path, see
tornado_websockets.websocket.WebSocket - min (int) – Minimum _value
- max (int) – Maximum _value
- path (str) – WebSocket path, see
4.1.2.2. Methods¶
-
ProgressBar.tick(label=None)[source]¶ Increments progress bar’s _value by
1and emitupdateevent. Can also emitdoneevent 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.1.2.3. Events¶
-
ProgressBar.on(callback)[source]¶ Shortcut for
tornado_websockets.websocket.WebSocket.on()decorator.Parameters: callback (Callable) – Function or a class method. Returns: callbackparameter.
-
ProgressBar.emit_init()[source]¶ Emit
before_init,initandafter_initevents to initialize a client-side progress bar.If progress bar is not indeterminate,
min,maxandvaluevalues are sent withinitevent.