-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add YAML Editor and Visualization Panel #35947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5aa3c38
Yaml Panel
Chenzo1001 e0b523e
Update CHANGES.md
Chenzo1001 b274675
Update
Chenzo1001 f0ef65a
Update sdks/python/apache_beam/runners/interactive/extensions/apache-…
Chenzo1001 63901e1
Update CHANGES.md
Chenzo1001 ec69908
Merge branch 'master' into master
Chenzo1001 cf3475c
Fix CI/CD fails
Chenzo1001 c5e7c79
Update CHANGES.md
Chenzo1001 fb80f42
Update yaml_parse_utils.py
Chenzo1001 b37f1c0
Update CHANGES.md
Chenzo1001 b730d26
Update CHANGES.md
Chenzo1001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...ons/apache-beam-jupyterlab-sidepanel/apache_beam_jupyterlab_sidepanel/yaml_parse_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| # Licensed under the Apache License, Version 2.0 (the 'License'); you may not | ||
| # use this file except in compliance with the License. You may obtain a copy of | ||
| # the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations under | ||
| # the License. | ||
|
|
||
| import dataclasses | ||
| import json | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
| from typing import Dict | ||
| from typing import List | ||
| from typing import TypedDict | ||
|
|
||
| import yaml | ||
|
|
||
| import apache_beam as beam | ||
| from apache_beam.yaml.main import build_pipeline_components_from_yaml | ||
|
|
||
| # ======================== Type Definitions ======================== | ||
|
|
||
|
|
||
| @dataclass | ||
| class NodeData: | ||
| id: str | ||
| label: str | ||
| type: str = "" | ||
|
|
||
| def __post_init__(self): | ||
| # Ensure ID is not empty | ||
| if not self.id: | ||
| raise ValueError("Node ID cannot be empty") | ||
|
|
||
|
|
||
| @dataclass | ||
| class EdgeData: | ||
| source: str | ||
| target: str | ||
| label: str = "" | ||
|
|
||
| def __post_init__(self): | ||
| if not self.source or not self.target: | ||
| raise ValueError("Edge source and target cannot be empty") | ||
|
|
||
|
|
||
| class FlowGraph(TypedDict): | ||
| nodes: List[Dict[str, Any]] | ||
| edges: List[Dict[str, Any]] | ||
|
|
||
|
|
||
| # ======================== Main Function ======================== | ||
|
|
||
|
|
||
| def parse_beam_yaml(yaml_str: str, isDryRunMode: bool = False) -> str: | ||
| """ | ||
| Parse Beam YAML and convert to flow graph data structure | ||
|
|
||
| Args: | ||
| yaml_str: Input YAML string | ||
|
|
||
| Returns: | ||
| Standardized response format: | ||
| - Success: {'status': 'success', 'data': {...}, 'error': None} | ||
| - Failure: {'status': 'error', 'data': None, 'error': 'message'} | ||
| """ | ||
| # Phase 1: YAML Parsing | ||
| try: | ||
| parsed_yaml = yaml.safe_load(yaml_str) | ||
| if not parsed_yaml or 'pipeline' not in parsed_yaml: | ||
| return build_error_response( | ||
| "Invalid YAML structure: missing 'pipeline' section") | ||
| except yaml.YAMLError as e: | ||
| return build_error_response(f"YAML parsing error: {str(e)}") | ||
|
|
||
| # Phase 2: Pipeline Validation | ||
| try: | ||
| options, constructor = build_pipeline_components_from_yaml( | ||
| yaml_str, | ||
| [], | ||
| validate_schema='per_transform' | ||
| ) | ||
| if isDryRunMode: | ||
| with beam.Pipeline(options=options) as p: | ||
| constructor(p) | ||
| except Exception as e: | ||
| return build_error_response(f"Pipeline validation failed: {str(e)}") | ||
|
|
||
| # Phase 3: Graph Construction | ||
| try: | ||
| pipeline = parsed_yaml['pipeline'] | ||
| transforms = pipeline.get('transforms', []) | ||
|
|
||
| nodes: List[NodeData] = [] | ||
| edges: List[EdgeData] = [] | ||
|
|
||
| nodes.append(NodeData(id='0', label='Input', type='input')) | ||
| nodes.append(NodeData(id='1', label='Output', type='output')) | ||
|
|
||
| # Process transform nodes | ||
| for idx, transform in enumerate(transforms): | ||
| if not isinstance(transform, dict): | ||
| continue | ||
|
|
||
| payload = {k: v for k, v in transform.items() if k not in {"type"}} | ||
|
|
||
| node_id = f"t{idx}" | ||
| node_data = NodeData( | ||
| id=node_id, | ||
| label=transform.get('type', 'unnamed'), | ||
| type='default', | ||
| **payload) | ||
| nodes.append(node_data) | ||
|
|
||
| # Create connections between nodes | ||
| if idx > 0: | ||
| edges.append( | ||
| EdgeData(source=f"t{idx-1}", target=node_id, label='chain')) | ||
|
|
||
| if transforms: | ||
| edges.append(EdgeData(source='0', target='t0', label='start')) | ||
| edges.append(EdgeData(source=node_id, target='1', label='stop')) | ||
|
|
||
| def to_dict(node): | ||
| if hasattr(node, '__dataclass_fields__'): | ||
| return dataclasses.asdict(node) | ||
| return node | ||
|
|
||
| nodes_serializable = [to_dict(n) for n in nodes] | ||
|
|
||
| return build_success_response( | ||
| nodes=nodes_serializable, edges=[dataclasses.asdict(e) for e in edges]) | ||
|
|
||
| except Exception as e: | ||
| return build_error_response(f"Graph construction failed: {str(e)}") | ||
|
|
||
|
|
||
| # ======================== Utility Functions ======================== | ||
|
|
||
|
|
||
| def build_success_response( | ||
| nodes: List[Dict[str, Any]], edges: List[Dict[str, Any]]) -> str: | ||
| """Build success response""" | ||
| return json.dumps({'data': {'nodes': nodes, 'edges': edges}, 'error': None}) | ||
|
|
||
|
|
||
| def build_error_response(error_msg: str) -> str: | ||
| """Build error response""" | ||
| return json.dumps({'data': None, 'error': error_msg}) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Example usage | ||
| example_yaml = """ | ||
| pipeline: | ||
| transforms: | ||
| - type: ReadFromCsv | ||
| name: A | ||
| config: | ||
| path: /path/to/input*.csv | ||
| - type: WriteToJson | ||
| name: B | ||
| config: | ||
| path: /path/to/output.json | ||
| input: ReadFromCsv | ||
| - type: Join | ||
| input: [A, B] | ||
| """ | ||
|
|
||
| response = parse_beam_yaml(example_yaml, isDryRunMode=False) | ||
| print(response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.