Skip to content

Instantly share code, notes, and snippets.

@mturoci
mturoci / app.py
Last active November 2, 2022 13:41
Temporary workaround to prevent 1st frame from being dropped during streaming.
# Image / Stream
# Display an image and continuously update it in real time.
# ---
import io
import time
import uuid
import cv2
from h2o_wave import app, Q, ui, main
import numpy as np
@mturoci
mturoci / app.py
Created September 20, 2022 16:23
import concurrent.futures
from h2o_wave import main, app, Q, ui
import boto3
import asyncio
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.
@mturoci
mturoci / app.py
Created September 20, 2022 16:21
# Get the current event loop to update UI later.
loop = asyncio.get_event_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
await q.exec(pool, download, q, loop)
@mturoci
mturoci / app.py
Created September 20, 2022 16:21
# Get the current event loop to update UI later.
loop = asyncio.get_event_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
await q.exec(pool, download, q, loop)
@mturoci
mturoci / app.py
Created September 20, 2022 16:20
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
@mturoci
mturoci / app.py
Created September 20, 2022 16:20
from threading import Event
import httpx
from h2o_wave import Q, app, main, ui
async def download_file(q: Q):
# Create a tmp file to store the downloaded contents.
with open('my_download', 'wb') as tmp_file:
# Random URL to download a 100MB file.
@mturoci
mturoci / app.py
Created September 20, 2022 16:19
if q.args.cancel:
q.client.event.set()
@mturoci
mturoci / app.py
Created September 20, 2022 16:19
q.client.event = Event()
await download_file(q)
@mturoci
mturoci / app.py
Created September 20, 2022 16:17
async def download_file(q: Q):
# Create a tmp file to store the downloaded contents.
with open('my_download', 'wb') as tmp_file:
# Random URL to download a 100MB file.
url = 'https://speed.hetzner.de/100MB.bin'
# Get async HTTP client.
async with httpx.AsyncClient() as client:
# Stream the response to get periodic download updates.
async with client.stream('GET', url) as response:
total = int(response.headers['Content-Length'])
@mturoci
mturoci / app.py
Created September 20, 2022 16:17
import concurrent.futures
from threading import Event
import time
from h2o_wave import main, app, Q, ui
# This takes a lot of time (compute heavy).
async def blocking_function(q: Q):
count = 0
total = 10