Hi ,
I am new to streamlit. While trying to upload a text file as shown in below code using @st.cache, I am getting error to use hash function on it. COuld anyone please help me to resolve this issue…
**UnhashableType** : Cannot hash object of type _io.StringIO
While caching some code, Streamlit encountered an object of type `_io.StringIO` . You’ll need to help Streamlit understand how to hash that type with the `hash_funcs` argument. For example:
```
@st.cache(hash_funcs={_io.StringIO: my_hash_func})
def my_func(...):
...
```
You are hitting a case where Streamlit doesn’t know how to compute the hash for an object of type StringIO which is used inside pd.read_table on your file, so Streamlit knows if it has already computed and put into cache a similar uploaded file passing through StringIO. We need to indicate how to hash it through the hash_funcs argument.
Since it’s a string buffer, I think a first good way would be to download the content of the uploaded file and have Streamlit hash that, so if you upload the same file, it “checks” the content and if the content is the same as your previous uploaded file it doesn’t rerun the computation and fetches the computed pandas DataFrame.
You can download the whole file content through StringIO.getvalue so the following should work :
from io import StringIO
@st.cache(hash_funcs={StringIO: StringIO.getvalue})
def load_data(file_uploaded):
...
Unfortunately I think that makes the function read the full file twice, one for cache detection and one for the actual computation, and if the file is big that may be long. A better way would be to only read the beginning of the buffer in the hash_funcs instead of the whole file .
If you are new to Streamlit and want to learn more about it (especially how it checks for objects it has already run on), then you may benefit from reading the Caching and Advanced caching for other techniques caching techniques.
Thanks @andfanilo for explaining its usage. Its working now. But, when i use checkbox/selectbox/radio features to filter data to be shown as plots on tool, it reruns entire app.py script and starting from loading data again. Have I overlooked or missed something to include in my code to avoid this kind of problems.
My code is given here:
df_uploaded = st.sidebar.file_uploader(‘Choose txt file:’,type = [‘txt’,‘csv’])
@st.cache(hash_funcs={StringIO: StringIO.getvalue})
def load_data(file_uploaded):
return (pd.read_table(file_uploaded,header=None,encoding=‘utf-8’))
if df_uploaded:
temp = load_data(df_uploaded)
select_cols = st.selectbox('Choose column for analysis', list(temp.columns))
if select_cols:
** code to plot the variable distribution/scatter plot
This seems to be a bug on our side, I filed an issue for it here. Thanks for bringing it to our attention and feel free to follow/add any additional context in the GitHub issue!
I’m not sure if there is a bug here. Is the issue that your report is running slow, giving the impression that the data is being reloaded?
Perhaps this is due to your hash_funcs which uses getvalue to determine the hash, thus retrieving the entire contents of the file each time we call load_data.
Could you try sampling the data to determine the hash?
This should give you a speed improvement, however your hash will be slightly unreliable to determine true equality of the given StringIO object.
Supporting BytesIO/StringIO directly in @st.cache was officially merged in 0.57.0 . Also included in 0.57.0 is more detailed error messages for st.cache to help with debugging similar issues.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.