diff --git a/community/yoyo/Form_Component/Form_Component.py b/community/yoyo/Form_Component/Form_Component.py new file mode 100644 index 00000000..19bdfc9a --- /dev/null +++ b/community/yoyo/Form_Component/Form_Component.py @@ -0,0 +1,969 @@ +common = fused.load("https://github.com/fusedio/udfs/tree/b7fe87a/public/common/") + +@fused.udf(cache_max_age=0) +def udf( + config: dict | str = { + "parameter_name": "form", + "filter": [ + { + "column": "mission", + "type": "selectbox", + "default": "goes16", + "parameter_name": "mis" + }, + { + "column": "product_name", + "type": "selectbox", + "default": "ABI", + "parameter_name": "prod" + }, + { + "column": ["start_date", "end_date"], + "type": "daterange", + "parameter_name": ["start_meow", "end"] + }, + { + "type": "bounds", + "parameter_name": "bounds", + "default": [-125, 24, -66, 49], + "height": 240 + }, + { + "type": "text_input", + "default": "Sample text", + "parameter_name": "text" + }, + ], + "data_url": "https://unstable.udf.ai/fsh_aotlErnaYWdIlKcGg6huq/run?dtype_out_raster=png&dtype_out_vector=parquet" + }, + mapbox_token: str = "pk.eyJ1IjoiaXNhYWNmdXNlZGxhYnMiLCJhIjoiY2xicGdwdHljMHQ1bzN4cWhtNThvbzdqcSJ9.73fb6zHMeO_c8eAXpZVNrA", +): + import json + import jinja2 + + # normalize config + if isinstance(config, str): + cfg = json.loads(config) + else: + cfg = config + + global_param_name = cfg.get("parameter_name") + data_url = cfg.get("data_url") + + # normalize fields + normalized_fields = [] + for f in cfg.get("filter", []): + f_type = f.get("type") + raw_param = f.get("parameter_name") + + if f_type == "selectbox": + col = f.get("column") + default_val = f.get("default") + + if isinstance(raw_param, str) and raw_param: + channels = [raw_param] + else: + channels = [] + + alias_base = channels[0] if channels else col + + normalized_fields.append({ + "kind": "select", + "col": col, + "defaultValue": default_val, + "channels": channels, + "aliasBase": alias_base, + }) + + elif f_type == "daterange": + cols = f.get("column", []) + if isinstance(cols, list) and len(cols) == 2: + start_col, end_col = cols + else: + start_col, end_col = "start_date", "end_date" + + if isinstance(raw_param, list) and len(raw_param) == 2: + channels = [raw_param[0], raw_param[1]] + alias_base = raw_param[0] or start_col + elif isinstance(raw_param, str) and raw_param: + channels = [raw_param] + alias_base = raw_param + else: + channels = [] + alias_base = start_col + + normalized_fields.append({ + "kind": "date", + "startCol": start_col, + "endCol": end_col, + "defaultValue": f.get("default"), + "channels": channels, + "aliasBase": alias_base, + }) + + elif f_type == "bounds": + if isinstance(raw_param, str) and raw_param: + channels = [raw_param] + alias_base = raw_param + else: + channels = [] + alias_base = "bounds" + + default_bounds = f.get("default", [-180, -90, 180, 90]) + map_height_px = f.get("height", 240) + + normalized_fields.append({ + "kind": "bounds", + "channels": channels, + "aliasBase": alias_base, + "defaultBounds": default_bounds, + "mapHeightPx": map_height_px, + }) + + elif f_type == "text_input": + if isinstance(raw_param, str) and raw_param: + channels = [raw_param] + alias_base = raw_param + else: + channels = [] + alias_base = "text_input" + + default_val = f.get("default", "") + placeholder = f.get("placeholder", "") + + normalized_fields.append({ + "kind": "text_input", + "channels": channels, + "aliasBase": alias_base, + "defaultValue": default_val, + "placeholder": placeholder, + }) + + else: + pass + + GLOBAL_PARAM_NAME_JS = json.dumps(global_param_name) + DATA_URL_JS = json.dumps(data_url) + FIELDS_JS = json.dumps(normalized_fields) + MAPBOX_TOKEN_JS = json.dumps(mapbox_token) + + # template + template_src = r""" + +
+ + + + + + + + +