Skip to content

import_export

cleanup_file(file_path)

Removes temporary file after response finishes.

Source code in mlte/backend/api/endpoints/import_export.py
60
61
62
63
def cleanup_file(file_path: str) -> None:
    """Removes temporary file after response finishes."""
    if os.path.exists(file_path):
        os.remove(file_path)

export(*, current_user, models, custom_lists, users, catalogs, background_tasks)

Export store information as a zip file.

Parameters:

Name Type Description Default
models dict[str, list[str] | ExportWildcard] | ExportWildcard

Dict of models, with each containing list of versions to export

required
custom_lists list[CustomListName] | ExportWildcard

List of custom lists to export

required
users list[str] | ExportWildcard

List of users to export

required
catalogs list[str] | ExportWildcard

List of catalogs to export

required
Source code in mlte/backend/api/endpoints/import_export.py
 66
 67
 68
 69
 70
 71
 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
103
104
@router.post("/export")
def export(
    *,
    current_user: AuthorizedUser,
    models: dict[str, list[str] | ExportWildcard] | ExportWildcard,
    custom_lists: list[CustomListName] | ExportWildcard,
    users: list[str] | ExportWildcard,
    catalogs: list[str] | ExportWildcard,
    background_tasks: BackgroundTasks,
) -> FileResponse:
    """
    Export store information as a zip file.
    :param models: Dict of models, with each containing list of versions to export
    :param custom_lists: List of custom lists to export
    :param users: List of users to export
    :param catalogs: List of catalogs to export
    """
    try:
        file_path = state.stores.export_store(
            ExportSpec(
                state.stores.artifact_store,
                state.stores.user_store,
                state.stores.catalog_stores,
                models,
                custom_lists,
                users,
                catalogs,
            ),
        )
    except Exception as ex:
        raise_http_internal_error(ex)

    background_tasks.add_task(cleanup_file, str(file_path))

    return FileResponse(
        path=file_path,
        filename=file_path.name,
        media_type="application/octet-stream",
    )

import_store(*, current_user, import_data, force=Form(False))

Import store information from a file.

Parameters:

Name Type Description Default
import_data Annotated[UploadFile, File()]

JSON file containing store information.

required
Source code in mlte/backend/api/endpoints/import_export.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@router.post("/import")
def import_store(
    *,
    current_user: AuthorizedUser,
    import_data: Annotated[UploadFile, File()],
    force: bool = Form(False),
) -> None:
    """
    Import store information from a file.
    :param import_data: JSON file containing store information.
    """
    if not import_data.filename or not import_data.filename.endswith(".json"):
        raise HTTPException(
            status_code=codes.UNPROCESSABLE_ENTITY,
            detail="File is not of type JSON.",
        )

    try:
        file_bytes = import_data.file.read()
        parsed_json = json.loads(file_bytes)

        state.stores.import_store(
            parsed_json,
            force=force,
        )
    except errors.ErrorAlreadyExists as ex:
        raise HTTPException(
            status_code=codes.ALREADY_EXISTS, detail=f"{ex} already exists."
        ) from None
    except Exception as ex:
        raise_http_internal_error(ex)