Skip to content

Environments

add_environment_external_library_from_pypi(workspace, environment, libraries)

Add external libraries to an environment from PyPI.

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
environment str

The name or ID of the environment.

required
libraries List[tuple[str, str]]

A list of tuples containing library names and their versions.

required

Returns:

Type Description

None

Examples:

    add_environment_external_library_from_pypi(
        workspace='FabricOpsFlow-DEV',
        environment='Default',
        libraries=[
            ('pyFabricOps', '0.3.3'),
            ('pandas', '1.5.3'),
            ('numpy', '1.24.2')
        ]
    )
Source code in src/pyfabricops/helpers/environments.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
def add_environment_external_library_from_pypi(
    workspace: str,
    environment: str,
    libraries: list[tuple[str, str]],
):
    """
    Add external libraries to an environment from PyPI.

    Args:
        workspace (str): The name or ID of the workspace.
        environment (str): The name or ID of the environment.
        libraries (List[tuple[str, str]]): A list of tuples containing library names and their versions.

    Returns:
        None

    Examples:
    ```python
        add_environment_external_library_from_pypi(
            workspace='FabricOpsFlow-DEV',
            environment='Default',
            libraries=[
                ('pyFabricOps', '0.3.3'),
                ('pandas', '1.5.3'),
                ('numpy', '1.24.2')
            ]
        )
    ```
    """
    target_path = "../tmp/env"

    definition = get_environment_definition(
        workspace,
        environment,
    )

    delete_path("../tmp")

    unpack_item_definition(
        definition,
        path=target_path,
    )

    _create_environment_external_library_yaml(libraries)

    update_environment_definition(
        workspace=workspace,
        environment=environment,
        environment_definition=pack_item_definition(target_path),
    )

    logger.success(
        f"External libraries were added to environment {environment} successfully."
    )
    return None

deploy_all_environments(workspace, path, start_path=None)

Deploy all environments to workspace.

Shortcut for deploy_all_items(..., item_types=["Environment"]).

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
path str

The path to the environments.

required
start_path Optional[str]

The starting path for folder creation.

None

Returns:

Name Type Description
DeploymentReport DeploymentReport

The outcome of each environment.

Source code in src/pyfabricops/helpers/environments.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
def deploy_all_environments(
    workspace: str,
    path: str,
    start_path: str | None = None,
) -> DeploymentReport:
    """
    Deploy all environments to workspace.

    Shortcut for ``deploy_all_items(..., item_types=["Environment"])``.

    Args:
        workspace (str): The name or ID of the workspace.
        path (str): The path to the environments.
        start_path (Optional[str]): The starting path for folder creation.

    Returns:
        DeploymentReport: The outcome of each environment.
    """
    return deploy_all_items(
        workspace, path, start_path, item_types=["Environment"]
    )

deploy_environment(workspace, path, start_path=None, description=None, df=True)

Deploy a environment to workspace.

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
path str

The path to the environment.

required
start_path Optional[str]

The starting path for folder creation.

None
description Optional[str]

Description for the environment.

None
df Optional[bool]

If True, returns a DataFrame, otherwise returns a dictionary.

True

Returns:

Type Description
Union[DataFrame, Dict[str, Any], None]

The deployed environment or None if deployment fails.

Source code in src/pyfabricops/helpers/environments.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
@df
def deploy_environment(
    workspace: str,
    path: str,
    start_path: str | None = None,
    description: str | None = None,
    df: bool | None = True,
) -> DataFrame | dict[str, Any] | None:
    """
    Deploy a environment to workspace.

    Args:
        workspace (str): The name or ID of the workspace.
        path (str): The path to the environment.
        start_path (Optional[str]): The starting path for folder creation.
        description (Optional[str]): Description for the environment.
        df (Optional[bool]): If True, returns a DataFrame, otherwise returns a dictionary.

    Returns:
        (Union[DataFrame, Dict[str, Any], None]): The deployed environment or None if deployment fails.
    """
    workspace_id = resolve_workspace(workspace)
    if workspace_id is None:
        return None

    display_name = extract_display_name_from_platform(path)
    if display_name is None:
        return None

    environment_id = resolve_environment(workspace_id, display_name)

    folder_path_string = extract_middle_path(path, start_path=start_path)
    folder_id = create_folders_from_path_string(
        workspace_id, folder_path_string
    )

    item_definition = pack_item_definition(path)

    if environment_id is None:
        return create_environment(
            workspace_id,
            display_name=display_name,
            environment_definition=item_definition,
            description=description,
            folder=folder_id,
            df=False,
        )

    else:
        if folder_id:
            move_item(workspace_id, environment_id, target_folder=folder_id)
        return update_environment_definition(
            workspace_id,
            environment_id,
            environment_definition=item_definition,
            df=False,
        )

export_all_environments(workspace, path)

Export all environments to path.

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
path Union[str, Path]

The path to export to.

required
Source code in src/pyfabricops/helpers/environments.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def export_all_environments(
    workspace: str,
    path: str | Path,
) -> None:
    """
    Export all environments to path.

    Args:
        workspace (str): The name or ID of the workspace.
        path (Union[str, Path]): The path to export to.
    """
    workspace_id = resolve_workspace(workspace)
    if workspace_id is None:
        return None

    items = list_environments(workspace_id, df=False)
    if items is None:
        return None

    failed = []

    for item in items:
        try:
            folder_path = resolve_folder_from_id_to_path(
                workspace_id, item["folderId"]
            )
        except Exception:
            logger.info(
                f"{item['displayName']}.Environment is not inside a folder."
            )
            folder_path = None

        if folder_path is None:
            item_path = Path(path) / (item["displayName"] + ".Environment")
        else:
            item_path = (
                Path(path)
                / folder_path
                / (item["displayName"] + ".Environment")
            )

        definition = get_environment_definition(workspace_id, item["id"])
        if definition is None:
            logger.error(
                f"Could not get the definition of "
                f"{item['displayName']}.Environment; skipping it."
            )
            failed.append(item["displayName"])
            continue

        os.makedirs(item_path, exist_ok=True)
        unpack_item_definition(definition, item_path)

    if failed:
        logger.warning(
            f"{len(failed)} environment(s) could not be exported: "
            f"{', '.join(failed)}."
        )
    else:
        logger.success(
            f"All environments were exported to {path} successfully."
        )
    return None

export_environment(workspace, environment, path)

Export an environment to path.

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
environment str

The name or ID of the environment.

required
path Union[str, Path]

The path to export to.

required
Source code in src/pyfabricops/helpers/environments.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def export_environment(
    workspace: str,
    environment: str,
    path: str | Path,
) -> None:
    """
    Export an environment to path.

    Args:
        workspace (str): The name or ID of the workspace.
        environment (str): The name or ID of the environment.
        path (Union[str, Path]): The path to export to.
    """
    workspace_id = resolve_workspace(workspace)
    if workspace_id is None:
        return None

    item = get_environment(workspace_id, environment, df=False)
    try:
        folder_path = resolve_folder_from_id_to_path(
            workspace_id, item["folderId"]
        )
    except Exception:
        logger.info(
            f"{item['displayName']}.Environment is not inside a folder."
        )
        folder_path = None

    if folder_path is None:
        item_path = Path(path) / (item["displayName"] + ".Environment")
    else:
        item_path = (
            Path(path) / folder_path / (item["displayName"] + ".Environment")
        )
    os.makedirs(item_path, exist_ok=True)

    definition = get_environment_definition(workspace_id, item["id"])
    if definition is None:
        return None

    unpack_item_definition(definition, item_path)

    logger.success(
        f"`{item['displayName']}.Environment` was exported to {item_path} successfully."
    )
    return None

get_all_environments_config(workspace)

Get environments config from a workspace.

Parameters:

Name Type Description Default
workspace str

The name or ID from the workspace.

required

Returns:

Type Description
Union[Dict[str, Any], None]

The dict config of all environments in the workspace

Source code in src/pyfabricops/helpers/environments.py
 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
 97
 98
 99
100
101
102
def get_all_environments_config(
    workspace: str,
) -> dict[str, Any] | None:
    """
    Get environments config from a workspace.

    Args:
        workspace (str): The name or ID from the workspace.

    Returns:
        (Union[Dict[str, Any], None]): The dict config of all environments in the workspace
    """
    items = list_environments(workspace, df=False)

    if items is None:
        return None

    config = {}

    for item in items:
        get_environment(workspace, item["id"], df=False)

        config[item["displayName"]] = {
            "id": item["id"],
            "description": item.get("description", None),
            "folder_id": ""
            if item.get("folderId") is None or pd.isna(item.get("folderId"))
            else item["folderId"],
        }

    return config

get_environment_config(workspace, environment)

Get a specific environment config from a workspace.

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
environment str

The name or ID of the environment.

required

Returns:

Type Description
Union[Dict[str, Any], None]

The dict config from the environment

Source code in src/pyfabricops/helpers/environments.py
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
def get_environment_config(
    workspace: str, environment: str
) -> dict[str, Any] | None:
    """
    Get a specific environment config from a workspace.

    Args:
        workspace (str): The name or ID of the workspace.
        environment (str): The name or ID of the environment.

    Returns:
        (Union[Dict[str, Any], None]): The dict config from the environment
    """
    item = environment
    item_data = get_environment(workspace, item, df=False)

    if item_data is None:
        return None

    else:
        config = {}
        config = config[item_data.get("displayName")] = {}

        config = {
            "id": item_data["id"],
            "description": item_data.get("description", None),
            "folder_id": ""
            if item_data.get("folderId") is None
            or pd.isna(item_data.get("folderId"))
            else item_data["folderId"],
        }

        return config