Skip to main content

Preloading Webimages

To optimize the performance and avoid having to download webimages individually each time they're needed, multiple webimages can be downloaded in parallel. For this, the webimages-library is used. First, a list of urls is retrieved from the XML-datafile using the data-provider-library. By default the content-attribute on all item-nodes with type="asset" are retrieved.

<item key="main_image" content="https://url/to/an/image" type="asset"/>
<item key="second_image" content="https://url/to/another/image" type="asset"/>

The list of urls can then be used as input for the parallel-download.

webimage_lib = comet.pyImport('[xml]/library/framework/webimages.py')
dataprovider_lib = comet.pyImport('[xml]/library/framework/data_provider.py')

asset_urls = dataprovider_lib.get_preload_urls(type="asset")

preloader = webimage_lib.WebImageBundle(asset_urls)
preloader.download_queue(url)

Paths to the downloaded images are cached. When loading images later on, in placeholder-scripts, the matching path to a url is used when evaluating the found url with the function webimages_lib.get_webimage_url().


image_url:str = dataprovider.get_attribute_value("main_image")
# => "https://url/to/an/image"

path:str = webimage_lib.get_webimage_url(image_url)
# => checking cached urls {"https://url/to/an/image":"/path/to/downloaded/image.jpg"}
# => "/path/to/downloaded/image.jpg"
info

See here for more information: webimages-library