From 8ba0c78bf91c97e933d82d6f0eb3bfffc91818f6 Mon Sep 17 00:00:00 2001 From: Nicolas CARPi Date: Tue, 16 Jan 2024 16:34:14 +0100 Subject: [PATCH] add example exporting experiments to pdf --- config.json | 2 +- examples/12-experiments-export-pdf.py | 59 +++++++++++++++++++++++++++ examples/README.md | 5 +++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 examples/12-experiments-export-pdf.py diff --git a/config.json b/config.json index f4efc7a..7203250 100644 --- a/config.json +++ b/config.json @@ -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" } diff --git a/examples/12-experiments-export-pdf.py b/examples/12-experiments-export-pdf.py new file mode 100755 index 0000000..e8a9765 --- /dev/null +++ b/examples/12-experiments-export-pdf.py @@ -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) diff --git a/examples/README.md b/examples/README.md index b9ae3bd..f1c8be6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 @@ -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