UnhashableTypeError: Cannot hash object of type _json.Scanner

Hi there! I’m using st.cache on this function which requests data from Coin Market Cap and then parses it using Beautiful Soup. I’m getting the UnhashableTypeError.

I went through the advance caching section in the docs and a few posts regarding this same issue, but I’m still not sure how to go about solving this using hash_funcs in my case.

Any help is appreciated! Thanks!

@st.cache()
def load_data():
    cmc = requests.get('https://coinmarketcap.com')
    soup = BeautifulSoup(cmc.content, 'html.parser')

    data = soup.find('script', id='__NEXT_DATA__', type='application/json')
    coins = {}
    coin_data = json.loads(data.contents[0])

    global_metrics = coin_data['props']['pageProps']['globalMetrics']
    total_marketcap = global_metrics['marketCap']
    btc_market_share = global_metrics['btcDominance']
    eth_market_share = global_metrics['ethDominance']

    latest_listing_data = coin_data['props']['initialState']['cryptocurrency']['listingLatest']['data']
    keysArr = latest_listing_data[0]['keysArr']
    listings = latest_listing_data[1:]

    query = 'quote.' + currency_price_unit + '.'

    coin_name = []
    coin_symbol = []
    market_cap = []
    percent_change_1h = []
    percent_change_24h = []
    percent_change_7d = []
    price = []
    volume_24h = []

    for i in listings:

      i = {key : value for key, value in zip(keysArr, i)}

      coin_name.append(i['slug'])
      coin_symbol.append(i['symbol'])
      price.append(i[query+'price'])
      percent_change_1h.append(i[query+'percentChange1h'])
      percent_change_24h.append(i[query+'percentChange24h'])
      percent_change_7d.append(i[query+'percentChange7d'])
      market_cap.append(i[query+'marketCap'])
      volume_24h.append(i[query+'volume24h'])

    df = pd.DataFrame(columns=['coin_name', 'coin_symbol', 'market_cap', 'percent_change_1h', 'percent_change_24h', 'percent_change_7d', 'price', 'volume_24h'])
    df['coin_name'] = coin_name
    df['coin_symbol'] = coin_symbol
    df['price'] = price
    df['percent_change_1h'] = percent_change_1h
    df['percent_change_24h'] = percent_change_24h
    df['percent_change_7d'] = percent_change_7d
    df['market_cap'] = market_cap
    df['volume_24h'] = volume_24h

    return df, total_marketcap, btc_market_share, eth_market_share

df, total_marketcap, btc_market_share, eth_market_share = load_data()

I’m hitting this issue as well using the code:

@st.cache(allow_output_mutation=True)
def fetch_data_json(filename: str) -> Dict[str, Any]:
    with open(filename, "r") as f:
        config = json.load(f)
    return config

Unclear how to solve this as previous suggestions say to allow output mutations but that obviously doesn’t fix the problem here…

i had the same issue, but when I made sure the version of streamlit was explicitly specified as1.10.0 in requirements.txt it went away. hope this helps

My streamlit version has been pinned the whole time. See UnhashableTypeError: Cannot hash object of type _json.Scanner... · Issue #4876 · streamlit/streamlit · GitHub for the issue thread. Same code in 1.10.0 fails but in 1.9.2 it works.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.