-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
update_ref_search.py
96 lines (79 loc) · 3.37 KB
/
update_ref_search.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import json
import os
from algoliasearch import algoliasearch
client = algoliasearch.Client("7EK9KHJW8M", os.environ["ALGOLIA_API_KEY"])
index = client.init_index("schema")
p = json.load(open("_data/plotschema.json"))
schema = []
skippable_keys = [
"src",
"_deprecated",
"impliedEdits",
"uid",
"editType",
]
def insert_whitespace(x):
for word in ["axis", "scatter", "bar", "group", "show", "text",
"hover", "auto", "reverse", "max", "min", "mode", "anchor", "pad",
"prefix", "suffix", "format", "color", "item", "name", "direction", "revision",
"mapbox", "polar", "smith"
]:
x = x.replace(word, " " + word + " ").replace(" ", " ")
return x.strip(" ")
split_layout_attrs = [
"xaxis", "yaxis", "coloraxis", "scene", "polar", "smith", "ternary", "geo", "mapbox",
"sliders", "updatemenus", "annotations", "shapes", "images", "selections"
]
_epsilon = 0.0
def epsilon():
global _epsilon
_epsilon += 0.00001
return _epsilon
def next_level(previous_level, chain_dict):
for sub_attr in previous_level:
if isinstance(previous_level[sub_attr], dict) and not any(
v in sub_attr for v in skippable_keys
):
attribute = dict(
name=chain_dict["name"] + " > " + sub_attr,
split_name=insert_whitespace(chain_dict["name"] + " > " + sub_attr),
permalink=chain_dict["permalink"] + "-" + sub_attr,
rank=chain_dict["rank"] + 1 + epsilon(),
)
for attr in split_layout_attrs:
if attribute["permalink"].endswith("layout-"+attr):
attribute["permalink"] = attribute["permalink"].replace("/layout/", "/layout/"+attr+"/")
if "description" in previous_level[sub_attr]:
attribute["description"] = previous_level[sub_attr][
"description"
].replace("*", '"')
else:
attribute["description"] = "Properties for " + sub_attr
if "values" in previous_level[sub_attr]:
attribute["values"] = ", ".join(str(x) for x in previous_level[sub_attr][
"values"
]).replace("*", '"')
schema.append(attribute)
next_level(previous_level[sub_attr], attribute.copy())
layout_chain_dict = dict(name="layout", split_name="layout", permalink="reference/layout/#layout", rank=epsilon())
# recursively add layout attributes to schema
next_level(p["layout"]["layoutAttributes"], layout_chain_dict.copy())
for i, trace_type in enumerate(p["traces"]):
trace_chain_dict = dict(
name=trace_type + " traces",
split_name=insert_whitespace(trace_type),
permalink="reference/"+ trace_type + "/#" + trace_type, rank=epsilon()
)
if p["traces"][trace_type]["meta"]:
trace_chain_dict["description"] = (
p["traces"][trace_type]["meta"]["description"]
).replace("*", '"')
schema.append(trace_chain_dict)
next_level(p["traces"][trace_type]["attributes"], trace_chain_dict.copy())
# if there are layoutAttributes in the trace add them too.
if p["traces"][trace_type].get("layoutAttributes"):
next_level(
p["traces"][trace_type]["layoutAttributes"], layout_chain_dict.copy()
)
index.clear_index()
index.add_objects(schema)