Using imported functions inside cached functions

Hi,

Currently experimenting a fair bit with Streamlit as I found it has great potential.

Trying to clean up a bit my files, I tried to put functions used in other cached functions, into another module

So replacing :

@st.cache(allow_output_mutation=True)
def get_data(site):
return get_site_data()

with this:

import ccb

@st.cache(allow_output_mutation=True)
def get_data(site):
return ccb.get_site_data()

the function ccb.get_site_data() returns the correct data when used at a different location.
However, inside the cached function, I get an error in streamlit:
Streamlit failed to hash an object of type <class β€˜function’>.

TypeError : object supporting the buffer API required
Traceback:
…
File β€œc:\users\ \cbflcp~1\venv~1\lib\site-packages\streamlit\hashing.py”, line 241, in _update hasher.update(b)

I am on Win10 with python 3.7.5 and streamlit 0.51 running in a venv

I do not quite understand why the same function cannot be in a different module…

Is it due to the cache function?

Is there any way around it or all components of my cached functions need to be in the streamlit script ?

Thanks a lot,
Fabien

Replying to myself,
it turns out that if I explicitly import the function from that module, then it works:

from ccb import get_site_data

@st.cache(allow_output_mutation=True)
def get_data(site):
return get_site_data()

Not sure if that is a bug or expected behavior, but it might help someone else.

4 Likes