download.py 808 B

12345678910111213141516171819202122
  1. from pathlib import Path
  2. from typing import Optional, Union
  3. from .. import core, helpers
  4. from ..context import context
  5. def download(src: Union[str, Path, bytes], filename: Optional[str] = None, media_type: str = '') -> None:
  6. """Download
  7. Function to trigger the download of a file, URL or bytes.
  8. :param src: target URL, local path of a file or raw data which should be downloaded
  9. :param filename: name of the file to download (default: name of the file on the server)
  10. :param media_type: media type of the file to download (default: "")
  11. """
  12. if not isinstance(src, bytes):
  13. if helpers.is_file(src):
  14. src = core.app.add_static_file(local_file=src, single_use=True)
  15. else:
  16. src = str(src)
  17. context.client.download(src, filename, media_type)