Bokeh callbacks

The interactive parts of the bokeh charts – the date slider, its Play button, the hover tooltips – run in the browser, as javascript. Each callback is a file of pyvoa/js/, which pyvoa.visu_bokeh reads and hands to a bokeh CustomJS (or CustomJSHover) along with the objects it acts on.

The code of a file is the body of a function: its parameters, listed below, are those objects, the keys of the args of the CustomJS, and cb_obj, the widget whose change fires it. Some of them repeat in javascript what the python side draws before the slider moves, and are tested against it in tests/test_js.py.

Every callback is part of pyvoa, by Tristan Beau, Julien Browaeys and Olivier Dadoun, under the licence of the package.

animation_callback(cb_obj, slider)

The Play / Pause button of the bokeh date slider.

Toggled on, it advances the slider by one date every 100 ms, restarting from the first date when it stands on the last, and turns itself off at the end; toggled off, it stops there. Each step moves the slider, which fires the slider’s own callback.

Arguments:
  • cb_obj (Toggle) – the button itself, handed by bokeh to the callback it fires.

  • slider (Slider) – the date slider the button drives.

Notes

The timer is kept on the button itself, as cb_obj._pyvoa_play: a second click must find the one the first click started, and a CustomJS callback keeps no state of its own between two calls. On window it would be shared by every Play button of the page, so that with two charts in one notebook, pausing one would stop the other.

Source: pyvoa/js/animation_callback.js, the comment left out.

if (cb_obj.active) {
    // toggled on: start a timer, unless one is running already
    if (!cb_obj._pyvoa_play) {
        // started on the last date, where the slider opens: play from the first
        if (slider.value >= slider.end) {
            slider.value = slider.start;
        }
        cb_obj._pyvoa_play = setInterval(function() {
            let v = slider.value + 1;

            if (v > slider.end) {
                // past the last date: stop, and set the button back to Play
                clearInterval(cb_obj._pyvoa_play);
                cb_obj._pyvoa_play = null;
                cb_obj.active = false;
                cb_obj.label = '► Play';
                return;
            }
            // moving the slider fires its own callback, which redraws the chart
            slider.value = v;
        }, 100);
        cb_obj.label = '❚❚ Pause';
    }
} else {
    // toggled off: stop the timer, the slider stays on the date reached
    if (cb_obj._pyvoa_play) {
        clearInterval(cb_obj._pyvoa_play);
        cb_obj._pyvoa_play = null;
    }
    cb_obj.label = '► Play';
}
histo_value_callback(cb_obj, frames, source, dates, div, figlinear, figlog, titleprefix)

The date slider of the bokeh histogram by value.

Each frame holds the bins of one date, computed in Python as the histogram without the slider computes them. The callback swaps them in, and fits the axes to them.

Arguments:
  • cb_obj (Slider) – the date slider, whose value is the index of the date drawn.

  • frames (list of dict) – one frame per date, a column name to its list of values: the bins left and right, their height top, and the other columns the quads read.

  • source (ColumnDataSource) – the source the quads of both figures are drawn from.

  • dates (list of str) – the dates, in the order of frames.

  • div (Div) – the label showing the date drawn, under the slider.

  • figlinear (figure) – the linear and the log-log figures, one per tab.

  • figlog (figure) – the linear and the log-log figures, one per tab.

  • titleprefix (str) – the default title less its trailing (date), or empty when the caller gave a title of their own, which is then left as it is.

Source: pyvoa/js/histo_value_callback.js, the comment left out.

// a shallow copy: assigning a new object is what makes bokeh redraw
const i = cb_obj.value;
const frame = frames[i];
source.data = {...frame};

// the bins differ from one date to the next: the x axis spans the bins of the
// date drawn, the y axis its tallest bin, with the same margins as in Python
// (5% above in linear, a decade above in log)
const tops = frame["top"].map(Number);
const highest = tops.length > 0 ? Math.max(...tops) : 1;
for (const fig of [figlinear, figlog]) {
    fig.x_range.start = frame["left"][0];
    fig.x_range.end   = frame["right"][frame["right"].length - 1];
}
figlinear.y_range.end = 1.05 * highest;
figlog.y_range.end    = 10 * highest;

// the default title, 'EBOLARDC database (09/10/2026)', follows the date drawn
if (titleprefix) {
    for (const fig of [figlinear, figlog]) {
        fig.title.text = titleprefix + "(" + dates[i] + ")";
    }
}
div.text = "<b>" + dates[i] + "</b>";
rollover_callback(value)

Format a value shown by a bokeh hover tooltip.

A value above 10000 or below 0.01 in absolute terms, zero aside, is written in scientific notation with two decimals; any other in the French locale, with two decimals.

Arguments:
  • value (number or str) – the value of the hovered field, handed by bokeh to a CustomJSHover.

Returns:

the text the tooltip shows, e.g. 5.49e+7 or 1 234,57.

Return type:

str

Source: pyvoa/js/rollover_callback.js, the comment left out.

let v = Number(value);

// very large and very small values go to scientific notation: with two
// decimals, the first would run long and the second would read '0,00'.
// Zero itself stays '0,00'
if (Math.abs(v) > 10000 || (Math.abs(v) < 0.01 && v !== 0)) {
    return v.toExponential(2);
} else {
    // the others in the French locale: a comma for the decimal point, a
    // space between the thousands
    return v.toLocaleString('fr-FR', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    });
}
slider_callback(cb_obj, frames, sourcemap, sourcehisto, which, dates, div, maxcountrydisplayed, ylabellinear, ylabellog, ymax, color_mapperjs, titlefigs, ispie, minshare, titleprefix)

The date slider of the bokeh maps, histograms and pie charts.

On a map, each value of the selected frame goes to the shape of its location and the colour scale is fitted to them. On a histogram or a pie chart, the locations are ranked as tools.rank_locations ranks them, the first maxcountrydisplayed drawn, the others gathered into one SumOthers slice of a pie, with those under minshare of the total, as tools.gather_others gathers them. The title and the date label follow the date drawn.

Arguments:
  • cb_obj (Slider) – the date slider, whose value is the index of the date drawn.

  • frames (list of dict) – one frame per date, a column name to its list of values, holding every location of that date.

  • sourcemap (ColumnDataSource) – on a map, the source of the shapes, one per location; on a histogram or a pie chart, the same object as sourcehisto, which is how the callback tells the two apart.

  • sourcehisto (ColumnDataSource) – the source of the bars or of the slices.

  • which (str) – the column drawn.

  • dates (list of str) – the dates, in the order of frames.

  • div (Div) – the label showing the date drawn, under the slider.

  • maxcountrydisplayed (int) – the number of locations drawn on a histogram or a pie chart.

  • ylabellinear (Axis) – the y axes of the linear and the log figures, whose ticks carry the names of the locations of a horizontal histogram.

  • ylabellog (Axis) – the y axes of the linear and the log figures, whose ticks carry the names of the locations of a horizontal histogram.

  • ymax (float) – the height the bars of a horizontal histogram are spread over.

  • color_mapperjs (ColorMapper) – the colour scale of a map.

  • titlefigs (list of figure) – the figures whose title follows the date.

  • ispie (bool) – whether the chart is a pie chart, the only one to gather the others.

  • minshare (float) – the share of the total under which a location of a pie chart is gathered into SumOthers whatever its rank, tools.PIE_MINSHARE.

  • titleprefix (str) – the default title less its trailing (date), or empty when the caller gave a title of their own, which is then left as it is.

Source: pyvoa/js/slider_callback.js, the comment left out.

const i = cb_obj.value;
const frame = frames[i];
const keys = Object.keys(frame);

// the default title, 'SPF database (31/12/2020)', follows the date drawn
if (titleprefix) {
    for (const f of titlefigs) {
        f.title.text = titleprefix + "(" + dates[i] + ")";
    }
}

// ====================================================
// Build rows
// ====================================================
// the frame is column-wise, as a ColumnDataSource is: turned into one object
// per location, it can be matched to the shapes and ranked
const rows = [];
for (let j = 0; j < frame[which].length; j++) {
    let r = {};
    for (const k of keys) r[k] = frame[k][j];
    rows.push(r);
}

// ====================================================
// MAP
// ====================================================
// a histogram or a pie chart is handed its own source as 'sourcemap' too:
// the map part only applies to a map
if (sourcemap !== sourcehisto) {
    // the source holds one shape per location, in its own order: each value
    // of the frame goes to the shape of its location, not to the row of the
    // same rank, and a location the frame lacks is left without a value
    const where = sourcemap.data["where"];
    const pos = new Map();
    for (let j = 0; j < where.length; j++) pos.set(where[j], j);
    const data = {...sourcemap.data};
    for (const k of keys) {
        if (k === "where") continue;
        const col = new Array(where.length).fill(NaN);
        for (const r of rows) {
            const j = pos.get(r["where"]);
            if (j !== undefined) col[j] = r[k];
        }
        data[k] = col;
    }
    // the colour of the shapes is read from 'cases', not from 'which'
    data["cases"] = data[which];
    sourcemap.data = data;
    // the colour scale spans the values of the date drawn, the missing ones
    // left out
    const values = data["cases"].filter(v => Number.isFinite(Number(v)));
    if (values.length > 0) {
        color_mapperjs.low  = Math.min(...values);
        color_mapperjs.high = Math.max(...values);
    }
    sourcemap.change.emit();
    // the title is set already: the histogram part below is skipped
    div.text = "<b>" + dates[i] + "</b>";
    return;
}

// ====================================================
// HISTOGRAM AND PIE — ranked, capped at maxcountrydisplayed
// ====================================================
// the frame holds every location of the date: only the first
// maxcountrydisplayed of them by value are drawn, so that none is left
// stacked on the slots of the others
// ranked as tools.rank_locations ranks them without the slider: decreasing
// value, a missing one last, ties by location name compared as strings, so
// that the locations kept at equal values are the same ones
const val = r => { const n = Number(r[which]); return Number.isFinite(n) ? n : -Infinity; };
const byname = (a, b) => {
    const x = String(a["where"]), y = String(b["where"]);
    return x < y ? -1 : (x > y ? 1 : 0);
};
const rank = (a, b) => (val(b) - val(a)) || byname(a, b);
const sorted_rows = [...rows].sort(rank);
// a pie chart also gathers a slice too thin for its label, as gather_others
// does: under minshare of the total of the date, a missing value with them
const num = r => Number(r[which]);
const all = sorted_rows.reduce((a, r) => a + (Number.isFinite(num(r)) ? num(r) : 0), 0);
const keep = (r, j) => j < maxcountrydisplayed
    && (!ispie || !(minshare > 0 && all > 0) || num(r) / all >= minshare);
let limited = sorted_rows.filter(keep);
const others = sorted_rows.filter((r, j) => !keep(r, j));
const othersum = others.reduce((a, r) => a + (Number.isFinite(num(r)) ? num(r) : 0), 0);
// with a minshare, the locations at zero leave no empty slice behind them
if (ispie && others.length > 0 && !(minshare > 0 && othersum === 0)) {
    // the other locations make one slice, as without the slider, so that the
    // shares are those of the whole
    const rest = {...(limited.length > 0 ? limited[0] : others[0])};
    rest["where"] = "SumOthers";
    rest["code"] = "SumOthers";
    rest[which] = othersum;
    rest["colors"] = "#FFFFFF";
    // ranked with the others, as gather_others ranks it in Python
    limited = [...limited, rest].sort(rank);
}
const len = limited.length;
// no location has a value at this date: the chart is left as it is
if (len === 0) return;

// every column gets a new array of the same length: the frame's own columns,
// then the drawing ones computed below, then whatever else the source holds
const data = {};
for (const col of keys) {
    data[col] = limited.map(r => r[col]);
}

// the columns the drawing methods read, computed as addcolumnshisto and
// addcolumnspie compute them in Python: the slot of each bar, from the top
// down, its extent, and the share and angle of each slice
const total = limited
    .map(r => Number(r[which]) || 0)
    .reduce((a, b) => a + b, 0);
const step = ymax / maxcountrydisplayed;
// the text of a bar's value label, as _fmt in addcolumnshisto writes it:
// '0' for zero, three significant figures in Python's '.3g' for a value of
// 10000 or more or below 0.01, str(round(v, 2)) otherwise
const fmt = v => {
    if (v === 0) return "0";
    if (Math.abs(v) >= 1e4 || Math.abs(v) < 0.01) {
        // '.3g' writes 5.49e+07 for a large value but 0.005 for a small one:
        // the exponent form only outside [1e-4, 1e3), trailing zeros dropped
        const [m, e] = v.toExponential(2).split("e");
        const exp = Number(e);
        if (exp < -4 || exp >= 3) {
            const mant = m.replace(/\.?0+$/, "");
            return mant + "e" + (exp < 0 ? "-" : "+") + String(Math.abs(exp)).padStart(2, "0");
        }
        return String(Number(v.toPrecision(3)));
    }
    // str() of a float keeps one decimal at least: 5.0, not 5
    const r = Math.round(v * 100) / 100;
    return Number.isInteger(r) ? r.toFixed(1) : String(r);
};
data["top"] = [];
data["bottom"] = [];
data["horihistotexty"] = [];
data["left"] = [];
data["right"] = [];
data["horihistotextx"] = [];
data["horihistotext"] = [];
data["percentage"] = [];
data["angle"] = [];
data["textdisplayed"] = [];
data["textdisplayed2"] = [];

const labelMap = new Map();
const ytick_loc = [];
for (let j = 0; j < len; j++) {
    const value = Number(limited[j][which]) || 0;
    // 'where' is cut to maxlettersdisplayed already, by decohistomap
    const w = String(limited[j]["where"] || "");

    // the j-th location takes the j-th slot from the top, one step high; the
    // SumOthers slice of a pie, one past the last slot, wraps round to the
    // first, as in addcolumnshisto (a pie draws no bar: only the columns agree)
    const slot       = j % maxcountrydisplayed;
    const top_pos    = ymax * (maxcountrydisplayed - slot) / maxcountrydisplayed + 0.5 * step;
    const bottom_pos = ymax * (maxcountrydisplayed - slot) / maxcountrydisplayed - 0.5 * step;
    const tick_pos   = parseInt(bottom_pos + 0.5 * step);

    data["top"].push(top_pos);
    data["bottom"].push(bottom_pos);
    data["horihistotexty"].push(tick_pos);
    data["left"].push(Math.min(value, 0));
    data["right"].push(Math.max(value, 0));
    data["horihistotextx"].push(Math.max(value, 0));
    data["horihistotext"].push(fmt(value));
    data["percentage"].push(total > 0 ? value / total : 0);
    data["angle"].push(total > 0 ? (value / total) * 2 * Math.PI : 0);
    // the labels of a pie slice: its name, padded on the left as
    // addcolumnspie pads it, and its share
    data["textdisplayed"].push(w.padStart(36, " "));
    data["textdisplayed2"].push("      " + (total > 0 ? (100 * value / total).toFixed(1) : "0.0") + "%");

    // a tick at the middle of each bar, labelled with its location
    ytick_loc.push(tick_pos);
    labelMap.set(tick_pos, w);
}
// a column the source holds and nothing above recomputes keeps its values,
// the last one repeated when there are more rows than before: every column of
// a ColumnDataSource must have the same length
for (const col of Object.keys(sourcehisto.data)) {
    if (!(col in data)) {
        const old = sourcehisto.data[col];
        data[col] = Array.from({length: len}, (_, j) => old[Math.min(j, old.length - 1)]);
    }
}
sourcehisto.data = data;

// ====================================================
// Axis update
// ====================================================
// the y ticks of a horizontal histogram carry the names of the locations: they
// follow the new ranking, on both figures
ylabellinear.ticker.ticks          = ytick_loc;
ylabellog.ticker.ticks             = ytick_loc;
ylabellinear.major_label_overrides = labelMap;
ylabellog.major_label_overrides    = labelMap;

div.text = "<b>" + dates[i] + "</b>";