File: utils.py

package info (click to toggle)
dask.distributed 2022.12.1%2Bds.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,164 kB
  • sloc: python: 81,938; javascript: 1,549; makefile: 228; sh: 100
file content (65 lines) | stat: -rw-r--r-- 1,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations

from numbers import Number

import bokeh
from bokeh.core.properties import without_property_validation
from bokeh.io import curdoc
from packaging.version import Version
from tlz.curried import first

try:
    import numpy as np
except ImportError:
    np = None  # type: ignore

BOKEH_VERSION = Version(bokeh.__version__)

PROFILING = False

if BOKEH_VERSION.major < 3:
    _DATATABLE_STYLESHEETS_KWARGS = {}
else:
    _DATATABLE_STYLESHEETS_KWARGS = {
        "stylesheets": [
            """
    .bk-data-table {
    z-index: 0;
    }
    """
        ]
    }


def transpose(lod):
    return {k: [d[k] for d in lod] for k in lod[0]}


@without_property_validation
def update(source, data):
    """Update source with data

    This checks a few things first

    1.  If the data is the same, then don't update
    2.  If numpy is available and the data is numeric, then convert to numpy
        arrays
    3.  If profiling then perform the update in another callback
    """
    if not np or not any(isinstance(v, np.ndarray) for v in source.data.values()):
        if source.data == data:
            return
    if np and len(data[first(data)]) > 10:
        d = {}
        for k, v in data.items():
            if type(v) is not np.ndarray and isinstance(v[0], Number):
                d[k] = np.array(v)
            else:
                d[k] = v
    else:
        d = data

    if PROFILING:
        curdoc().add_next_tick_callback(lambda: source.data.update(d))
    else:
        source.data.update(d)