Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamlit failed to hash an object #242

Closed
robotastic opened this issue Oct 2, 2019 · 17 comments
Closed

Streamlit failed to hash an object #242

robotastic opened this issue Oct 2, 2019 · 17 comments
Assignees
Labels
feature:cache Related to st.cache_data and st.cache_resource type:bug Something isn't working

Comments

@robotastic
Copy link

I am trying to get the DataFrame Demo from streamlit hello to work. I am running this using Anaconda and Python 3.6.

I get the following error in the browser:

Streamlit cannot hash an object of type <class 'os._Environ'>.,

More information: to prevent unexpected behavior, Streamlit tries to detect mutations in cached objects so it can alert the user if needed. However, something went wrong while performing this check.

Please file a bug.

To stop this warning from showing in the meantime, try one of the following:

Preferred: modify your code to avoid using this type of object.
Or add the argument ignore_cache=True to the st.cache decorator.

I get a bunch of additional errors on Streamlit failed to hash an object of type <class 'function'>.,

And then this error blob:

TypeError: object supporting the buffer API required
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 306, in _run_script exec(code, module.__dict__)
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hello.py", line 334, in <module> run()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hello.py", line 308, in run demo()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hello.py", line 247, in data_frame_demo df = get_UN_data()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/caching.py", line 544, in wrapped_func return get_or_set_cache()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/caching.py", line 509, in get_or_set_cache code_hasher.update(func)
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hashing.py", line 185, in update self._update(self.hasher, obj, context)
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hashing.py", line 217, in _update hasher.update(b)
@robotastic robotastic added the type:bug Something isn't working label Oct 2, 2019
@treuille
Copy link
Contributor

treuille commented Oct 2, 2019

Thanks for letting us know @robotastic! We're looking into this!

@cgpadwick
Copy link

cgpadwick commented Oct 3, 2019

I am seeing this too on my platform, ubuntu 18 python 3.6. It is the mapping demo. The plotting demo works fine, but none of the others do.

@cwerner
Copy link

cwerner commented Oct 3, 2019

same here, macos, conda, python 3.7.4

@cgpadwick
Copy link

I backed up to v0.46.0 pip install streamlit==0.46.0 and I don't get that same error. The demo page is a lot different but I was able to run the udacity self driving car tutorial without issue. So this problem seems confined to the latest release.

@takacsd
Copy link

takacsd commented Oct 4, 2019

Don't know if I should open a new bug or not, but I have a similar issue with my own code for:
<class 'tuple'>, <class 'list'>, <class 'pathlib.PosixPath'>.
Downgrading to 0.36 did not solve the issue...

@monchier
Copy link
Collaborator

monchier commented Oct 4, 2019

Hi @takacsd, this may be covered by this, but if you have some repro instructions I can verify it is actually the same bug.

@ahmedahmedov
Copy link

ahmedahmedov commented Oct 6, 2019

I have the same issue with a Mac OS (10.14.6) and Python 3.6. Downgrading to 0.46 didn't solve the issue

@sandbox
Copy link

sandbox commented Oct 7, 2019

I have the same issue as well with a Mac OS (10.14.6), Python 3.7, streamlit (0.47.4). Very willing to help through a call or something like that :)

My debugging information:

TypeError: object supporting the buffer API required
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/ScriptRunner.py", line 306, in _run_script exec(code, module.__dict__)
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hello.py", line 334, in <module> run()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hello.py", line 308, in run demo()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hello.py", line 247, in data_frame_demo df = get_UN_data()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/caching.py", line 544, in wrapped_func return get_or_set_cache()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/caching.py", line 509, in get_or_set_cache code_hasher.update(func)
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hashing.py", line 185, in update self._update(self.hasher, obj, context)
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hashing.py", line 217, in _update hasher.update(b)

@lcerman
Copy link

lcerman commented Oct 7, 2019

I have the same problem with streamlit 0.47.4, Python 3.6.8, Ubuntu 18.04 running inside WSL@Windows10.

@ahmedahmedov
Copy link

Workaround:
Commenting out @st.cache in the following piece of code solved the issue for me:

# This is the main app app itself, which appears when the user selects "Run the app".
def run_the_app():
    # To make Streamlit fast, st.cache allows us to reuse computation across runs.
    # In this common pattern, we download data from an endpoint only once.
    #@st.cache
    def load_metadata(url):
        return pd.read_csv(url)

@roycoding
Copy link

roycoding commented Oct 8, 2019

Commenting out the @st.cache decorators in hello.py (lines 69 and 240) allowed the mapping and dataframe demos to work for me (release 0.47.4).

Thanks @ahmedahmedov

@lcerman
Copy link

lcerman commented Oct 8, 2019

Hello again, I just found something interesting. If I run the Udacity demo with:

streamlit run https://raw.githubusercontent.com/streamlit/demo-self-driving/master/app.py

It runs just fine!

But if I download the file app.py to the local filesystem and run:

streamlit run app.py

Then it fails with four complaints Streamlit failed to hash an object of type <class 'function'>.... in yellow blocks followed by the error message:

TypeError: object supporting the buffer API required
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 306, in _run_script exec(code, module.__dict__)
File "/mnt/c/zzz/streamlit_udacity_demo/app.py", line 296, in <module> main()
File "/mnt/c/zzz/streamlit_udacity_demo/app.py", line 45, in main run_the_app()
File "/mnt/c/zzz/streamlit_udacity_demo/app.py", line 109, in run_the_app metadata = load_metadata(os.path.join(DATA_URL_ROOT, "labels.csv.gz"))
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/caching.py", line 544, in wrapped_func return get_or_set_cache()
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/caching.py", line 509, in get_or_set_cache code_hasher.update(func)
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/hashing.py", line 185, in update self._update(self.hasher, obj, context)
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/hashing.py", line 217, in _update hasher.update(b) 

@treuille
Copy link
Contributor

treuille commented Oct 9, 2019

@lcerman: I believe the issue you're seeing is this one, which has a different cause.

@lcerman
Copy link

lcerman commented Oct 10, 2019

@lcerman: I believe the issue you're seeing is this one, which has a different cause.

Interestingly, the bug #167 exhibits wrong behavior of the remote code, while in my case the remote code works fine, but the local code fails (this is not only problem of the Udacity demo, it fails for every cached function in the other tutorials or in my own code).

@treuille treuille added the feature:cache Related to st.cache_data and st.cache_resource label Oct 11, 2019
@tvst
Copy link
Contributor

tvst commented Oct 12, 2019

The fix landed on develop a couple of days ago.

But since this bug is so popular, I'll leave this open until the release goes out. This way other people are more likely to find it.

tvst added a commit that referenced this issue Oct 13, 2019
* Add "anaconda" and "venv" to folder blacklist (based on #242)

* Improve st.cache error message to user.
@tvst
Copy link
Contributor

tvst commented Oct 14, 2019

Released as 0.48.1

More info: When you st.cache a function, Streamlit's caching mechanism traverses all function calls inside it to track whether the called function's body was edited since the cached function was last cached. And Streamlit already ignored "library" functions, since those can be assumed to be unmodified.

However, the way we defined library functions was "functions that were defined outside your current working directory its subfolders". But this broke whenever your Python environment was placed inside the cwd!

So since version 0.48.1 we define "library function" as any function that was defined:

  • Outside your current working directory its subfolders
  • In a blacklisted folder:
    • The default blacklist is:
      "**/.*",
      "**/anaconda",
      "**/anaconda2",
      "**/anaconda3",
      "**/miniconda",
      "**/miniconda2",
      "**/miniconda3",
      "**/venv",
      "**/virtualenv",
      "**/pyenv",
      
    • Plus anything you specify with the Streamlit config option server.folderWatchBlacklist.

In addition, we also added this explanation to our error message:

This error can occur when your virtual environment lives in the same
folder as your project, since that makes it hard for Streamlit to
understand which files it should check. If you think that's what caused
this, please add the following to ~/.streamlit/config.toml:

[server]
folderWatchBlacklist = ['foldername']

...where foldername is the relative or absolute path to the folder
where you put your virtual environment.


Of course, this still doesn't solve the underlying problem of why those library functions were not hashable in the first place. We are working on that right now, but since there are a few different cases where things may not be hashable, I'd rather create one bug for each case than have a long-stanging bug with lots of mixed conversations.

Also, for almost everyone who reported this issue, the script that caused the bug was streamlit hello (which we know is 100% hashable!) and the tracebacks provided show the error was always due to library functions not being hashable (which is what we fixed now). So I'll close this bug now.

@tvst tvst closed this as completed Oct 14, 2019
@lcerman
Copy link

lcerman commented Oct 15, 2019

Have tested the new version and I confirm its seems fixed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:cache Related to st.cache_data and st.cache_resource type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests