4. Modules — Django-tornado-websockets 0.1.3 documentation

# 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)

  Initialize a new ProgressBar module instance.

  If `min` and `max` values are equal, this progress bar has its indeterminate state set to `True`.

  Parameters: | websocket (tornado_websockets.websocket.WebSocket) – Instance of `WebSocket` min (int) – Minimum value max (int) – Maximum value

#### 4.1.2.2. Methods

`ProgressBar.` `reset`()

  Reset progress bar’s progression to its minimum value.

`ProgressBar.` `tick`(label=None)

  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`()

  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)

  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`()

  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)

  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`()

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