Spaces:
Running
Running
File size: 1,594 Bytes
40a414f |
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 |
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'
@app.route('/')
def index():
# Render the main page
return render_template_string(HTML_TEMPLATE)
@app.route('/api/prices', methods=['GET'])
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')
|