Python scripts

Here, we provide some python scripts to access files on Girder. To be able to use them, please install the girder client python module (read how here. )

1. With user and password

1.1. What not to do

The following script is only meant as a first approach to understand how the module works. You should not use it because it requires your password to be written in clear in the script. Use the interactive version instead.
#!/usr/bin/env python3


# We need the girder client module.
import girder_client

# First, we initiate the client with the girder server address.
gc = girder_client.GirderClient(apiUrl='https://girder.math.unistra.fr/api/v1')

# We authenticate using the username and the password.
# /!\ This is for learning purpose.
# For security reasons, you should never put your password in the script.
gc.authenticate(username='USER', password='PASSWORD')   (1)

# We download the file using its file ID. The path indicates where the file
# should be written (the file name should be included at the end of the path)
gc.downloadFile(fileId='FILEID', path='PATH')           (2)
1 USER should be replaced by the Girder user name, PASSWORD by the corresponding password
2 FILEID should be replaced by the actual Girder file ID and PATH should be the path where to store the results, including the desired file name and extension.
If you don’t supply the file name, the system will not warn you, it will automatically generate one, which could be confusing !
Remember not to use this script. Try the interactive one instead.

1.2. Interactive download

Here, we use a modified version of the authenticate function to use interactive login. This means the password will be prompted for at run time.

This implies the script can not be used in a fully automated way, because each execution of the script will require the user of the script to be present to type the password. For a safe and automatic access, use the API keys.
#!/usr/bin/env python3


# We need the girder client module.
import girder_client

# First, we initiate the client with the girder server address.
gc = girder_client.GirderClient(apiUrl='https://girder.math.unistra.fr/api/v1')

# We authenticate using the username, the password will be typed at runtime
gc.authenticate(username='USER', interactive=True)   (1)

# We download the file using its file ID. The path indicates where the file
# should be written (the full file name should be included at the end of the path)
gc.downloadFile(fileId='FILEID', path='PATH')        (2)
1 USER should be replaced by the Girder user name, and because of the interactive=True argument, the password will be prompted for at runtime.
We can even prompt the user to type both the user name and the password by providing only the interactive=True argument ! This is a better solution when multiple users are likely to use the script only once or a handful of times each.
1 FILEID should be replaced by the actual Girder file ID and PATH should be the path where to store the results, including the desired file name and extension.
If you don’t supply the file name, the system will not warn you, it will automatically generate one, which could be confusing !

1.3. Interactive upload

To upload a file, only a few changes are required.

#!/usr/bin/env python3


# We need the girder client module.
import girder_client

# First, we initiate the client with the girder server address.
gc = girder_client.GirderClient(apiUrl='https://girder.math.unistra.fr/api/v1')

# We authenticate using the username, the password will be typed at runtime
gc.authenticate(username='USER', interactive=True)    (1)


# /!\ This is mandatory: we have to open the file in read mode before
# uploading it
f = open('PATH', 'r')                                 (2)

# Now we can upload the file                            (3)
gc.uploadFile(parentId='PID', stream=f, name="NAME", size=SIZE, parentType='TYPE')
1 USER should be replaced by the Girder user name, and because of the interactive=True argument, the password will be prompted for at runtime.
2 PATH should be replaced by the full path to the file to read. r stands for "read mode".
3 PID should be replaced by the parent directory ID (on the Girder server). f is the read stream defined previously . NAME should be replaced by the desired file name (on the Girder server). SIZE should be replaced by the file size (in bytes). TYPE is either folder, user, or collection.
We should try and find a way to get the file size automatically.