PageSourceSearch

https://tangleml-tangle.hf.space/assets/create_component_from_python_function_text-CTH31WE9.js

js tangleml-tangle.hf.space collected 2026-10-03 10:21:41 UTC 2,328 bytes, 64 lines download raw bytes

1const n=`import importlib
2import pathlib
3import json
4from cloud_pipelines import components
5
6
7def create_component_from_python_function_text(
8    python_function_text: str,
9    base_image: str = "python:3.12",
10    packages_to_install: list[str] = None,
11    annotations: dict[str, str] = None,
12    function_name: str = None,
13    add_metadata_annotations: bool = True,
14):
15    pathlib.Path("component_code.py").write_text(python_function_text)
16    # Issue: For some reason, despite importlib.reload, Pyodide retains
17    # the original module attributes (like the old function), so the
18    # new module is concatenated with the old one, causing there to be two functions.
19    # Fortunately, deleting the module from sys.modules fixes this.
20    import sys
21    try:
22        del sys.modules["component_code"]
23    except:
24        pass
25    import component_code
26
27    importlib.reload(component_code)
28
29    if function_name:
30        function = getattr(component_code, function_name)
31    else:
32        functions = []
33        for name in dir(component_code):
34            if not name.startswith("_"):
35                obj = getattr(component_code, name)
36                if callable(obj) and not isinstance(obj, type):
37                    functions.append(obj)
38        if not functions:
39            raise ValueError(f"Could not find any functions in code.")
40
41        if len(functions) > 1:
42            raise ValueError(
43                f"Found multiple functions in code. Please specify function-name=name. Functions: {[func.__name__ for func in functions]}"
44            )
45        function = functions[0]
46    
47    if add_metadata_annotations:
48        annotations = annotations or {}
49        annotations["python_original_code"] = python_function_text
50        if packages_to_install:
51            annotations["python_dependencies"] = json.dumps(packages_to_install)
52        annotations["cloud-pipelines.net"] = "true"
53        annotations["cloud-pipelines.net/pipeline-studio-app"] = "true"
54
55    op = components.create_component_from_func(
56        func=function,
57        base_image=base_image,
58        packages_to_install=packages_to_install,
59        annotations=annotations,
60        output_component_file="component.yaml",
61    )
62    component_text = pathlib.Path("component.yaml").read_text()
63    
64    return component_text`;export{n as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.