Skip to content
Snippets Groups Projects
Commit 00e68bb8 authored by Ankit Izardar's avatar Ankit Izardar
Browse files

Added script

parent dcb6d8b7
No related branches found
No related tags found
No related merge requests found
"""
Script to create a project and a Sample Manager type subfolder in LabKey.
This script takes a project name and a subfolder name as command-line arguments.
It first creates the project, then creates a subfolder of type "Sample Manager" named 'Sample Manager' within it.
Usage:
python create_project_with_subfolder.py <project_name> <subfolder_name>
"""
import os
import argparse
from labkey.api_wrapper import APIWrapper
import urllib.parse
import urllib3
from labkey.exceptions import ServerContextError
# Argument parser setup
parser = argparse.ArgumentParser(description='Create a project and a Sample Manager subfolder in LabKey.')
parser.add_argument('project', help='Name of the project to be created')
parser.add_argument('subfolder', help='Name of the Sample Manager subfolder to be created')
args = parser.parse_args()
project = args.project
subfolder = args.subfolder
# Connection parameters
labkey_server = "labkey-pro-dev.scicore.unibas.ch"
context_path = ''
api = APIWrapper(labkey_server, '', context_path, use_ssl=True)
def create_container(parent_path, container_name, folder_type):
"""
Creates a container (project/folder) in LabKey.
Args:
parent_path (str): The path of the parent container (empty for root-level projects).
container_name (str): The name of the container to create.
folder_type (str): The type of container (e.g., 'Collaboration' for projects, 'Sample Manager' for subfolders).
Returns:
dict: Response from the API, or an empty dictionary in case of failure.
"""
create_url = api.server_context.build_url("core", "createContainer.api", container_path=urllib.parse.quote(parent_path))
create_params = {
"name": container_name,
"title": container_name,
"description": '',
"folderType": folder_type,
"isWorkbook": False
}
headers = urllib3.util.make_headers()
try:
response = api.server_context.make_request(create_url, json=create_params, headers=headers)
return response
except ServerContextError as e:
error_message = str(e)
if "already exists" in error_message:
print(f'Container "{container_name}" already exists under "{parent_path}". Continuing...')
return {"id": "existing_container"}
else:
print(f'Error creating container "{container_name}": {error_message}')
return {}
# Create the project
project_response = create_container('', project, 'Collaboration')
if 'id' in project_response:
print(f'Successfully created or found existing project "{project}".')
# Create the Sample Manager subfolder within the project
subfolder_response = create_container(project, subfolder, 'Sample Manager') # To create another type of subfolder, change the 'Sample Manager' argument to the desired type
if 'id' in subfolder_response:
print(f'Successfully created or found existing Sample Manager subfolder "{subfolder}" within project "{project}".')
else:
print(f'Failed to create Sample Manager subfolder. Response: {subfolder_response}')
# Sample Manager path to upload data
sample_manager_path = f'{project}/{subfolder}'
print(f'Sample Manager path: {sample_manager_path}')
else:
print(f'Failed to create project "{project}". Response: {project_response}')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment