Spaces:
Running
Running
from flask import Flask, render_template_string, jsonify | |
import requests | |
# Initialize Flask app | |
app = Flask(__name__) | |
# API endpoint for getting cryptocurrency prices from CoinGecko | |
API_URL = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,dogecoin&vs_currencies=usd' | |
def index(): | |
# Render the main page | |
return render_template_string(HTML_TEMPLATE) | |
def get_prices(): | |
# Fetch price data | |
response = requests.get(API_URL) | |
prices = response.json() | |
return jsonify(prices) | |
# HTML template with HTMX integration | |
HTML_TEMPLATE = ''' | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<script src="https://unpkg.com/htmx.org"></script> | |
<title>Crypto Price Dashboard</title> | |
</head> | |
<body> | |
<h1>Cryptocurrency Prices</h1> | |
<div id="prices" hx-get="/api/prices" hx-trigger="every 10s"> | |
<div>Loading...</div> | |
</div> | |
<script> | |
document.addEventListener('htmx:afterRequest', function(event) { | |
const pricesDiv = document.getElementById('prices'); | |
const data = event.detail.xhr.response; | |
pricesDiv.innerHTML = ` | |
<div>Bitcoin: $${data.bitcoin.usd}</div> | |
<div>Ethereum: $${data.ethereum.usd}</div> | |
<div>Dogecoin: $${data.dogecoin.usd}</div> | |
`; | |
}); | |
</script> | |
</body> | |
</html> | |
''' | |
# Run the application | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0') | |