Skip to content

Commit 8ba0c78

Browse files
committed
add example exporting experiments to pdf
1 parent 8c6346e commit 8ba0c78

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

Diff for: config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"packageName": "elabapi_python",
33
"pythonPackageName": "elabapi_python",
44
"projectName": "elabapi-python",
5-
"packageVersion": "0.5.1",
5+
"packageVersion": "0.6.0",
66
"packageUrl": "https://github.com/elabftw/elabapi-python"
77
}

Diff for: examples/12-experiments-export-pdf.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
import datetime
3+
4+
###############
5+
# DESCRIPTION #
6+
##############
7+
# In this script, we list experiments from a user and save the recently modified into a PDF
8+
##############
9+
10+
# the python library for elabftw
11+
import elabapi_python
12+
13+
#########################
14+
# CONFIG #
15+
#########################
16+
# replace with the URL of your instance
17+
API_HOST_URL = 'https://elab.local:3148/api/v2'
18+
# replace with your api key
19+
API_KEY = 'apiKey4Test'
20+
# number of days to look back
21+
PERIOD_IN_DAYS = 7
22+
#########################
23+
# END CONFIG #
24+
#########################
25+
26+
# Configure the api client
27+
configuration = elabapi_python.Configuration()
28+
configuration.api_key['api_key'] = API_KEY
29+
configuration.api_key_prefix['api_key'] = 'Authorization'
30+
configuration.host = API_HOST_URL
31+
configuration.debug = False
32+
configuration.verify_ssl = False
33+
34+
# create an instance of the API class
35+
api_client = elabapi_python.ApiClient(configuration)
36+
# fix issue with Authorization header not being properly set by the generated lib
37+
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
38+
39+
#### SCRIPT START ##################
40+
41+
# Load the experiments api
42+
experimentsApi = elabapi_python.ExperimentsApi(api_client)
43+
44+
# calculate the date
45+
today = datetime.date.today()
46+
date_from = today - datetime.timedelta(days = PERIOD_IN_DAYS)
47+
48+
# get a list of experiments for user with ID:2
49+
# OWNER: the target userid. Note: using owner keyword requires elabapi 0.6.0
50+
# LIMIT: the max number of items to return. Use a high limit so we get all at once
51+
# EXTENDED: this is the content of the advanced search, here we filter on timestamped entries during last week
52+
results = experimentsApi.read_experiments(owner=2, limit=9999, extended=f'timestamped:yes timestamped_at:>{date_from}')
53+
for exp in results:
54+
now = datetime.datetime.now()
55+
filename = f'{exp.id}-{exp.elabid}-{now.strftime("%Y-%m-%d_%H-%M-%S")}-export.pdf'
56+
print(f'Saving file {filename}')
57+
with open(filename, 'wb') as file:
58+
# the _preload_content flag is necessary so the api_client doesn't try and deserialize the response
59+
file.write(experimentsApi.get_experiment(exp.id, format='pdf', _preload_content=False).data)

Diff for: examples/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Work with date-time data formats with a demonstration of doing statistics with e
5050

5151
Create and edit a Resources Category (Items types).
5252

53+
# [12-experiments-export-pdf.py]
54+
55+
Look for all timestamped experiments last week for a particular user and save a pdf locally.
56+
5357
[00-read-items.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/00-read-items.py
5458
[01-download-timestamp-archive.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/01-download-timestamp-archive.py
5559
[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).
6266
[09-import-csv.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/09-import-csv.py
6367
[10-date-time-conversions.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/10-date-time-conversions.py
6468
[11-resources-categories.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/11-resources-categories.py
69+
[12-experiments-export-pdf.py]: https://github.com/elabftw/elabapi-python/blob/master/examples/12-experiments-export-pdf.py
6570

0 commit comments

Comments
 (0)