Repository navigation
Expand file tree
/
Copy path_guidata_jsonschema_shim.py
More file actions
52 lines (40 loc) · 2.06 KB
/
Copy path_guidata_jsonschema_shim.py
File metadata and controls
52 lines (40 loc) · 2.06 KB
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
# Copyright (c) DataLab Platform Developers, BSD 3-Clause License
# See LICENSE file for details
# @shim-registry: guidata-jsonschema
"""Add browser-specific schema hints missing from guidata.
Guidata provides the JSON Schema exporter natively from version 3.15.0, and
DataLab-Web requires guidata 3.16 or later (see ``guidata-dependency.json``).
DataLab-Web additionally needs the ``variable_size`` and ``minmax`` item
properties to configure its array editor and the ``even`` constraint to
validate integer controls. This patch augments only guidata's native item
converters and leaves the public exporter untouched.
"""
from __future__ import annotations
from typing import Any
import guidata.dataset.dataitems as gdi
import guidata.dataset.jsonschema as gdjson
def _install_schema_hints() -> None:
"""Augment guidata's native schema converters once."""
if getattr(gdjson, "_dlw_schema_hints_installed", False):
return
native_array_converter = gdjson._float_array_to_property # pylint: disable=protected-access
native_numeric_converter = gdjson._numeric_to_property # pylint: disable=protected-access
def convert_array(item: gdi.FloatArrayItem) -> dict[str, Any]:
prop = native_array_converter(item)
if item.get_prop("edit", "variable_size", False):
prop["x-guidata-variable-size"] = True
minmax = item.get_prop("display", "minmax", None)
if minmax:
prop["x-guidata-minmax"] = minmax
return prop
def convert_numeric(item: gdi.NumericTypeItem, kind: str) -> dict[str, Any]:
prop = native_numeric_converter(item, kind)
if kind == "int":
even = item.get_prop("data", "even", None)
if even is not None:
prop.setdefault("x-guidata-even", bool(even))
return prop
gdjson._float_array_to_property = convert_array # pylint: disable=protected-access
gdjson._numeric_to_property = convert_numeric # pylint: disable=protected-access
gdjson._dlw_schema_hints_installed = True # pylint: disable=protected-access
_install_schema_hints()