From 4312e6c4749e59682585468a162a3ee2fe523782 Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Tue, 23 Jun 2026 14:38:26 -0400 Subject: [PATCH 1/2] stub out python create dataset script from https://gdcc.github.io/pyDataverse/high-level/dataverse/#creating-datasets --- python/create_dataset/create_dataset.py | 31 +++++++++++++++++++++++++ python/create_dataset/requirements.txt | 1 + 2 files changed, 32 insertions(+) create mode 100644 python/create_dataset/create_dataset.py create mode 100644 python/create_dataset/requirements.txt diff --git a/python/create_dataset/create_dataset.py b/python/create_dataset/create_dataset.py new file mode 100644 index 0000000..2fe6b7c --- /dev/null +++ b/python/create_dataset/create_dataset.py @@ -0,0 +1,31 @@ +from pyDataverse import Dataverse + +# First, connect to your Dataverse installation +dv = Dataverse(base_url="http://localhost:8080") + +# 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", + "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 +) + +# 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 From 240d5a9d21d75a86f765f1173f6240dd669d1569 Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Wed, 15 Jul 2026 16:03:07 -0400 Subject: [PATCH 2/2] get create dataset script working --- python/create_dataset/README.md | 46 +++++++++++++++++++++++++ python/create_dataset/create_dataset.py | 23 +++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 python/create_dataset/README.md 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 index 2fe6b7c..87a918a 100644 --- a/python/create_dataset/create_dataset.py +++ b/python/create_dataset/create_dataset.py @@ -1,7 +1,20 @@ +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="http://localhost:8080") +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( @@ -11,7 +24,10 @@ { "name": "Jane Smith", "affiliation": "University of Science", - "identifier_scheme": "ORCID", # Optional: identifies the author using ORCID + # 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 } ], @@ -22,7 +38,8 @@ "affiliation": "University of Science" # Optional: where they work } ], - subjects=["Computer and Information Science", "Engineering"] # Categories for the dataset + 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