flat_file_renderers.base module
- class flat_file_renderers.base.BaseRenderer(context: dict[str, any])[source]
Bases:
objectBase class for dataset renderers.
Defines the interface and basic structure for concrete renderers (CSV, Excel, Parquet, etc). Subclasses must implement the abstract render method to provide the file-generation logic.
- context
Context dict used to render the file. May hold input data, a validated form, the requesting user, etc.
- Type:
dict
Example
renderer = CsvRenderer({‘columns’: [“Name”, “Age”], ‘rows’: [{“Name”: “Alice”, “Age”: “30”}]}) csv_file = renderer.render() with open(“data.csv”, “wb”) as f:
f.write(csv_file.getvalue())
- property default_filename: str
Builds a default filename from the current date/time and the renderer’s extension.
- Returns:
Suggested filename.
- Return type:
str
- property extension: str
Returns the default file extension produced by this renderer.
- Returns:
File extension (e.g. ‘csv’, ‘xlsx’).
- Return type:
str
- abstractmethod render(*args, **kwargs) BytesIO[source]
Abstract method that renders the data and produces the file.
Subclasses must implement this to provide the format-specific generation logic.
- Returns:
In-memory generated file.
- Return type:
BytesIO
- write_zipfile(entries: list[ZipFileEntry]) Path[source]
Compresses one or more files into a temporary ZIP file.
- Parameters:
entries (list[ZipFileEntry]) – Entries to compress.
- Returns:
Path to the generated ZIP file.
- Return type:
Path
Examples: ```python zip_path = renderer.write_zipfile([ZipFileEntry(“data.csv”, Path(“/path/to/data.csv”))])
- with open(zip_path, “rb”) as f:
zip_content = f.read()
from zipfile import ZipFile with ZipFile(zip_path, ‘r’) as zipf:
print(zipf.namelist())