-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Link to how-to doc from index page (#1032)
* docs: Link to how-to doc from index page Closes #1030: * Fix samples * Fix header * Update ruff.toml
- Loading branch information
1 parent
bad3278
commit 8bbca64
Showing
14 changed files
with
252 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Example of using an auth plugin.""" | ||
|
||
from __future__ import annotations | ||
|
||
# start example | ||
from citric import Client | ||
|
||
client = Client( | ||
"https://example.com/index.php/admin/remotecontrol", | ||
"iamadmin", | ||
"secret", | ||
auth_plugin="AuthLDAP", | ||
) | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Example of using the context manager to automatically clean up the session key.""" | ||
|
||
from __future__ import annotations | ||
|
||
# start example | ||
from citric import Client | ||
|
||
LS_URL = "http://localhost:8001/index.php/admin/remotecontrol" | ||
|
||
with Client(LS_URL, "iamadmin", "secret") as client: | ||
# Do stuff with the client | ||
... | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Example of using a custom requests.Session object with the client.""" | ||
|
||
from __future__ import annotations | ||
|
||
# start example | ||
import requests_cache | ||
from citric import Client | ||
|
||
cached_session = requests_cache.CachedSession( | ||
expire_after=60, | ||
allowable_methods=["POST"], | ||
) | ||
|
||
client = Client( | ||
"https://example.com/index.php/admin/remotecontrol", | ||
"iamadmin", | ||
"secret", | ||
requests_session=cached_session, | ||
) | ||
|
||
# Get all surveys from user "iamadmin". | ||
# All responses will be cached for 1 minute. | ||
surveys = client.list_surveys("iamadmin") | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Export survey responses to a DuckDB database.""" | ||
|
||
from __future__ import annotations | ||
|
||
# ruff: noqa: I001, PTH123 | ||
|
||
# start example | ||
import citric | ||
import duckdb | ||
|
||
client = citric.Client( | ||
"https://mylimeserver.com/index.php/admin/remotecontrol", | ||
"iamadmin", | ||
"secret", | ||
) | ||
|
||
with open("responses.csv", "wb") as file: | ||
file.write(client.export_responses(12345, file_format="csv")) | ||
|
||
duckdb.execute("CREATE TABLE responses AS SELECT * FROM 'responses.csv'") | ||
duckdb.sql( | ||
""" | ||
SELECT | ||
token, | ||
submitdate - startdate AS duration | ||
FROM responses | ||
ORDER BY 2 DESC | ||
LIMIT 10 | ||
""" | ||
).show() | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Get all surveys and questions from user "iamadmin".""" | ||
|
||
# ruff: noqa: T201 | ||
|
||
from __future__ import annotations | ||
|
||
# start example | ||
from citric import Client | ||
|
||
LS_URL = "http://localhost:8001/index.php/admin/remotecontrol" | ||
|
||
client = Client(LS_URL, "iamadmin", "secret") | ||
|
||
# Get all surveys from user "iamadmin" | ||
surveys = client.list_surveys("iamadmin") | ||
|
||
for s in surveys: | ||
print(s["surveyls_title"]) | ||
|
||
# Get all questions, regardless of group | ||
questions = client.list_questions(s["sid"]) | ||
for q in questions: | ||
print(q["title"], q["question"]) | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Export survey responses to a Pandas DataFrame.""" | ||
|
||
from __future__ import annotations | ||
|
||
# ruff: noqa: PD901, S106 | ||
# start example | ||
import io | ||
|
||
import pandas as pd | ||
from citric import Client | ||
|
||
survey_id = 123456 | ||
|
||
client = Client( | ||
"https://mylimeserver.com/index.php/admin/remotecontrol", | ||
"iamadmin", | ||
"secret", | ||
) | ||
|
||
# Export responses to CSV and read into a Pandas DataFrame | ||
df = pd.read_csv( | ||
io.BytesIO(client.export_responses(survey_id, file_format="csv")), | ||
delimiter=";", | ||
parse_dates=["datestamp", "startdate", "submitdate"], | ||
index_col="id", | ||
) | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
extend = "../pyproject.toml" | ||
ignore = [ | ||
"INP001", # implicit-namespace-package | ||
] | ||
|
||
[lint.isort] | ||
known-first-party = [] | ||
known-third-party = ["citric"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Example of using the Client.session attribute to call RPC methods directly.""" | ||
|
||
from __future__ import annotations | ||
|
||
from citric import Client | ||
|
||
# start example | ||
client = Client( | ||
"https://mylimeserver.com/index.php/admin/remotecontrol", | ||
"iamadmin", | ||
"secret", | ||
) | ||
|
||
# Call the copy_survey method, not available in Client | ||
new_survey_id = client.session.copy_survey(35239, "copied_survey") | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Upload survey files to S3.""" | ||
|
||
from __future__ import annotations | ||
|
||
# start example | ||
import boto3 | ||
from citric import Client | ||
|
||
s3 = boto3.client("s3") | ||
|
||
client = Client( | ||
"https://mylimeserver.com/index.php/admin/remotecontrol", | ||
"iamadmin", | ||
"secret", | ||
) | ||
|
||
survey_id = 12345 | ||
|
||
# Get all uploaded files and upload them to S3 | ||
for file in client.get_uploaded_file_objects(survey_id): | ||
s3.upload_fileobj( | ||
file.content, | ||
"my-s3-bucket", | ||
f"uploads/sid={survey_id}/qid={file.meta.question.qid}/{file.meta.filename}", | ||
) | ||
# end example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.