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 min and max values are equal, this progress bar has its indeterminate state set to True.

Parameters:
  • path (str) – WebSocket path, see tornado_websockets.websocket.WebSocket
  • min (int) – Minimum _value
  • max (int) – Maximum _value

4.1.2.2. Methods

ProgressBar.reset()[source]

Reset progress bar’s progression to its minimum value.

ProgressBar.tick(label=None)[source]

Increments progress bar’s _value by 1 and emit update event. Can also emit done event if progression is done.

Call emit_update() method each time this method is called. Call emit_done() method if progression is done.

Parameters:label (str) – A label which can be displayed on the client screen
ProgressBar.is_done()[source]

Return True if progress bar’s progression is done, otherwise False.

Returns False if progress bar is indeterminate, returns True if progress bar is determinate and current value is equals to max value. Returns False by default.

Return type:bool

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:callback parameter.
ProgressBar.emit_init()[source]

Emit before_init, init and after_init events to initialize a client-side progress bar.

If progress bar is not indeterminate, min, max and value values are sent with init event.

ProgressBar.emit_update(label=None)[source]

Emit before_update, update and after_update events to update a client-side progress bar.

Parameters:label (str) – A label which can be displayed on the client screen
ProgressBar.emit_done()[source]

Emit done event when progress bar’s progression is_done().