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 = WebSocket('my_app')
progressbar = ProgressBar(ws, min=0, max=100)
# Client emitted `my_event` event
@ws.on
def my_event(socket, data): pass
# Client emitted ``module_progressbar_start_progression`` event
@progressbar.on
def start_progression(socket, data):
def my_func():
for value in range(progressbar.min, progressbar.max):
time.sleep(.1) # Emulate a slow task :^)
progressbar.tick(label="[%d/%d] Task #%d is done" % (progressbar.value, progressbar.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
(websocket, min=0, max=100)[source]¶ Initialize a new ProgressBar module instance.
If
min
andmax
values are equal, this progress bar has its indeterminate state set toTrue
.Parameters: - websocket (tornado_websockets.websocket.WebSocket) – Instance of
WebSocket
- min (int) – Minimum value
- max (int) – Maximum value
- websocket (tornado_websockets.websocket.WebSocket) – Instance of
4.1.2.2. Methods¶
-
ProgressBar.
tick
(label=None)[source]¶ Increments progress bar’s _value 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.1.2.3. Events¶
-
ProgressBar.
on
(callback)¶ Shortcut for
tornado_websockets.websocket.WebSocket.on()
decorator, but with a specific prefix for each module.:ction or a class method. :type callback: Callable :return:
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.