4. Modules — Django-tornado-websockets 0.2.1 documentation

# 4. Modules

## 4.1. Module

class `tornado_websockets.modules.` `Module`(name='')

## 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()` or `reset()`

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)

  Initialize a new ProgressBar module instance.

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

  Parameters: | min (int) – Minimum value max (int) – Maximum value

#### 4.2.1.2. Methods

`ProgressBar.` `reset`()

  Reset progress bar’s progression to its minimum value.

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

  Increments progress bar’s _current 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.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`()

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

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

### 4.2.2. Client-side

Read documentation about ProgressBar client-side module here.