annie.pages.download¶
Save a generated document to the user’s machine, with their choice of folder and name.
Annie’s server runs where the data is — often inside a Docker container — so a server-side folder picker would browse the wrong filesystem. The browser is where the person is, so the save happens there: Chromium browsers (Chrome, Edge, Brave, …) open the operating system’s native Save as dialog through the File System Access API, starting in the user’s Documents folder with the suggested name prefilled. Browsers without it (Firefox, Safari) fall back to a normal download using the suggested name. The content is generated on the server, fetched by the browser from a small route, and written to disk there — server → browser → disk — so local runs and Docker behave identically.
Attributes¶
Extension → MIME type for the formats Annie exports. |
Functions¶
|
Make |
|
Return a click handler that fetches |
|
Make clicking |
|
Add a menu entry that opens the native Save dialog straight away when clicked. |
Module Contents¶
- annie.pages.download.MIME_TYPES: dict[str, str]¶
Extension → MIME type for the formats Annie exports.
- annie.pages.download.sanitize_filename(name: str, *, extension: str, default_stem: str) str[source]¶
Make
namea safe file name that ends in.extension.Path separators and characters illegal on Windows are replaced, surrounding dots and spaces trimmed, an empty result falls back to
default_stem, and the extension is forced (added if missing, never doubled).- Parameters:
name – The name the user typed (may be empty or contain a path).
extension – The required extension without the dot, e.g.
"csv".default_stem – Stem to use when
namehas nothing usable.
- Returns:
A file name such as
annie_events_clip1.csv.
- annie.pages.download.save_from_url_js(filename: str, url: str, mime: str) str[source]¶
Return a click handler that fetches
urland saves it where the user picks.The handler runs client-side inside the click: it fetches the file first (a local, millisecond request, well inside the browser’s transient-activation window), then opens the native Save dialog with
filenameprefilled and writes the file. A204response means “nothing to export” and shows a notice instead of an empty file. Cancelling the dialog is silent. Browsers withoutshowSaveFilePicker(Firefox, Safari) fall back to a normal download underfilename.- Parameters:
filename – Suggested file name, already sanitised.
url – Same-origin URL returning the file’s content (text or binary).
mime – MIME type, e.g.
"text/csv".
- Returns:
A JavaScript arrow function (as a string) for
Element.on(..., js_handler=...).
- annie.pages.download.attach_save(element: nicegui.ui.element, *, filename: str, url: str, extension: str) None[source]¶
Make clicking
elementopen the native Save dialog straight away.- Parameters:
element – A button or menu item.
filename – Default file name offered in the Save dialog.
url – Same-origin URL serving the file (see
save_from_url_js()).extension – File extension without the dot (selects the MIME type).
Add a menu entry that opens the native Save dialog straight away when clicked.
- Parameters:
label – Menu text.
filename – Default file name offered in the Save dialog.
url – Same-origin URL serving the file (see
save_from_url_js()).extension – File extension without the dot (selects the MIME type).
- Returns:
The created menu item.