Skip to content

Commit e4557ee

Browse files
feat(frontend): add Python UDF UI parameter form support (apache#5043)
### What changes were proposed in this PR? This PR adds frontend building blocks for Python UDF UI parameters. It introduces: | Area | Change | | --- | --- | | Python parameter parser | Adds a parser service for detecting supported `self.UiParameter(...)` declarations. | | Parameter sync utility | Adds a service that can merge inferred parameter structure with existing edited values and report invalid UI parameter declarations. | | Formly field type | Adds and registers a `ui-udf-parameters` Formly component for displaying parameter rows. | | Shared parser/sync type | Defines the frontend `UiUdfParameter` shape in the parser service and imports it from the sync service. | | Dependency | Adds `@lezer/[email protected]` for Python syntax parsing. This dependency is MIT licensed. | This PR does not complete the end-to-end feature by itself. The parser/sync utilities and Formly field type are added here, but full wiring with backend operator descriptors and runtime injection is handled by later PRs in the stack. ### Any related issues, documentation, discussions? Part of the Python UDF UI parameter feature split from `feat/ui-parameter`. apache#5044 Intended stack order: 1. Frontend UI parameter building blocks 2. Scala backend injection model 3. Python runtime support 4. End-to-end integration This PR introduces: ```mermaid sequenceDiagram autonumber participant Code as Python UDF Code participant Parser as UiUdfParametersParserService participant Sync as UiUdfParametersSyncService participant Type as UiUdfParameter type participant Component as UiUdfParametersComponent participant Formly as Formly Config participant Later as Later PRs Code->>Parser: self.UiParameter(...) declarations Parser-->>Type: name/type/value metadata Parser-->>Sync: inferred parameter structure Sync->>Sync: preserve existing edited values Component->>Formly: register ui-udf-parameters field type Type-->>Later: available for descriptor wiring Formly-->>Later: field can be used when schema exposes it Note over Parser,Formly: This PR adds frontend building blocks only. Note over Later: Rendering in the operator panel depends on later backend/schema wiring. ``` ### How was this PR tested? Added frontend unit tests for the parser, sync service, and Formly component. Commands run: ```bash cd frontend node .yarn/releases/yarn-4.14.1.cjs format:ci node .yarn/releases/yarn-4.14.1.cjs build:ci node .yarn/releases/yarn-4.14.1.cjs test --watch=false --include=src/app/workspace/service/code-editor/ui-udf-parameters-parser.service.spec.ts --include=src/app/workspace/service/code-editor/ui-udf-parameters-sync.service.spec.ts --include=src/app/workspace/component/ui-udf-parameters/ui-udf-parameters.component.spec.ts ``` Manual UI testing is not available in this PR alone because the backend/operator schema wiring that makes the field visible in the operator panel is intentionally handled in later PRs. ### Was this PR authored or co-authored using generative AI tooling? No --------- Co-authored-by: Xiaozhen Liu <[email protected]>
1 parent 4c0e91f commit e4557ee

12 files changed

Lines changed: 1193 additions & 0 deletions

‎frontend/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@codingame/monaco-vscode-java-default-extension": "8.0.4",
3939
"@codingame/monaco-vscode-python-default-extension": "8.0.4",
4040
"@codingame/monaco-vscode-r-default-extension": "8.0.4",
41+
"@lezer/python": "1.1.18",
4142
"@ngneat/until-destroy": "8.1.4",
4243
"@ngx-formly/core": "6.3.12",
4344
"@ngx-formly/ng-zorro-antd": "6.3.12",

‎frontend/src/app/app.module.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ import { NzTreeModule } from "ng-zorro-antd/tree";
156156
import { NzTreeViewModule } from "ng-zorro-antd/tree-view";
157157
import { NzNoAnimationModule } from "ng-zorro-antd/core/animation";
158158
import { TreeModule } from "@ali-hm/angular-tree-component";
159+
import { UiUdfParametersComponent } from "./workspace/component/ui-udf-parameters/ui-udf-parameters.component";
159160
import { ResultExportationComponent } from "./workspace/component/result-exportation/result-exportation.component";
160161
import { ReportGenerationService } from "./workspace/service/report-generation/report-generation.service";
161162
import { SearchBarComponent } from "./dashboard/component/user/search-bar/search-bar.component";
@@ -265,6 +266,7 @@ registerLocaleData(en);
265266
NzGridModule,
266267
ScrollingModule,
267268
FormlyRepeatDndComponent,
269+
UiUdfParametersComponent,
268270
AdminGmailComponent,
269271
PublicProjectComponent,
270272
WorkspaceComponent,

‎frontend/src/app/common/formly/formly-config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { PresetWrapperComponent } from "./preset-wrapper/preset-wrapper.componen
2727
import { DatasetFileSelectorComponent } from "../../workspace/component/dataset-file-selector/dataset-file-selector.component";
2828
import { CollabWrapperComponent } from "./collab-wrapper/collab-wrapper/collab-wrapper.component";
2929
import { FormlyRepeatDndComponent } from "./repeat-dnd/repeat-dnd.component";
30+
import { UiUdfParametersComponent } from "../../workspace/component/ui-udf-parameters/ui-udf-parameters.component";
3031
import { DatasetVersionSelectorComponent } from "../../workspace/component/dataset-version-selector/dataset-version-selector.component";
3132

3233
/**
@@ -80,6 +81,7 @@ export const TEXERA_FORMLY_CONFIG = {
8081
{ name: "inputautocomplete", component: DatasetFileSelectorComponent, wrappers: ["form-field"] },
8182
{ name: "datasetversionselector", component: DatasetVersionSelectorComponent, wrappers: ["form-field"] },
8283
{ name: "repeat-section-dnd", component: FormlyRepeatDndComponent },
84+
{ name: "ui-udf-parameters", component: UiUdfParametersComponent, wrappers: ["form-field"] },
8385
],
8486
wrappers: [
8587
{ name: "preset-wrapper", component: PresetWrapperComponent },
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<div
20+
class="ui-udf-parameter-list"
21+
*ngIf="model?.length">
22+
<div class="ui-udf-parameter-row header">
23+
<div
24+
class="field-cell"
25+
*ngFor="let column of fieldColumns">
26+
<span class="col-title">{{ column.label }}</span>
27+
</div>
28+
</div>
29+
30+
<div
31+
class="ui-udf-parameter-row"
32+
*ngFor="let parameter of (model || []); let i = index; trackBy: trackByParameterName">
33+
<ng-container *ngIf="field.fieldGroup?.[i] as rowField">
34+
<div
35+
class="field-cell"
36+
*ngFor="let column of fieldColumns">
37+
<formly-field
38+
*ngIf="getColumnField(rowField, column) as columnField"
39+
[field]="columnField"></formly-field>
40+
</div>
41+
</ng-container>
42+
</div>
43+
</div>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
.ui-udf-parameter-row {
21+
display: grid;
22+
grid-template-columns: 250px 250px 1fr;
23+
gap: 12px;
24+
align-items: start;
25+
}
26+
27+
.field-cell {
28+
min-width: 0;
29+
}
30+
31+
:host ::ng-deep .ant-form-item {
32+
margin-bottom: 0;
33+
}
34+
35+
:host ::ng-deep .ant-form-item-label {
36+
display: none;
37+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { FormControl } from "@angular/forms";
21+
import { FormlyFieldConfig } from "@ngx-formly/core";
22+
import { UiUdfParametersComponent } from "./ui-udf-parameters.component";
23+
24+
describe("UiUdfParametersComponent", () => {
25+
let component: UiUdfParametersComponent;
26+
27+
beforeEach(() => {
28+
component = new UiUdfParametersComponent();
29+
});
30+
31+
it("should disable name and type fields while leaving value editable", () => {
32+
const valueControl = new FormControl({ value: "42", disabled: true });
33+
const nameControl = new FormControl("threshold");
34+
const typeControl = new FormControl("double");
35+
36+
const rowField = rowConfig([
37+
{ key: "value", formControl: valueControl },
38+
{ key: "attributeName", formControl: nameControl },
39+
{ key: "attributeType", formControl: typeControl },
40+
]);
41+
42+
(component as any).field = { model: [{}], fieldGroup: [rowField] } as FormlyFieldConfig;
43+
44+
component.onPopulate((component as any).field);
45+
46+
// templateOptions is deprecated, but some existing Formly wrappers still read it.
47+
[
48+
{
49+
column: component.fieldColumns[0],
50+
field: component.getColumnField(rowField, component.fieldColumns[0]),
51+
control: valueControl,
52+
},
53+
{
54+
column: component.fieldColumns[1],
55+
field: component.getColumnField(rowField, component.fieldColumns[1]),
56+
control: nameControl,
57+
},
58+
{
59+
column: component.fieldColumns[2],
60+
field: component.getColumnField(rowField, component.fieldColumns[2]),
61+
control: typeControl,
62+
},
63+
].forEach(({ column, field, control }) => {
64+
expect(component.getColumnField(rowField, column)).toBe(field);
65+
const disabled = column.disabled;
66+
expect((field as FormlyFieldConfig).props?.disabled).toBe(disabled);
67+
expect((field as any).templateOptions?.disabled).toBe(disabled);
68+
expect((control as FormControl).disabled).toBe(disabled);
69+
});
70+
});
71+
72+
it("should apply disabled state to rows generated from the field array template", () => {
73+
const field: FormlyFieldConfig = {
74+
model: [{ value: "42", attribute: { attributeName: "threshold", attributeType: "double" } }],
75+
fieldArray: rowConfig([{ key: "value" }, { key: "attributeName" }, { key: "attributeType" }]),
76+
fieldGroup: [],
77+
};
78+
79+
component.onPopulate(field);
80+
81+
const generatedRow = field.fieldGroup?.[0] as FormlyFieldConfig;
82+
const valueControl = new FormControl({ value: "42", disabled: true });
83+
const nameControl = new FormControl("threshold");
84+
const typeControl = new FormControl("double");
85+
86+
[
87+
{ column: component.fieldColumns[0], control: valueControl },
88+
{ column: component.fieldColumns[1], control: nameControl },
89+
{ column: component.fieldColumns[2], control: typeControl },
90+
].forEach(({ column, control }) => {
91+
const columnField = component.getColumnField(generatedRow, column) as FormlyFieldConfig;
92+
Object.assign(columnField, { formControl: control });
93+
columnField.hooks?.onInit?.(columnField);
94+
95+
expect(columnField.props?.disabled).toBe(column.disabled);
96+
expect((columnField as any).templateOptions?.disabled).toBe(column.disabled);
97+
expect(control.disabled).toBe(column.disabled);
98+
});
99+
});
100+
});
101+
102+
function rowConfig(fields: ReadonlyArray<{ key: string; formControl?: FormControl }>): FormlyFieldConfig {
103+
const [valueField, nameField, typeField] = fields.map(field => ({
104+
key: field.key,
105+
formControl: field.formControl,
106+
}));
107+
108+
return {
109+
fieldGroup: [
110+
valueField,
111+
{
112+
key: "attribute",
113+
fieldGroup: [nameField, typeField],
114+
},
115+
],
116+
};
117+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { Component } from "@angular/core";
20+
import { NgFor, NgIf } from "@angular/common";
21+
import { FieldArrayType, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core";
22+
23+
type UiUdfParameterColumn = Readonly<{ label: string; key: string; parentKey?: string; disabled: boolean }>;
24+
25+
/** Renders inferred Python UDF UI parameters with editable values and locked name/type columns. */
26+
@Component({
27+
selector: "texera-ui-udf-parameters",
28+
templateUrl: "./ui-udf-parameters.component.html",
29+
styleUrls: ["./ui-udf-parameters.component.scss"],
30+
imports: [NgIf, NgFor, FormlyModule],
31+
})
32+
export class UiUdfParametersComponent extends FieldArrayType<FormlyFieldConfig> {
33+
private readonly disabledStateConfigured = new WeakMap<FormlyFieldConfig, boolean>();
34+
35+
readonly fieldColumns: UiUdfParameterColumn[] = [
36+
{ label: "Value", key: "value", disabled: false },
37+
{ label: "Name", key: "attributeName", parentKey: "attribute", disabled: true },
38+
{ label: "Type", key: "attributeType", parentKey: "attribute", disabled: true },
39+
];
40+
41+
override onPopulate(field: FormlyFieldConfig): void {
42+
this.configureRowTemplate(this.getFieldArrayTemplate(field));
43+
super.onPopulate(field);
44+
field.fieldGroup?.forEach(rowField => this.configureRowFields(rowField));
45+
}
46+
47+
/** Finds the Formly field config that backs one visible column in a parameter row. */
48+
getColumnField(rowField: FormlyFieldConfig, column: UiUdfParameterColumn): FormlyFieldConfig | undefined {
49+
return this.getChildField(column.parentKey ? this.getChildField(rowField, column.parentKey) : rowField, column.key);
50+
}
51+
52+
private getFieldArrayTemplate(field: FormlyFieldConfig): FormlyFieldConfig | undefined {
53+
return typeof field.fieldArray === "function" ? undefined : field.fieldArray;
54+
}
55+
56+
private configureRowTemplate(rowField: FormlyFieldConfig | undefined): void {
57+
this.configureRowColumns(rowField, this.setDisabledMetadata.bind(this));
58+
}
59+
60+
private configureRowFields(rowField: FormlyFieldConfig | undefined): void {
61+
this.configureRowColumns(rowField, this.configureDisabledState.bind(this));
62+
}
63+
64+
private configureRowColumns(
65+
rowField: FormlyFieldConfig | undefined,
66+
configureColumn: (field: FormlyFieldConfig | undefined, disabled: boolean) => void
67+
): void {
68+
if (!rowField) return;
69+
70+
this.fieldColumns.forEach(column => configureColumn(this.getColumnField(rowField, column), column.disabled));
71+
}
72+
73+
private getChildField(rowField: FormlyFieldConfig | undefined, key: string): FormlyFieldConfig | undefined {
74+
return rowField?.fieldGroup?.find(fieldConfig => fieldConfig.key === key);
75+
}
76+
77+
/** Sets Formly disabled metadata and keeps controls created later in sync through an onInit hook. */
78+
private configureDisabledState(field: FormlyFieldConfig | undefined, disabled: boolean): void {
79+
if (!field) return;
80+
81+
this.setDisabledMetadata(field, disabled);
82+
83+
if (this.disabledStateConfigured.get(field) === disabled) {
84+
this.applyDisabledState(field, disabled);
85+
return;
86+
}
87+
88+
const previousOnInit = field.hooks?.onInit;
89+
field.hooks = {
90+
...(field.hooks ?? {}),
91+
onInit: initializedField => {
92+
previousOnInit?.(initializedField);
93+
this.applyDisabledState(initializedField, disabled);
94+
},
95+
};
96+
97+
this.disabledStateConfigured.set(field, disabled);
98+
this.applyDisabledState(field, disabled);
99+
}
100+
101+
private setDisabledMetadata(field: FormlyFieldConfig | undefined, disabled: boolean): void {
102+
if (!field) return;
103+
104+
field.props = { ...(field.props ?? {}), disabled };
105+
106+
// Keep deprecated templateOptions in sync for existing Formly wrappers that still read it.
107+
(field as any).templateOptions = { ...((field as any).templateOptions ?? {}), disabled };
108+
}
109+
110+
private applyDisabledState(field: FormlyFieldConfig, disabled: boolean): void {
111+
if (disabled) field.formControl?.disable({ emitEvent: false });
112+
else field.formControl?.enable({ emitEvent: false });
113+
}
114+
115+
trackByParameterName = (index: number, parameter: any): string | number => {
116+
return parameter?.attribute?.attributeName ?? index;
117+
};
118+
}

0 commit comments

Comments
 (0)