Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import numpy as np
- import requests
- import streamlit as st
- from concurrent.futures import ThreadPoolExecutor
- @st.cache(suppress_st_warning=True, allow_output_mutation=True)
- def make_image(id, opacity, column_index):
- print('requesting image from api')
- print(f'id:{id} oppacity:{opacity}, column:{column_index}')
- # image get request, normally we would load the image from disk
- img = requests.get(url='https://picsum.photos/100/100')
- return img.content
- # standard caching without specifiying the hash func gives: UnhashableTypeError: Cannot hash object of type streamlit.delta_generator.DeltaGenerator, found in the arguments of show_image().
- # @st.cache(suppress_st_warning=True,allow_output_mutation=True)
- def show_image(id, column, column_index):
- print('showing image')
- with column:
- category = st.selectbox('category', ['base', 'ground truth', 'prediction'], key=str(column_index)+ 'cat' + str(id))
- print(f'cat: {category}')
- oppacity = st.select_slider(
- "Oppacity",
- [round(i, 2) for i in np.linspace(0, 1, 21).tolist()], # dat_filtered["x1"].tolist()
- value=0.5,
- key=str(column_index) + 'op' + str(id)
- )
- st.image(
- make_image(id=id, opacity=oppacity, column_index=column_index),
- # make_caption(d),
- use_column_width=False,
- output_format="PNG",
- channels="RGB",
- )
- def show_image_threadpool_test(id, column, column_index):
- print(f'threadpool: id: {id}, column: {column}, column_index: {column_index}')
- ######################################
- ######################################
- st.beta_set_page_config(layout="wide")
- column1, column2 = st.beta_columns(2)
- #Regular FOR loop
- for i in range(0, 200):
- show_image(i, column1, column_index=0)
- show_image(i, column2, column_index=1)
- #ThreadPoolExecutor
- #with ThreadPoolExecutor(max_workers=20) as exec:
- # for i in range(0, 200):
- # #this needs some work to sync both images, so they display next to each other
- # #as completed could help
- # exec.submit(show_image_threadpool_test, i, column1, 0)
- # exec.submit(show_image_threadpool_test, i, column2, 1)
Add Comment
Please, Sign In to add comment