Skip to content

Commit

Permalink
add example exporting experiments to pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Jan 16, 2024
1 parent 8c6346e commit 8ba0c78
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packageName": "elabapi_python",
"pythonPackageName": "elabapi_python",
"projectName": "elabapi-python",
"packageVersion": "0.5.1",
"packageVersion": "0.6.0",
"packageUrl": "https://github.com/elabftw/elabapi-python"
}
59 changes: 59 additions & 0 deletions examples/12-experiments-export-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
import datetime

###############
# DESCRIPTION #
##############
# In this script, we list experiments from a user and save the recently modified into a PDF
##############

# the python library for elabftw
import elabapi_python

#########################
# CONFIG #
#########################
# replace with the URL of your instance
API_HOST_URL = 'https://elab.local:3148/api/v2'
# replace with your api key
API_KEY = 'apiKey4Test'
# number of days to look back
PERIOD_IN_DAYS = 7
#########################
# END CONFIG #
#########################

# Configure the api client
configuration = elabapi_python.Configuration()
configuration.api_key['api_key'] = API_KEY
configuration.api_key_prefix['api_key'] = 'Authorization'
configuration.host = API_HOST_URL
configuration.debug = False
configuration.verify_ssl = False

# create an instance of the API class
api_client = elabapi_python.ApiClient(configuration)
# fix issue with Authorization header not being properly set by the generated lib
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)

#### SCRIPT START ##################

# Load the experiments api
experimentsApi = elabapi_python.ExperimentsApi(api_client)

# calculate the date
today = datetime.date.today()
date_from = today - datetime.timedelta(days = PERIOD_IN_DAYS)

# get a list of experiments for user with ID:2
# OWNER: the target userid. Note: using owner keyword requires elabapi 0.6.0
# LIMIT: the max number of items to return. Use a high limit so we get all at once
# EXTENDED: this is the content of the advanced search, here we filter on timestamped entries during last week
results = experimentsApi.read_experiments(owner=2, limit=9999, extended=f'timestamped:yes timestamped_at:>{date_from}')
for exp in results:
now = datetime.datetime.now()
filename = f'{exp.id}-{exp.elabid}-{now.strftime("%Y-%m-%d_%H-%M-%S")}-export.pdf'
print(f'Saving file {filename}')
with open(filename, 'wb') as file:
# the _preload_content flag is necessary so the api_client doesn't try and deserialize the response
file.write(experimentsApi.get_experiment(exp.id, format='pdf', _preload_content=False).data)
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Work with date-time data formats with a demonstration of doing statistics with e

Create and edit a Resources Category (Items types).

# [12-experiments-export-pdf.py]

Look for all timestamped experiments last week for a particular user and save a pdf locally.

[00-read-items.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/00-read-items.py
[01-download-timestamp-archive.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/01-download-timestamp-archive.py
[02-patch-metadata-per-category.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/02-patch-metadata-per-category.py
Expand All @@ -62,4 +66,5 @@ Create and edit a Resources Category (Items types).
[09-import-csv.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/09-import-csv.py
[10-date-time-conversions.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/10-date-time-conversions.py
[11-resources-categories.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/11-resources-categories.py
[12-experiments-export-pdf.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/12-experiments-export-pdf.py

0 comments on commit 8ba0c78

Please sign in to comment.