Documents
fyron.documents downloads clinical documents, PDFs, reports, text files, or other URL-addressable artifacts. It supports individual URLs, URL lists, and DataFrame-driven batch downloads.
Document downloads are often the bridge between structured cohorts and narrative evidence. Store the returned metadata with your cohort so each local file can be traced back to the source URL, row identifier, and download status. Avoid mixing document retrieval with LLM extraction in the same step; first collect the files, then analyze them.
Document Workflow
- Resolve or construct document URLs from your source system.
- Download documents with deterministic output paths.
- Store hashes, status, and local paths.
- Review failed downloads separately from content extraction.
- Pass local paths to downstream parsing or LLM workflows only after retrieval is complete.
Import
from fyron import DocumentDownloaderDocumentDownloader
loader = DocumentDownloader(
base_url="https://example.org",
output_dir="docs_out",
skip_existing=True,
)Constructor Parameters
| Parameter | Required | Type | Default | Description | ||
|---|---|---|---|---|---|---|
auth | no | `Auth | requests.Session | None` | None | Reuse Fyron auth/session. |
base_url | no | `str | None` | env FHIR_BASE_URL | Prefix for relative URLs. | |
output_dir | no | `str | Path` | "documents_out" | Parent folder for downloads. | |
timeout | no | int | 30 | Request timeout in seconds. | ||
skip_existing | no | bool | True | Skip files already downloaded. | ||
log_downloads | no | bool | False | Log each download. | ||
max_workers | no | int | 4 | Thread workers for URL lists/DataFrames. | ||
retries | no | `urllib3 Retry | None` | default retry policy | Custom retry behavior. | |
save_mode | no | str | "auto" | Save behavior: auto/text/PDF style handling. | ||
force_extension | no | `str | None` | None | Force a file extension. | |
basic_auth | no | `(str, str) | None` | None | Standalone basic auth tuple. |
download_url
Downloads one URL into a deterministic folder based on URL hash.
result = loader.download_url("https://example.org/report_001.pdf")| Parameter | Required | Description |
|---|---|---|
url | yes | Absolute URL or relative URL when base_url is set. |
Returns a DocumentResult with URL, local path, status, content type, hash, and error metadata.
download_urls
Downloads many URLs, optionally writing a results CSV.
results = loader.download_urls(
[
"https://example.org/report_001.pdf",
"https://example.org/report_002.pdf",
],
results_csv="document_downloads.csv",
)| Parameter | Required | Default | Description |
|---|---|---|---|
urls | yes | none | Iterable of URLs. |
results_csv | no | None | Optional CSV path for results. |
Returns a DataFrame of download results.
download_from_df
Downloads URLs from a DataFrame column.
results = loader.download_from_df(
documents_df,
url_col="document_url",
results_csv="document_downloads.csv",
)| Parameter | Required | Default | Description |
|---|---|---|---|
df | yes | none | Input DataFrame. |
url_col | no | "document_url" | Column containing URLs. |
results_csv | no | None | Optional output CSV. |
download_from_dataframe is the same method under the longer name.
Static Helper: download_id
download_id = DocumentDownloader.download_id("https://example.org/report.pdf")Returns a deterministic SHA-256 ID for a URL. This ID is used for stable folder naming.
Context Manager
with DocumentDownloader(output_dir="docs") as loader:
results = loader.download_urls(urls)The context manager closes owned HTTP sessions.
Practical Tips
- Use
results_csvfor long-running batches so failures can be inspected. - Use
skip_existing=Trueto make download jobs restartable. - Use
basic_authorauth, not both. - Use
force_extension=".pdf"only when the server does not provide useful filenames/content types.