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:

  1. Rendering a download button, progress bar, and download script.
  2. Web form handling (optional).
  3. File creation (optional).
Parameters: btn_template : str or None, default=None

Name of the download button html template. If None, the download button manager's btn_template is used.

progress_template : str or None, default=None

Name of the progress bar html template. If None, the download button manager's progress_template is used.

**kwargs :

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.Tag

Download 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 None, the client is redirected to the callback URL after download has completed.

downloads : list, default=[]

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=False

Indicates 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 None. If there are multiple forms on the page, set form_id to the ID of the form associated with the download button.

progress : sqlalchemy_mutbalesoup.MutableSoup

Progress bar html soup.

progress_bar : bs4.Tag

Progress bar tag.

progress_text : bs4.Tag

Progress bar text tag.

Function attributes: handle_form_functions : list, default=[]

HandleForm functions are executed sequentially after the download button is clicked. These functions process the response from any form which may be associated with the download button.

create_file_functions : list, default=[]

CreateFile functions are executed sequentially after the HandleForm functions.

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 <body> tag in a Jinja template.

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 <head> tag in a Jinja template.

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 None, the progress bar will display the stage without a percent complete message.

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 None, the progress bar will display the stage without a percent complete message.

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.