From f429202e71fb226d9d3ff18278951c8b12e57a1c Mon Sep 17 00:00:00 2001 From: knighthinata Date: Thu, 10 Oct 2024 21:16:40 +0530 Subject: [PATCH] add_cryptos_api_program --- .../pages/programs/ApiPrograms/cryptos.py | 331 ++++++++++++++++++ 1 file changed, 331 insertions(+) create mode 100644 src/apps/pages/programs/ApiPrograms/cryptos.py diff --git a/src/apps/pages/programs/ApiPrograms/cryptos.py b/src/apps/pages/programs/ApiPrograms/cryptos.py new file mode 100644 index 00000000..7ef24be0 --- /dev/null +++ b/src/apps/pages/programs/ApiPrograms/cryptos.py @@ -0,0 +1,331 @@ +import streamlit as st +import plotly.express as px +import plotly.graph_objects as go +import requests + +BASE_URL = "https://api.coingecko.com/api/v3" +SUPPORTED_CURRENCIES = [] +SUPPORTED_COINS = [] + +def clipDecimal(number, precision=1): + return round(float(number), precision) + +def format_price_change(percentage_change): + if clipDecimal(percentage_change) > 0: + return f" {clipDecimal(percentage_change)}%" + else: + return f"🔻 {clipDecimal(percentage_change)}%" + +def showTrendingAssets(): + URL = f"{BASE_URL}/search/trending" + + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + st.markdown("## Trending Assets") + + cryptocurrency = st.selectbox("Select an asset:", options=[None, "Cryptocurrency", "NFTs", "Categories"]) + + if cryptocurrency == "Cryptocurrency": + + table_html = """ + + + + + + + + + + + + """ + for i, coin in enumerate(data["coins"]): + coin=coin["item"] + table_html += "" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += "" + table_html += "
#Coin NameMarket Cap RankPrice24hMarket CapTotal VolumeSparkline
{i+1}   {coin['name']}   {coin['symbol']}{coin['market_cap_rank']}${clipDecimal(coin['data']['price'], 3)}{format_price_change(coin['data']['price_change_percentage_24h']['usd'])}{coin['data']['market_cap']}{coin['data']['total_volume']}
" + + st.markdown(table_html, unsafe_allow_html=True) + + elif cryptocurrency == "NFTs": + + table_html = """ + + + + + + + + + + + """ + for i, nft in enumerate(data['nfts']): + table_html += "" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += "" + table_html += "
#NameFloor Price24h Price Change24h VolumeAverage Sale PriceSparkline
{i+1}   {nft['name']}   {nft['symbol']}{nft['data']['floor_price']}{format_price_change(nft['data']['floor_price_in_usd_24h_percentage_change'])}{nft['data']['h24_volume']}{nft['data']['h24_average_sale_price']}
" + + st.markdown(table_html, unsafe_allow_html=True) + + elif cryptocurrency == "Categories": + + table_html = """ + + + + + + + + + + + """ + for i, category in enumerate(data["categories"]): + table_html += "" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += f"" + table_html += "" + + table_html += "
#NameCoins Count24h Market Cap ChangeMarket CapTotal VolumeSparkline
{i+1}{category['name']}{category['coins_count']}{format_price_change(category['data']['market_cap_change_percentage_24h']['usd'])}${category['data']['market_cap']}${category['data']['total_volume']}
" + st.markdown(table_html, unsafe_allow_html=True) + + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + +@st.cache_data(ttl=86400) +def getSupportedCurrencies(): + global SUPPORTED_CURRENCIES + + URL = f"{BASE_URL}/simple/supported_vs_currencies" + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + SUPPORTED_CURRENCIES = data + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + +@st.cache_data(ttl=86400) +def getSupportedCoins(): + global SUPPORTED_COINS + + URL = f"{BASE_URL}/coins/list" + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + SUPPORTED_COINS = [coin['id'] for coin in data] + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + +def searchCryptocurrency(query): + URL = f"{BASE_URL}/search?query={query}" + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + results = data['coins'] + if results: + st.title(f"Search Results for '{query}'") + + num_columns = 3 + for row in range(0, len(results), num_columns): + row_coins = results[row:row + num_columns] + cols = st.columns(len(row_coins)) + for col, coin in zip(cols, row_coins): + with col: + st.markdown( + f"

" + f"{coin['name']}   {coin['symbol']}
" + f"Market Cap Rank: {coin['market_cap_rank']}
", + unsafe_allow_html=True + ) + st.markdown("---") + else: + st.error("No results found.") + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + +def cryptoConversion(from_coin, to_coin): + URL = f"{BASE_URL}/simple/price?ids={from_coin}&vs_currencies={to_coin}" + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + st.markdown("### Exchange Rates") + price = data[from_coin][to_coin] + st.markdown(f"**{from_coin.upper()} = {price:,.2f} {to_coin.upper()}**") + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"The conversion from {from_coin.upper()} to {to_coin.upper()} is not valid. Please check your inputs.") + +def showTopCryptocurrency(): + URL = f"{BASE_URL}/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=10" + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + st.markdown("### Top 10 Cryptocurrency") + st.markdown("On Market Cap") + + for coin in data: + col1, col2 = st.columns([1, 3]) + + with col1: + st.image(coin['image'], width=100) + + with col2: + st.subheader(f"{coin['name']} ({coin['symbol'].upper()})") + st.write(f"**Current Price**: ${coin['current_price']:,.2f}") + st.write(f"**Market Cap**: ${coin['market_cap']:,.0f}") + st.write(f"**24h Price Change**: {format_price_change(coin['price_change_percentage_24h'])}", unsafe_allow_html=True) + st.markdown("---") + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + +def showCryptoMarketOverview(): + URL = f"{BASE_URL}/global" + + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + st.markdown("### Cryptocurrency Market Overview") + + col1, col2, col3, col4 = st.columns(4) + + with col1: + st.metric(label="Active Cryptocurrencies", value=data['data']['active_cryptocurrencies']) + + with col2: + st.metric(label="Ongoing ICOs", value=data['data']['ongoing_icos']) + + with col3: + st.metric(label="Ended ICOs", value=data['data']['ended_icos']) + + with col4: + st.metric(label="Markets", value=data['data']['markets']) + + st.markdown(f"Market Capitalization Change (24h) {format_price_change(data['data']['market_cap_change_percentage_24h_usd'])}", unsafe_allow_html=True) + + + coins = list(data['data']['total_volume'].keys()) + volume = list(data['data']['total_volume'].values()) + + coin_volume_pairs = list(zip(coins, volume)) + top_10_coin_volume_pairs = sorted(coin_volume_pairs, key=lambda x: x[1], reverse=True)[:10] + + top_10_coins = [coin for coin, _ in top_10_coin_volume_pairs] + top_10_volumes = [vol for _, vol in top_10_coin_volume_pairs] + top_10_volume_percentages = [vol / sum(top_10_volumes) * 100 for vol in top_10_volumes] + st.plotly_chart(px.pie( + names=top_10_coins, + values=top_10_volume_percentages, + title="Cryptocurrency Volume Dominance", + hole=0.3, + )) + + + st.plotly_chart(px.pie( + names=[coin for coin in data['data']['market_cap_percentage']], + values=[data['data']['market_cap_percentage'][coin] for coin in data['data']['market_cap_percentage']], + title="Cryptocurrency Market Capitalization Dominance", + hole=0.3, + )) + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + +def showCompanyHoldings(): + URL = f"{BASE_URL}/companies/public_treasury/bitcoin" + + try: + response = requests.get(URL) + data = response.json() + if response.status_code == 200: + st.markdown("### Cryptocurrency Market Overview") + + st.header("Bitcoin (BTC) Holdings") + col1, col2, col3 = st.columns(3) + col1.metric(label="Total Holdings", value=data["total_holdings"]) + col2.metric(label="Total Value (USD)", value=f"${data['total_value_usd']:.2f}") + col3.metric(label="Market Cap Dominance", value=f"{data['market_cap_dominance']:.2f}%") + st.subheader("Companies Holding Bitcoin") + st.table(data["companies"]) + else: + st.error("API call not successful. Please try again later.") + except Exception as e: + st.error(f"An unexpected error occurred: {e}") + + +def cryptos(): + st.title("Cryptocurrency Dashboard") + + option = st.selectbox("Select an option:", options=[None, "Trending Assets", "Search Cryptocurrency", "Exchange Rates", "Top Cryptocurrency", "Crypto Global Market", "Companies Bitcoin Holdings"]) + + if option == "Trending Assets": + showTrendingAssets() + + elif option == "Search Cryptocurrency": + query = st.text_input(f"Enter the coin name or symbol") + if st.button("Search"): + searchCryptocurrency(query) + + elif option == "Exchange Rates": + if 'SUPPORTED_COINS' not in st.session_state: + st.session_state.SUPPORTED_COINS = getSupportedCoins() + + if 'SUPPORTED_CURRENCIES' not in st.session_state: + st.session_state.SUPPORTED_CURRENCIES = getSupportedCurrencies() + + from_currency = st.selectbox("From currency", options=SUPPORTED_COINS) + to_currency = st.selectbox("To currency", options=SUPPORTED_CURRENCIES) + + if st.button("Convert"): + cryptoConversion(from_currency, to_currency) + + elif option == "Top Cryptocurrency": + showTopCryptocurrency() + + elif option == "Crypto Global Market": + showCryptoMarketOverview() + + elif option == "Companies Bitcoin Holdings": + showCompanyHoldings()