Project libraries#

You can interact with the Library of each project through the API.

Getting the DSSLibrary object#

You must first retrieve the DSSLibrary through the get_library() method

project = client.get_project("MYPROJECT")
library = project.get_library()

Retrieving the content of a file#

library_file = library.get_file("/file.txt")
print("Content: %s" % library_file.read())

# Alternate ways to retrieve a file handle
library_file = library.get_file("/python/some_code.py")
library_file = library.get_folder("/").get_file("file.txt")

Getting the list of all the library items#

def print_library_items(item):
    print(item.path)
    if "list" in dir(item):
        for child in item.list():
            print_library_items(child)

for item in library.list():
    print_library_items(item)

Add a new folder in the library#

library.add_folder("/new_folder")
library.add_folder("/python/new_sub_folder")
library.get_folder("/python").add_folder("another_sub_folder")

Add a new file in the library#

with open("/path/to/local/file", "rb") as file:
    new_txt_file = library.add_file("/new_folder/new_file.txt")
    new_txt_file.write(file)

with open("/path/to/local/file", "rb") as file:
    new_json_file = library.get_folder("/new_folder").add_file("new_file.json")
    new_json_file.write(file)

Rename a file or a folder in the library#

# rename a file in the library
library.get_file("/folder/file.txt").rename("renamed_file.txt")

# rename a folder in the library
library.get_folder("/folder").rename("renamed_folder")

Move a file or a folder in the library#

# move a file in the library
library.get_file("/folder/file.txt").move_to(library.get_folder("/folder2"))

# move a folder in the library
library.get_folder("/folder").move_to(library.get_folder("/folder2"))

Delete a file or a folder from the library#

library.get_file("/path/to/item").delete()
library.get_folder("/path/to").get_file("/item").delete()
library.get_folder("/path/to").delete()

Reference documentation#

dataikuapi.dss.projectlibrary.DSSLibrary(...)

A handle to manage the library of a project It saves locally a copy of taxonomy to help navigate in the library All modifications done through this object and related library items are done locally and on remote.