The pretext-cli package can be used directly from Python, without spawning
CLI subprocesses.
The two main entry points are:
Project.parse(...): load an existingproject.ptxmanifest.Project(...)+new_target(...): build projects programmatically when no manifest exists.
from pathlib import Path
from pretext import project as pr
# Case 1: You already have project.ptx
project = pr.Project.parse(Path("."))
project.get_target("web").build()
# Case 2: No project.ptx (for example, core/examples/epub)
project = pr.Project(ptx_version="2", source=Path(""), publication=Path(""))
target = project.new_target(
"ebook",
"epub",
source=Path("epub-sampler.xml"),
publication=Path("publication.xml"),
)
target.build()Project prepends its own base paths to each target's source and
publication paths.
- Default
Project.sourceissource/. - Default
Project.publicationispublication/.
When your files live directly in the project root (as in the core EPUB
example), setting both to Path("") makes target paths resolve from that root.
To mirror CLI-style log output in library usage:
import logging
from pretext import logger
log = logging.getLogger("ptxlogger")
logger.add_log_stream_handler()
log.setLevel(logging.INFO)To also log to files:
logger.add_log_file_handler(path_to_log_directory)If you want the running CLI version in your app logs, either:
- call
utils.report_version()for the built-in message, or - import
VERSIONand log it yourself.