File size: 2,484 Bytes
5cb5b93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import requests

# Function to fetch comprehensive Bitcoin information from CoinGecko
def fetch_bitcoin_info():
    # API endpoint for detailed Bitcoin information
    endpoint = "https://api.coingecko.com/api/v3/coins/bitcoin"

    # Send a GET request to the endpoint
    response = requests.get(endpoint)

    if response.status_code == 200:
        data = response.json()

        # Extract detailed information for Bitcoin
        current_price = data["market_data"]["current_price"]["usd"]
        market_cap = data["market_data"]["market_cap"]["usd"]
        trading_volume_24h = data["market_data"]["total_volume"]["usd"]
        rank = data["market_cap_rank"]
        change_rate_24h = data["market_data"]["price_change_percentage_24h"]
        all_time_high = data["market_data"]["ath"]["usd"]
        number_of_markets = len(data["tickers"])  # Number of markets in which Bitcoin is listed
        number_of_exchanges = len(set([ticker["market"]["name"] for ticker in data["tickers"]]))  # Unique exchanges
        total_supply = data["market_data"]["total_supply"]
        circulating_supply = data["market_data"]["circulating_supply"]

        # Create a formatted output
        output = (
            f"Bitcoin Price Information:\n"
            f"Current Price: ${current_price:.2f}\n"
            f"Market Cap: ${market_cap:.2f}\n"
            f"24-Hour Trading Volume: ${trading_volume_24h:.2f}\n"
            f"24-Hour Change Rate: {change_rate_24h:.2f}%\n"
            f"All-Time High (ATH): ${all_time_high:.2f}\n"
            f"Market Cap Rank: {rank}\n"
            f"Number of Markets: {number_of_markets}\n"
            f"Number of Exchanges: {number_of_exchanges}\n"
            f"Total Supply: {total_supply:.2f}\n"
            f"Circulating Supply: {circulating_supply:.2f}"
        )
        return output
    else:
        return "Failed to fetch Bitcoin information."

# Create the Gradio interface
with gr.Blocks() as demo:
    # Create a textbox to display Bitcoin information
    bitcoin_info_text = gr.Textbox(label="Bitcoin Information", interactive=False)

    # Button to fetch Bitcoin information
    fetch_button = gr.Button("Fetch Bitcoin Information")

    # Connect the button click event to the function to fetch and display Bitcoin information
    fetch_button.click(fetch_bitcoin_info, outputs=bitcoin_info_text)

# Launch the Gradio app
demo.launch(share=True)