Skip to content

Instantly share code, notes, and snippets.

@mturoci
Created September 20, 2022 16:20
Show Gist options
  • Save mturoci/a2cd34b4e6f6a9d0da0f5a1708637e6f to your computer and use it in GitHub Desktop.
Save mturoci/a2cd34b4e6f6a9d0da0f5a1708637e6f to your computer and use it in GitHub Desktop.
def download(q: Q, loop: asyncio.AbstractEventLoop):
bucket = 'h2o-wave'
file_key = 'releases/h2o_wave-0.22.0-py3-none-win_amd64.whl'
# Get the total size of the file.
total = boto3.client('s3').head_object(Bucket=bucket, Key=file_key)['ContentLength']
already_downloaded = 0
# Nested update function that is called periodically from within boto.
def update(val):
nonlocal already_downloaded
already_downloaded += val
# Update the Wave UI.
asyncio.ensure_future(update_ui(q, already_downloaded / total), loop=loop)
# Since boto3 is synchronous, this download is blocking.
boto3.resource('s3').Bucket(bucket).download_file(
file_key, 'hello.whl', Callback=update
)
asyncio.ensure_future(show_notification(q), loop=loop)
async def show_notification(q: Q):
q.page['meta'].dialog = None
q.page['meta'].notification_bar = ui.notification_bar(
name='success_notification',
text='Job done!',
type='success',
events=['dismissed']
)
await q.page.save()
async def update_ui(q: Q, value: int):
q.page['meta'].dialog.items[0].progress.value = value
await q.page.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment