Download button mixin
flask_download_btn.DownloadBtnMixin
class flask_download_btn.DownloadBtnMixin(btn_template=None, progress_template=None, **kwargs) [source]
Subclass the DownloadnBtnMixin
to create download button models.
Download buttons are responsible for:
- Rendering a download button, progress bar, and download script.
- Web form handling (optional).
- File creation (optional).
Parameters: | btn_template : str or None, default=None
Name of the download button html template. If
Name of the progress bar html template. If You can set the download button's attribtues by passing them as keyword arguments. |
---|---|
Attributes: | btn : sqlalchemy_mutablesoup.MutableSoup
Download button html soup. btn_tag : bs4.TagDownload button tag. btn_text : str, default='Download'Text of the download button. cache : str, default='no-store'Cache response directive. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control. callback : str or None, default=None
Name of the callback view function. If this is not List of downloads. Items of this list may be URLs (str) or (URL, file_name) tuples (str, str). download_msg : str, default=''Message which will be briefly displayed after the download has completed. downloaded : bool, default=FalseIndicates that the file(s) associated with this button has been downloaded. form_id : str or None, default=None
ID of the form processed by the download button. If you are not processing a form, or there is only one form on the page, leave this as Progress bar html soup. progress_bar : bs4.TagProgress bar tag. progress_text : bs4.TagProgress bar text tag. |
Function attributes: | handle_form_functions : list, default=[]
|
Methods
get_id(self, sfx) [source]
Get the CSS id associated with download button attributes.
Parameters: | sfx : str
Suffix of the CSS id. |
---|---|
Suffix values: | btn : download button tag
csrf : key for CSRF token lookup in session progress : progress bar <div> container
progress-bar : progress bar tag progress-txt : progress bar text
|
Returns: | id : str
Of the form 'self.model_id-sfx'. |
render_progress(self) [source]
Render the progress bar inside a container which sets the progress bar display style to none. The contents of the container may be updated with progress reports during file creation.
Returns: | progress : flask.Markup
Rendered progress bar wrapped in a display none container. Insert this into a |
---|
script(self) [source]
Render the download button script.
The script will call routes for form handling, file creation, and download. Authentication for these routes requires a CSRF token. This method creates a unique token and stores it in the session.
Returns: | script : flask.Markup
Rendered download button javascript. Insert this into a |
---|
clear_csrf(self) [source]
Clear CSRF token from session. Call this method to revoke client permission to download the file.
Returns: | CSRF token : str or None
|
---|
reset(self, stage='', pct_complete=None) [source]
Resets the progress bar by replacing its html. You will typically want to reset the progress bar at the start of a new stage.
Parameters: | stage : str, default=''
Stage of file creation. e.g. 'Compiling file 0'. pct_complete : float or None, default=None
Percent of the stage complete. If |
---|---|
Returns: | reset event : str
Server sent event to reset the progress bar. |
report(self, stage='', pct_complete=None) [source]
Reports file creation progress by updating the progress bar html.
Parameters: | stage : str, default=''
Stage of file creation. e.g. 'Compiling file 0'. pct_complete : float or None, default=None
Percent of the stage complete. If |
---|---|
Returns: | report event : str
Server sent event to update the progress bar. |
transition_speed(self, speed) [source]
Set the transition speed for the progress bar.
Parameters: | speed : str
Number of seconds for the transiton, e.g. '.5s' |
---|---|
Returns: | transition speed event : str
Server sent event to update the transition speed. |