
Initialize a report project's quartifyr Python environment
Source:R/initialize.R
initialize_quartifyr_project.RdProvisions a project-local Python environment via pyro (writing a
quartifyr dependency group to the calling project's pyproject.toml)
so render_report()'s styling_*() calls have a quartifyr_styling
venv to run against. Call this once per report project, alongside
reportifyr::initialize_report_project() – not implied by
render_report() itself, since re-provisioning on every render would be
wasteful.
Usage
initialize_quartifyr_project(
directory = ".",
from_dir = NULL,
from_repo = NULL,
ref = NULL,
subdir = NULL,
type = NULL,
force = FALSE,
python_env = getOption("quartifyr.python_env"),
install_extensions = FALSE,
extensions_from = NULL
)Arguments
- directory
Target project directory. Defaults to the current directory.
- from_dir
Use a local directory as the template source instead of leaving
directoryempty. Mutually exclusive withfrom_repo.- from_repo
Fetch a git repo as the template source:
"[host/]owner/repo[/subdir][@ref]"shorthand (host defaults to github.com – an explicit host is how GitHub Enterprise is supported) or a full URL. Requiresgiton PATH. Mutually exclusive withfrom_dir.- ref
Branch/tag/SHA to check out. Only valid with
from_repo; overrides any@refembedded in its shorthand.- subdir
Subdirectory of the resolved repo to use as the template source. Only valid with
from_repo; overrides any subdirectory embedded in its shorthand.- type
Template name under a source's
templates/<name>/structure. Only valid alongsidefrom_dir/from_repo.- force
Overwrite existing
_quarto.yml/style YAML/shell.qmdfiles indirectory. Only meaningful alongsidefrom_dir/from_repo. DefaultFALSE.- python_env
Path to an existing Python environment (a venv, or a project directory holding a
.venv) that already haspython-docxandpyyaml. It is checked (naming any missing module;quartifyr_stylingitself comes from the copy bundled in this package) and then pyro and uv are skipped entirely: nothing is written topyproject.tomland no environment is created. Defaults togetOption("quartifyr.python_env"). For thestyling_*()calls inrender_report()to use it too, set that option (or theQUARTIFYR_PYTHON_ENVenvironment variable) in the project's.Rprofile. With it set,render_report()also runsreportifyr's pass 2 in that environment (which then needspillowtoo), so the project needs no.venv; set up the report folders and marker without pyro or the network withinitialize_report_project_offline().- install_extensions
Also install the Quarto extensions the project needs:
quarto-plusviainstall_quarto_extensions()(fromextensions_from, default the copy bundled in this package) and quartifyr's own viainstall_quartifyr_extension(), unless already present. Neither touches the network. DefaultFALSE.- extensions_from
Passed as
fromtoinstall_quarto_extensions(). Only used withinstall_extensions = TRUE.
Value
Invisibly, the result of pyro::initialize_python() (NULL when
python_env is used).
Details
When from_dir/from_repo is given (issue #23), also scaffolds
directory's _quarto.yml/style YAML/shell .qmd from a template
source instead of leaving directory empty: either a local directory
(from_dir) or a git repo (from_repo), fetched and materialized the
same way ../deckifyr's initialize_deck_project() does for its own
design.yaml/layouts.yaml/presentation.yaml. A template source is
either "flat" (a directory with its own _quarto.yml and exactly one
.qmd file at its root – its _quarto.yml/style.yaml, if present,
are duplicated as-is, paired with a freshly generated, minimal shell
.qmd) or "typed" (a directory with a templates/ subdirectory, each
holding its own named flat template – select one with type; its
shell .qmd is copied verbatim as a real minimal starter, not
emptied).
A stale-venv gotcha in existing reportifyr projects
On a project already initialized by reportifyr before it depended on
pyro (reportifyr < 0.4.0), this can fail with an opaque error from
pyro::initialize_python()'s underlying uv sync call – that
project's Python environment predates pyro/uv and isn't necessarily
something uv can adopt as-is. Run
reportifyr::initialize_report_project(project_dir = directory) first
(safe to re-run) to bring the environment up to a pyro-compatible
state before calling this function. This function catches that failure
and re-raises with a pointer to the same fix, instead of letting
pyro/uv's raw error propagate uninterpreted.
Offline use
Nothing here needs the network if what it would download is already
local; see the "Offline use" article on the package website for the whole
recipe.
Offline mode is on when options(quartifyr.offline = TRUE) or uv's own
UV_OFFLINE=1 is set. In it:
pyro's provisioning runs with
UV_OFFLINE=1, souv lock/uv syncresolve from uv's cache orUV_FIND_LINKSwheels instead of PyPI;if
uvisn't installed it stops with a message saying so, rather than letting pyro download and install uv from GitHub (into~/.local/bin);from_repois refused (clone the repository yourself and usefrom_dir);a failure that needed the network says which step, which resource, and how to provide it locally (error class
quartifyr_offline_failure).
Alternatively, python_env skips pyro and uv altogether.
Examples
if (FALSE) { # \dontrun{
# Provision the Python environment only (this mutates the calling
# project's pyproject.toml and provisions a Python/uv environment --
# run it somewhere disposable, not the current project).
project_dir <- file.path(tempdir(), "my-report")
initialize_quartifyr_project(project_dir)
# Also scaffold _quarto.yml/style.yaml/report.qmd from an org-standard
# template repo, pinned to a tag, selecting the "csr" template type.
initialize_quartifyr_project(
project_dir,
from_repo = "acme-org/quartifyr-templates@v2",
type = "csr"
)
} # }