Is there a hack, which does not involve modifying the streamlit library, to use st.cache() without it hashing the code? If not, why not?
(It would allow the developer to control exactly what is looked at. It might also break codes which first referenced a st object in the code which is to be decorated by st.cache, though I did not try. But then, doing so without removing the code hashing does not run already because the hashing would fail first.)
I am displaying a kind of DeepDream. In that version, there are several resizings interleaved with ‘‘dream’’ generations. After each ‘‘dream’’ at a size, I change the content of a st.image that I previous referenced (to see the intermediate results as they are being computed). After the last resize and ‘‘dream’’, I return the newly generated image and put into the previous reference of the st.image an ImageChops.blend of the original image and the generated image. I created a slider for the blending (the alpha argument).
Now, when moving the slider I only want the blending to be recalculated from the previously generated image (without generating again the ‘‘dream’’ image).
Because I have some ‘‘st.image’’ in the code (and some other things), the code hashing fails.
- I could sub-divide my function and make sub functions that only contain the computation part and then decorate just these. But that is going to require quite some efforts and suddenly streamlit would appear less appealing to me in its usage.
- Commenting lines 522 to 524 of the caching.py file of the library allowed me to do just that at the cost of not working with the official branch of streamlit.
Just as an example of the problem caused by the code hashing (the ‘‘f function’’ I use for my application is way uglier, this f is enough to trigger the ‘‘TypeError: object supporting the buffer API required’’):
original_img = get_img_from_url(img_url) #where original_img is a PIL Image returned from a string containing a valid url of a png or jpg image
img2 = st.image(original_img)
dreamed_img = cached_dream(img_url)