diff --git a/python/create_dataset/README.md b/python/create_dataset/README.md new file mode 100644 index 0000000..1c3b3a2 --- /dev/null +++ b/python/create_dataset/README.md @@ -0,0 +1,46 @@ +# Create a dataset with pyDataverse + +This recipe creates a dataset in a Dataverse collection using [`pyDataverse`](https://github.com/gdcc/pyDataverse). + +## Switch to Python 3.13 + +Python 3.14 is known not to work. See https://github.com/gdcc/pyDataverse/issues/240 + +## Set up the virtual environment + +From the repository root: + +```bash +cd python/create_dataset +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +``` + +## Configure authentication + +Because we are creating a dataset, an API token is required. + +Get an API token from your Dataverse account and export it as an environment variable. This is more secure than adding the API token to the script itself. + +```bash +export API_TOKEN="your-dataverse-api-token" +``` + +The account associated with the token must have permission to create datasets in the target collection. + +By default, the script connects to `https://demo.dataverse.org` and creates the +dataset in the root collection. You can override either setting: + +```bash +export DATAVERSE_URL="https://beta.dataverse.org" +export COLLECTION="my-collection" +``` + +`COLLECTION` must be a collection alias, not its display name. + +## Run the script + +```bash +python create_dataset.py +``` \ No newline at end of file diff --git a/python/create_dataset/create_dataset.py b/python/create_dataset/create_dataset.py new file mode 100644 index 0000000..87a918a --- /dev/null +++ b/python/create_dataset/create_dataset.py @@ -0,0 +1,48 @@ +import os + +from pyDataverse import Dataverse + +api_token = os.getenv("API_TOKEN") +if not api_token: + raise SystemExit( + "Error: API_TOKEN is required.\n" + "Set it before running the script:\n" + ' export API_TOKEN="your-dataverse-api-token"' + ) + +# First, connect to your Dataverse installation +dv = Dataverse( + base_url=os.getenv("DATAVERSE_URL", "https://demo.dataverse.org"), + api_token=api_token, +) + +# Create a new dataset with basic information +dataset = dv.create_dataset( + title="My Research Dataset", + description="A comprehensive dataset containing experimental results from our study on machine learning algorithms", + authors=[ + { + "name": "Jane Smith", + "affiliation": "University of Science", + # TODO uncomment when this issue is fixed: + # https://github.com/gdcc/pyDataverse/issues/241 + # The expected error is this: TypeError: issubclass() arg 1 must be a class + # "identifier_scheme": "ORCID", # Optional: identifies the author using ORCID + "identifier": "0000-0000-0000-0000" # Optional: the actual ORCID number + } + ], + contacts=[ + { + "name": "Jane Smith", + "email": "jane.smith@university.edu", + "affiliation": "University of Science" # Optional: where they work + } + ], + subjects=["Computer and Information Science", "Engineering"], # Categories for the dataset + collection=os.getenv("COLLECTION", ":root") +) + +# The dataset is now ready to use with all metadata blocks configured +# You can access the citation metadata block like this: +print(dataset.metadata_blocks["citation"].title) +# Output: 'My Research Dataset' diff --git a/python/create_dataset/requirements.txt b/python/create_dataset/requirements.txt new file mode 100644 index 0000000..f39f368 --- /dev/null +++ b/python/create_dataset/requirements.txt @@ -0,0 +1 @@ +pyDataverse @ git+https://github.com/gdcc/pyDataverse.git@7f56e219f6158eafb61711c7419b1ee12d068993