Quickstart Guideο
This guide will help you get started with dartfx-dataverse in just a few minutes.
Installationο
First, install the package using uv (recommended):
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dartfx-dataverse
uv pip install dartfx-dataverse
Or using pip:
pip install dartfx-dataverse
Basic Workflowο
The typical workflow for using this package involves:
Discovering available Dataverse installations (optional)
Creating a server connection
Performing searches or retrieving information
Processing the results
Step 1: Discover Dataverse Installationsο
Get a list of all known Dataverse installations worldwide. This functionality leverages the community-maintained dataverse-installations project:
from dartfx.dataverse import fetch_dataverse_installations
# Fetch all known installations
installations = fetch_dataverse_installations()
# Display the first 5 installations
for installation in installations[:5]:
print(f"{installation.name}")
print(f" Hostname: {installation.hostname}")
print(f" Country: {installation.country}")
print(f" Launch Year: {installation.launch_year}")
print()
Step 2: Connect to a Serverο
Create a connection to a specific Dataverse server:
from dartfx.dataverse import DataverseServer, ServerInstallation
# Create server installation object
harvard = ServerInstallation(
name="Harvard Dataverse",
hostname="dataverse.harvard.edu"
)
# Create server connection
server = DataverseServer(installation=harvard)
# Get server information
info = server.get_server_info()
print(f"Server version: {info['data']['version']}")
With API Key (Optional)ο
If you have an API key for authenticated requests:
server = DataverseServer(
installation=harvard,
api_key="your-api-key-here"
)
Step 3: Search for Dataο
Perform a simple search:
# Simple text search
results = server.search_simple("climate")
print(f"Found {results['data']['total_count']} results")
# Display first 5 results
for item in results['data']['items'][:5]:
print(f"- {item['name']}")
Advanced Searchο
Use the SearchParameters model for more control:
from dartfx.dataverse import SearchParameters
# Create search parameters
params = SearchParameters(
q="climate change", # Search query
type="dataset", # Only search datasets
per_page=10, # Results per page
sort="date", # Sort by date
order="desc", # Descending order
show_facets=True # Include facets in results
)
# Execute search
results = server.search(params)
# Process results
for item in results['data']['items']:
print(f"Dataset: {item['name']}")
print(f" Published: {item.get('published_at', 'N/A')}")
print(f" URL: {item.get('url', 'N/A')}")
print()
Step 4: Filter and Refineο
Use filters to refine your search:
# Search with filters
params = SearchParameters(
q="*", # Match all
type="dataset",
fq=[ # Filter queries
"publicationDate:[2020 TO *]", # From 2020 onwards
"dvName:climate" # In climate dataverse
],
per_page=20
)
results = server.search(params)
Working with Multiple Serversο
You can work with multiple Dataverse installations simultaneously:
from dartfx.dataverse import DataverseServer, fetch_dataverse_installations
# Get installations
installations = fetch_dataverse_installations()
# Filter for specific installations
harvard = next(i for i in installations if "harvard" in i.name.lower())
demo = next(i for i in installations if "demo" in i.name.lower())
# Create connections
harvard_server = DataverseServer(installation=harvard)
demo_server = DataverseServer(installation=demo)
# Search both servers
harvard_results = harvard_server.search_simple("education")
demo_results = demo_server.search_simple("education")
print(f"Harvard: {harvard_results['data']['total_count']} results")
print(f"Demo: {demo_results['data']['total_count']} results")
Step 5: Retrieve Multi-Standard Dataset Metadataο
Retrieve full native JSON metadata or standard export formats (Croissant, DDI, Schema.org, DataCite) using a datasetβs Persistent Identifier (DOI):
pid = "doi:10.5683/SP3/FNS9EF"
# 1. Native Dataverse JSON
dataset = server.get_dataset(pid)
print(f"Title: {dataset['data']['latestVersion']['metadataBlocks']['citation']['fields'][0]['value']}")
# 2. Croissant ML (JSON-LD) for ML pipelines
croissant_data = server.get_dataset_export(pid, exporter="croissant")
# 3. DDI Codebook 2.5 (XML) for variable documentation
ddi_xml = server.get_dataset_export(pid, exporter="ddi")
# 4. Schema.org (JSON-LD) for search indexing
schema_json = server.get_dataset_export(pid, exporter="schema.org")
# 5. DataCite (XML) for citation indexing
datacite_xml = server.get_dataset_export(pid, exporter="datacite")
Step 6: Incremental Harvesting & Statisticsο
You can use the harvester engine via CLI or Python:
# Query global repository stats and tabular data file counts
dartfx-dataverse stats --country NL
# Incrementally sync multi-standard metadata (Croissant, Native, DDI, Schema.org, DataCite)
dartfx-dataverse harvest ./harvested_records --server dataverse.harvard.edu --format all --limit 10
Or in Python:
from dartfx.dataverse import fetch_server_stats
stats = fetch_server_stats("dataverse.harvard.edu")
print(f"Datasets: {stats['datasets']:,} | Tabular Files: {stats['tabular_files']:,} ({stats['tabular_pct']}%)")
Handling Errorsο
The package provides structured error handling:
from dartfx.dataverse import DataverseServer, DataverseApiError, ServerInstallation
try:
server = DataverseServer(
installation=ServerInstallation(
name="Test Server",
hostname="invalid.example.com"
)
)
results = server.get_server_info()
except DataverseApiError as e:
print(f"API Error: {e.message}")
print(f"Status Code: {e.status_code}")
print(f"URL: {e.url}")
except Exception as e:
print(f"Unexpected error: {e}")
Configuring Cachingο
The package uses request caching by default. You can configure it:
import requests_cache
from datetime import timedelta
# Create a custom cache session
session = requests_cache.CachedSession(
cache_name='my_dataverse_cache',
backend='sqlite',
expire_after=timedelta(hours=1)
)
# Use with server
server = DataverseServer(
installation=harvard,
session=session
)
Disable caching if needed:
import requests
# Use regular requests session (no caching)
server = DataverseServer(
installation=harvard,
session=requests.Session()
)
Next Stepsο
Now that you understand the basics, explore:
Usage Guide - Detailed usage examples and patterns
Examples - Real-world use cases
API Reference - Complete API reference
Common Use Casesο
Finding Datasets by Subjectο
params = SearchParameters(
q="subject:medicine",
type="dataset",
per_page=50
)
results = server.search(params)
Searching Within a Collectionο
params = SearchParameters(
q="*",
subtree="myDataverse", # Collection identifier
type="dataset"
)
results = server.search(params)
Geographic Searchο
params = SearchParameters(
q="*",
geo_point="42.3,-71.1", # Latitude, Longitude
geo_radius="10" # Radius in kilometers
)
results = server.search(params)
Getting Helpο
If you encounter issues or have questions:
Check the Usage Guide documentation for detailed examples
Review the API Reference for complete API reference
Report issues on GitHub
Consult the Dataverse API Guide