Project¶
Design projects: the manifest, building, previews and presets. See the Projects guide.
- class Project(config: ProjectConfig)[source]¶
Bases:
objectA design project: a folder with
svgwriter.toml, the code that draws it and the files it builds.The manifest names the entry (
"main:build", a function that draws into a document of the project’s size and theme), the theme file and the output formats.[tool.svgwriter]inpyproject.tomlworks as well, with the same keys. Nothing is read implicitly: importing svgwriter or creating a document never looks for a manifest; onlyload()and thesvgwriter buildandsvgwriter previewcommands do.- Parameters:
config (ProjectConfig) – The settings, usually from
load().
See also
svgwriter.tools.project.presetsRegister a starter folder for
new().
Notes
The entry may be a function taking a
Document, aSceneclass, or a list of either (one slide or page each), and may live in a notebook ("main.ipynb:build"). Modules the project imports, and the animations and sizes they register, are forgotten after each build, so a build always runs the current files.Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... project = svg.Project.new(f"{folder}/card", preset="blank") ... print([path.name for path in project.build()]) ['card.svg', 'card.png']
- classmethod load(path: str | PathLike[str] = '.') Project[source]¶
Read the project whose manifest is in path or the nearest folder above it.
svgwriter.tomlcomes first; otherwise apyproject.tomlwith a[tool.svgwriter]table.- Parameters:
path (str or os.PathLike, optional) – Where to start looking. Defaults to the current folder.
- Returns:
Project – The project.
- Raises:
ProjectError – If no manifest is found, or it has an unknown key or a bad value (the message names the file and key).
Examples
>>> import svgwriter as svg, tempfile, pathlib >>> with tempfile.TemporaryDirectory() as folder: ... _ = pathlib.Path(folder, "svgwriter.toml").write_text('[project]\nname = "demo"\nsize = "a4"') ... print(svg.Project.load(folder).config.size) a4
- classmethod new(path: str | PathLike[str], preset: str = 'blank', *, notebook: bool = False, name: str | None = None, theme: str | None = None, size: str | Sequence[float | str] | None = None) Project[source]¶
Create a project folder from a preset and load it.
- Parameters:
path (str or os.PathLike) – The new folder; it must not exist or be empty.
preset (str, optional) – A name from
svgwriter.tools.project.presets. Defaults to"blank".notebook (bool, optional) – Write
main.ipynbinstead ofmain.py. Defaults to False.name (str, optional) – The project name. Defaults to the folder name.
theme (str, optional) – A built-in theme name that
theme.tomlextends. Defaults to None, the preset’s own.size (str or sequence of (float or str), optional) – A size name (
"a4") or[width, height], written tosvgwriter.toml. Defaults to None, the preset’s own.
- Returns:
Project – The new project, ready to build.
- Raises:
ProjectError – If the preset, theme or size is unknown (the message lists the choices) or the folder is not empty.
Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... print(svg.Project.new(f"{folder}/talk", preset="slides").config.formats) ('pptx', 'pdf')
- build(formats: Sequence[str] | None = None, *, values: Mapping[str, Any] | None = None) list[Path][source]¶
Draw the project and write its outputs into the output folder.
Fonts in the font folder are registered first, the theme applies to every page and scene (also to scenes the entry module makes when it is imported), and the project’s code runs in the project folder, so relative paths such as
assets/data/values.csvwork.- Parameters:
formats (sequence of str, optional) – Formats to write. Defaults to None, the
[outputs] formatsof the manifest.values (mapping, optional) – Values for the controls the entry declares (
svgwriter.tools.widgets.controls()), by parameter name. Defaults to None, their starting values.
- Returns:
list of pathlib.Path – The files written, in format order.
- Raises:
ProjectError – If the entry, the theme or a format cannot be found.
Notes
With a notebook entry, a call from inside that notebook uses the notebook’s own entry object; any other call runs the notebook in a new kernel (the
notebookextra) and builds there, so both write the same files.Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... project = svg.Project.new(f"{folder}/sheet", preset="poster") ... print([path.name for path in project.build(["svg"])]) ['sheet.svg']
- controls() dict[str, Control][source]¶
The controls the entry’s page functions declare with
svgwriter.tools.widgets.controls().svgwriter previewshows them as a form above the outputs;svgwriter builduses their starting values. Notebook entries declare none here.- Returns:
dict of str to Control – One control per parameter name, across the entry’s functions.
- Raises:
ProjectError – If the entry cannot be found.
Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... print(svg.Project.new(f"{folder}/card").controls()) {}
- preview(formats: Sequence[str] | None = None) Preview[source]¶
Build the outputs and show them: in a notebook each file appears inline, and animations play.
- Parameters:
formats (sequence of str, optional) – Formats to build and show. Defaults to the manifest’s.
- Returns:
Preview – Displays as HTML in notebooks;
preview.pathslists the files.
See also
serveA browser page that rebuilds on every save.
Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... shown = svg.Project.new(f"{folder}/card").preview(["svg"]) ... print("<img" in shown._repr_html_()) True
- serve(port: int | None = None, *, open_browser: bool | None = None) None[source]¶
Serve a live preview at
http://127.0.0.1:<port>until interrupted (Ctrl+C).The page shows every page and scene as SVG, drawn in memory. Controls the entry declares (
svgwriter.tools.widgets.controls()) appear in a panel beside or below the drawing, and the drawing follows them as they move. Saving a file imports the project again and redraws; once the project has been quiet for a moment the output files are written too, as the page’s Export button does. A failed drawing shows its traceback under the last drawing that worked. The manifest’s[preview]table (PreviewConfig) sets the layout, backdrop, port and more.- Parameters:
See also
previewThe output files inline in a notebook.
Examples
>>> import svgwriter as svg >>> svg.Project.load().serve()
- class ProjectConfig(root: Path, source: Path, name: str, entry: str = 'main:build', size: str | ~svgwriter._document.sizes.PageSize | tuple[~svgwriter.model.units.lengths.Length, ~svgwriter.model.units.lengths.Length] | None=None, theme: str | None = None, fonts: str = 'assets/fonts', formats: tuple[str, ...]=('svg', 'png'), options: Mapping[str, ~collections.abc.Mapping[str, ~typing.Any]]=<factory>, output: str = 'output', preview: PreviewConfig = <factory>)[source]¶
Bases:
objectThe settings of a project, read from its manifest.
- Parameters:
root (pathlib.Path) – The project folder (where the manifest is).
source (pathlib.Path) – The file the settings came from.
name (str) – The project name; output files are named after it.
entry (str) –
"module:object"or"notebook.ipynb:object".size (str, PageSize, (Length, Length) or None) – A named size, a
PageSizeor a width and height for the documents handed to the entry.theme (str or None) – A theme file (relative to root) or a registered theme name.
fonts (str) – Folder whose font files are registered before building.
options (Mapping) – Exporter options per format.
output (str) – Output folder, relative to root.
preview (PreviewConfig) – How
svgwriter previewshows the project.
Examples
>>> from pathlib import Path >>> from svgwriter.tools.project import ProjectConfig >>> ProjectConfig(Path("."), Path("svgwriter.toml"), "demo").formats ('svg', 'png')
- preview: PreviewConfig¶
- class PreviewConfig(pages: tuple[str, ...] | None = None, background: str = 'checker', layout: str = 'auto', fit: str = 'contain', port: int = 8000, open: bool = True, debounce_ms: int = 80, build_on_save: bool = True, formats_on_build: tuple[str, ...] | None = None)[source]¶
Bases:
objectHow
svgwriter previewshows a project, read from the manifest’s[preview]table.- Parameters:
pages (tuple of str or None) – The pages and scenes to show, by output name, in this order. None shows all of them.
background (str) – Behind the drawing:
"checker","light","dark"or a colour.layout (str) – Where the controls sit:
"auto"(right of the drawing on wide windows, below it otherwise),"right"or"bottom".fit (str) –
"contain"scales the drawing to its area;"actual"shows it at its own size.port (int) – The local port.
open (bool) – Open the page in a browser on start.
debounce_ms (int) – Milliseconds the page waits after a control moves before asking for a new drawing.
build_on_save (bool) – Write the outputs after a file is saved and the project has been quiet for a moment.
formats_on_build (tuple of str or None) – The formats that saving and the page’s Export button write. None writes
[outputs] formats.
Examples
>>> from svgwriter.tools.project import PreviewConfig >>> PreviewConfig().layout, PreviewConfig().port ('auto', 8000)
- class Preview(paths: Sequence[Path], title: str = '')[source]¶
Bases:
objectA project’s output files shown inline: images play, SVG animations run, PDF and PowerPoint pages appear as drawings.
Notebooks display it as HTML;
pathslists the files.- Parameters:
paths (sequence of pathlib.Path) – The files to show.
title (str, optional) – A heading above them. Defaults to
"".
See also
svgwriter.Project.previewBuilds the project and returns one.
Examples
>>> from svgwriter.tools.project import Preview >>> Preview([])._repr_html_().endswith("<p>No outputs.</p></div>") True
- class Preset(name: str, description: str, directory: Path, extends: str | None = None)[source]¶
Bases:
objectA starter project: a folder of files, optionally laid over another preset’s.
In the folder, a
-tplsuffix is dropped from file names (main.py-tplbecomesmain.py, so the templates are not importable modules) and adot-prefix becomes a dot (dot-gitignore). Text files have{{ project_name }}and{{ preset }}replaced.- Parameters:
name (str) – The name given to
svgwriter new --preset.description (str) – One line for
svgwriter presets.directory (pathlib.Path) – The folder copied into the new project.
extends (str or None, optional) – A preset whose files are copied first. Defaults to None.
Examples
>>> import svgwriter as svg >>> svg.project.presets.get("poster").extends 'blank'
- presets¶
what
svgwriter new NAME --preset <name>andProject.new()start from.Register a
Presetwithregister()(its folder must exist andextendsname a registered preset);svgwriter presetslists them. Packages add presets through thesvgwriter.project_presetentry-point group.Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... preset = svg.project.presets.register(svg.project.Preset("badge", "A round badge", folder, "blank")) ... print("badge" in svg.project.presets.list()) ... _ = svg.project.presets.unregister("badge") True
- Type:
Project presets by name
- class ProjectPresets(kind: str, builtins: Callable[[], object] | None = None, *, group: str | None = None)[source]¶
-
Project presets by name: what
svgwriter new NAME --preset <name>andProject.new()start from.Register a
Presetwithregister()(its folder must exist andextendsname a registered preset);svgwriter presetslists them. Packages add presets through thesvgwriter.project_presetentry-point group.Examples
>>> import svgwriter as svg, tempfile >>> with tempfile.TemporaryDirectory() as folder: ... preset = svg.project.presets.register(svg.project.Preset("badge", "A round badge", folder, "blank")) ... print("badge" in svg.project.presets.list()) ... _ = svg.project.presets.unregister("badge") True