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()