-
Notifications
You must be signed in to change notification settings - Fork 561
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
KeyError: 'slug' in App 6 EDA Cryptocurrency #9
Comments
Hi, I am facing the same problem, have you solved this? |
tried stackoverflow and other websites still not working
…On Thu, 2 Dec 2021, 7:42 pm mostafa gafer, ***@***.***> wrote:
Hi, I am facing the same problem, have you solved this?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#9 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJOOXTJGJ3L2SWIBIK2XQILUO55ETANCNFSM5GZVY7HQ>
.
|
@ComputerScientist-01 @mostafagafer if you found the solution please share . I am having the same issue. |
@puranjay123 no solution has been found yet |
@ComputerScientist-01 they have changed the name of the data values so we need to change the name of the values and then it will work. |
Modified the code to fit the new data model from CoinMarketCap. The main changes are in the load_data function. Verified the code in the latest streamlit version. ❯ streamlit --version # This app is for educational purpose only. Insights gained is not financial advice. Use at your own risk!
import streamlit as st
from PIL import Image
import pandas as pd
import base64
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import requests
import json
import time
# ---------------------------------#
# New feature (make sure to upgrade your streamlit library)
# pip install --upgrade streamlit
# ---------------------------------#
# Page layout
# Page expands to full width
st.set_page_config(layout="wide")
# ---------------------------------#
# Title
image = Image.open("logo.jpg")
st.image(image, width=500)
st.title("Crypto Price App")
st.markdown(
"""
This app retrieves cryptocurrency prices for the top 100 cryptocurrency from the **CoinMarketCap**!
"""
)
# ---------------------------------#
# About
expander_bar = st.expander("About")
expander_bar.markdown(
"""
* **Python libraries:** base64, pandas, streamlit, numpy, matplotlib, seaborn, BeautifulSoup, requests, json, time
* **Data source:** [CoinMarketCap](http://coinmarketcap.com).
* **Credit:** Web scraper adapted from the Medium article *[Web Scraping Crypto Prices With Python](https://towardsdatascience.com/web-scraping-crypto-prices-with-python-41072ea5b5bf)* written by [Bryan Feng](https://medium.com/@bryanf).
"""
)
# ---------------------------------#
# Page layout (continued)
# Divide page to 3 columns (col1 = sidebar, col2 and col3 = page contents)
col1 = st.sidebar
col2, col3 = st.columns((2, 1))
# ---------------------------------#
# Sidebar + Main panel
col1.header("Input Options")
# Sidebar - Currency price unit
currency_price_unit = col1.selectbox("Select currency for price", ("USD", "BTC", "ETH"))
# Web scraping of CoinMarketCap data
@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])
listings = coin_data["props"]["initialState"]["cryptocurrency"]["listingLatest"][
"data"
]
attributes = listings[0]["keysArr"]
index_of_id = attributes.index("id")
index_of_slug = attributes.index("slug")
for i in listings[1:]:
coins[str(i[index_of_id])] = i[index_of_slug]
coin_name = []
coin_symbol = []
market_cap = []
percent_change_1h = []
percent_change_24h = []
percent_change_7d = []
price = []
volume_24h = []
index_of_slug = attributes.index("slug")
index_of_symbol = attributes.index("symbol")
index_of_quote_currency_price = attributes.index(
f"quote.{currency_price_unit}.price"
)
index_of_quote_currency_percent_change_1h = attributes.index(
f"quote.{currency_price_unit}.percentChange1h"
)
index_of_quote_currency_percent_change_24h = attributes.index(
f"quote.{currency_price_unit}.percentChange24h"
)
index_of_quote_currency_percent_change_7d = attributes.index(
f"quote.{currency_price_unit}.percentChange7d"
)
index_of_quote_currency_market_cap = attributes.index(
f"quote.{currency_price_unit}.marketCap"
)
index_of_quote_currency_volume_24h = attributes.index(
f"quote.{currency_price_unit}.volume24h"
)
for i in listings[1:]:
coin_name.append(i[index_of_slug])
coin_symbol.append(i[index_of_symbol])
price.append(i[index_of_quote_currency_price])
percent_change_1h.append(i[index_of_quote_currency_percent_change_1h])
percent_change_24h.append(i[index_of_quote_currency_percent_change_24h])
percent_change_7d.append(i[index_of_quote_currency_percent_change_7d])
market_cap.append(i[index_of_quote_currency_market_cap])
volume_24h.append(i[index_of_quote_currency_volume_24h])
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
df = load_data()
# Sidebar - Cryptocurrency selections
sorted_coin = sorted(df["coin_symbol"])
selected_coin = col1.multiselect("Cryptocurrency", sorted_coin, sorted_coin)
df_selected_coin = df[(df["coin_symbol"].isin(selected_coin))] # Filtering data
# Sidebar - Number of coins to display
num_coin = col1.slider("Display Top N Coins", 1, 100, 100)
df_coins = df_selected_coin[:num_coin]
# Sidebar - Percent change timeframe
percent_timeframe = col1.selectbox("Percent change time frame", ["7d", "24h", "1h"])
percent_dict = {
"7d": "percent_change_7d",
"24h": "percent_change_24h",
"1h": "percent_change_1h",
}
selected_percent_timeframe = percent_dict[percent_timeframe]
# Sidebar - Sorting values
sort_values = col1.selectbox("Sort values?", ["Yes", "No"])
col2.subheader("Price Data of Selected Cryptocurrency")
col2.write(
"Data Dimension: "
+ str(df_selected_coin.shape[0])
+ " rows and "
+ str(df_selected_coin.shape[1])
+ " columns."
)
col2.dataframe(df_coins)
# Download CSV data
# https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806
def filedownload(df):
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions
href = f'<a href="data:file/csv;base64,{b64}" download="crypto.csv">Download CSV File</a>'
return href
col2.markdown(filedownload(df_selected_coin), unsafe_allow_html=True)
# ---------------------------------#
# Preparing data for Bar plot of % Price change
col2.subheader("Table of % Price Change")
df_change = pd.concat(
[
df_coins.coin_symbol,
df_coins.percent_change_1h,
df_coins.percent_change_24h,
df_coins.percent_change_7d,
],
axis=1,
)
df_change = df_change.set_index("coin_symbol")
df_change["positive_percent_change_1h"] = df_change["percent_change_1h"] > 0
df_change["positive_percent_change_24h"] = df_change["percent_change_24h"] > 0
df_change["positive_percent_change_7d"] = df_change["percent_change_7d"] > 0
col2.dataframe(df_change)
# Conditional creation of Bar plot (time frame)
col3.subheader("Bar plot of % Price Change")
if percent_timeframe == "7d":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_7d"])
col3.write("*7 days period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_7d"].plot(
kind="barh",
color=df_change.positive_percent_change_7d.map({True: "g", False: "r"}),
)
col3.pyplot(plt)
elif percent_timeframe == "24h":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_24h"])
col3.write("*24 hour period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_24h"].plot(
kind="barh",
color=df_change.positive_percent_change_24h.map({True: "g", False: "r"}),
)
col3.pyplot(plt)
else:
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_1h"])
col3.write("*1 hour period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_1h"].plot(
kind="barh",
color=df_change.positive_percent_change_1h.map({True: "g", False: "r"}),
)
col3.pyplot(plt) |
|
can you share the source code for this please |
August 29 2023 **TypeError: string indices must be integers**
Traceback:
File "/Users/luis/miniforge3/envs/venv_FCC_Build_12_DS_Apps_Python_Streamlit_310/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
exec(code, module.__dict__)
File "/Users/luis/Documents/Programming/Courses_Programming/FCC_Build_12_DS_Apps_Python_Streamlit/venv_FCC_Build_12_DS_Apps_Python_Streamlit_310/Projects/App_006_001/App_006_001_Notebooks/App_006_001_002.py", line 144, in <module>
df = load_data()
File "/Users/luis/miniforge3/envs/venv_FCC_Build_12_DS_Apps_Python_Streamlit_310/lib/python3.10/site-packages/streamlit/runtime/legacy_caching/caching.py", line 715, in wrapped_func
return get_or_create_cached_value()
File "/Users/luis/miniforge3/envs/venv_FCC_Build_12_DS_Apps_Python_Streamlit_310/lib/python3.10/site-packages/streamlit/runtime/legacy_caching/caching.py", line 696, in get_or_create_cached_value
return_value = non_optional_func(*args, **kwargs)
File "/Users/luis/Documents/Programming/Courses_Programming/FCC_Build_12_DS_Apps_Python_Streamlit/venv_FCC_Build_12_DS_Apps_Python_Streamlit_310/Projects/App_006_001/App_006_001_Notebooks/App_006_001_002.py", line 68, in load_data
listings = coin_data["props"]["initialState"]["cryptocurrency"]["listingLatest"][ Python 3.10.12 August 30 2023 **TypeError: string indices must be integers** I would like to thank @kranthigy and everyone else that modified the code. I used that code as a base code and modified a few things. This is the code that I used modified by [@kranthigy] and then slightly modified by me on the load() function:
# This app is for educational purpose only. Insights gained is not financial advice. Use at your own risk!
import streamlit as st
from PIL import Image
import pandas as pd
import base64
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import requests
import json
import time
# ---------------------------------#
# New feature (make sure to upgrade your streamlit library)
# pip install --upgrade streamlit
# ---------------------------------#
# Page layout
# Page expands to full width
st.set_page_config(layout="wide")
# ---------------------------------#
# Title
image = Image.open("logo.jpg")
st.image(image, width=500)
st.title("Crypto Price App")
st.markdown(
"""
This app retrieves cryptocurrency prices for the top 100 cryptocurrency from the **CoinMarketCap**!
"""
)
# ---------------------------------#
# About
expander_bar = st.expander("About")
expander_bar.markdown(
"""
* **Python libraries:** base64, pandas, streamlit, numpy, matplotlib, seaborn, BeautifulSoup, requests, json, time
* **Data source:** [CoinMarketCap](http://coinmarketcap.com).
* **Credit:** Web scraper adapted from the Medium article *[Web Scraping Crypto Prices With Python](https://towardsdatascience.com/web-scraping-crypto-prices-with-python-41072ea5b5bf)* written by [Bryan Feng](https://medium.com/@bryanf).
"""
)
# ---------------------------------#
# Page layout (continued)
# Divide page to 3 columns (col1 = sidebar, col2 and col3 = page contents)
col1 = st.sidebar
col2, col3 = st.columns((2, 1))
# ---------------------------------#
# Sidebar + Main panel
col1.header("Input Options")
# Sidebar - Currency price unit
currency_price_unit = col1.selectbox("Select currency for price", ("USD", "BTC", "ETH"))
# Web scraping of CoinMarketCap data
@st.cache_data
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])
#listings = coin_data["props"]["initialState"]["cryptocurrency"]["listingLatest"][
#"data"
#]
coin_data = json.loads(data.contents[0])#
s = coin_data['props']['initialState']#
listings = json.loads(s.replace("'", ""))#
listings = listings['cryptocurrency']['listingLatest']['data']#
attributes = listings[0]["keysArr"]
index_of_id = attributes.index("id")
index_of_slug = attributes.index("slug")
for i in listings[1:]:
coins[str(i[index_of_id])] = i[index_of_slug]
coin_name = []
coin_symbol = []
market_cap = []
percent_change_1h = []
percent_change_24h = []
percent_change_7d = []
price = []
volume_24h = []
index_of_slug = attributes.index("slug")
index_of_symbol = attributes.index("symbol")
index_of_quote_currency_price = attributes.index(
f"quote.{currency_price_unit}.price"
)
index_of_quote_currency_percent_change_1h = attributes.index(
f"quote.{currency_price_unit}.percentChange1h"
)
index_of_quote_currency_percent_change_24h = attributes.index(
f"quote.{currency_price_unit}.percentChange24h"
)
index_of_quote_currency_percent_change_7d = attributes.index(
f"quote.{currency_price_unit}.percentChange7d"
)
index_of_quote_currency_market_cap = attributes.index(
f"quote.{currency_price_unit}.marketCap"
)
index_of_quote_currency_volume_24h = attributes.index(
f"quote.{currency_price_unit}.volume24h"
)
for i in listings[1:]:
coin_name.append(i[index_of_slug])
coin_symbol.append(i[index_of_symbol])
price.append(i[index_of_quote_currency_price])
percent_change_1h.append(i[index_of_quote_currency_percent_change_1h])
percent_change_24h.append(i[index_of_quote_currency_percent_change_24h])
percent_change_7d.append(i[index_of_quote_currency_percent_change_7d])
market_cap.append(i[index_of_quote_currency_market_cap])
volume_24h.append(i[index_of_quote_currency_volume_24h])
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
df = load_data()
# Sidebar - Cryptocurrency selections
sorted_coin = sorted(df["coin_symbol"])
selected_coin = col1.multiselect("Cryptocurrency", sorted_coin, sorted_coin)
df_selected_coin = df[(df["coin_symbol"].isin(selected_coin))] # Filtering data
# Sidebar - Number of coins to display
num_coin = col1.slider("Display Top N Coins", 1, 100, 100)
df_coins = df_selected_coin[:num_coin]
# Sidebar - Percent change timeframe
percent_timeframe = col1.selectbox("Percent change time frame", ["7d", "24h", "1h"])
percent_dict = {
"7d": "percent_change_7d",
"24h": "percent_change_24h",
"1h": "percent_change_1h",
}
selected_percent_timeframe = percent_dict[percent_timeframe]
# Sidebar - Sorting values
sort_values = col1.selectbox("Sort values?", ["Yes", "No"])
col2.subheader("Price Data of Selected Cryptocurrency")
col2.write(
"Data Dimension: "
+ str(df_selected_coin.shape[0])
+ " rows and "
+ str(df_selected_coin.shape[1])
+ " columns."
)
col2.dataframe(df_coins)
# Download CSV data
# https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806
def filedownload(df):
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions
href = f'<a href="data:file/csv;base64,{b64}" download="crypto.csv">Download CSV File</a>'
return href
col2.markdown(filedownload(df_selected_coin), unsafe_allow_html=True)
# ---------------------------------#
# Preparing data for Bar plot of % Price change
col2.subheader("Table of % Price Change")
df_change = pd.concat(
[
df_coins.coin_symbol,
df_coins.percent_change_1h,
df_coins.percent_change_24h,
df_coins.percent_change_7d,
],
axis=1,
)
df_change = df_change.set_index("coin_symbol")
df_change["positive_percent_change_1h"] = df_change["percent_change_1h"] > 0
df_change["positive_percent_change_24h"] = df_change["percent_change_24h"] > 0
df_change["positive_percent_change_7d"] = df_change["percent_change_7d"] > 0
col2.dataframe(df_change)
# Conditional creation of Bar plot (time frame)
col3.subheader("Bar plot of % Price Change")
if percent_timeframe == "7d":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_7d"])
col3.write("*7 days period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_7d"].plot(
kind="barh",
color=df_change.positive_percent_change_7d.map({True: "g", False: "r"}),
)
col3.pyplot(plt)
elif percent_timeframe == "24h":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_24h"])
col3.write("*24 hour period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_24h"].plot(
kind="barh",
color=df_change.positive_percent_change_24h.map({True: "g", False: "r"}),
)
col3.pyplot(plt)
else:
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_1h"])
col3.write("*1 hour period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_1h"].plot(
kind="barh",
color=df_change.positive_percent_change_1h.map({True: "g", False: "r"}),
)
col3.pyplot(plt) |
The text was updated successfully, but these errors were encountered: