Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template_string, jsonify
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Initialize Flask app
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# API endpoint for getting cryptocurrency prices from CoinGecko
|
8 |
+
API_URL = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,dogecoin&vs_currencies=usd'
|
9 |
+
|
10 |
+
@app.route('/')
|
11 |
+
def index():
|
12 |
+
# Render the main page
|
13 |
+
return render_template_string(HTML_TEMPLATE)
|
14 |
+
|
15 |
+
@app.route('/api/prices', methods=['GET'])
|
16 |
+
def get_prices():
|
17 |
+
# Fetch price data
|
18 |
+
response = requests.get(API_URL)
|
19 |
+
prices = response.json()
|
20 |
+
return jsonify(prices)
|
21 |
+
|
22 |
+
# HTML template with HTMX integration
|
23 |
+
HTML_TEMPLATE = '''
|
24 |
+
<!DOCTYPE html>
|
25 |
+
<html lang="en">
|
26 |
+
<head>
|
27 |
+
<meta charset="UTF-8">
|
28 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
29 |
+
<script src="https://unpkg.com/htmx.org"></script>
|
30 |
+
<title>Crypto Price Dashboard</title>
|
31 |
+
</head>
|
32 |
+
<body>
|
33 |
+
<h1>Cryptocurrency Prices</h1>
|
34 |
+
<div id="prices" hx-get="/api/prices" hx-trigger="every 10s">
|
35 |
+
<div>Loading...</div>
|
36 |
+
</div>
|
37 |
+
<script>
|
38 |
+
document.addEventListener('htmx:afterRequest', function(event) {
|
39 |
+
const pricesDiv = document.getElementById('prices');
|
40 |
+
const data = event.detail.xhr.response;
|
41 |
+
pricesDiv.innerHTML = `
|
42 |
+
<div>Bitcoin: $${data.bitcoin.usd}</div>
|
43 |
+
<div>Ethereum: $${data.ethereum.usd}</div>
|
44 |
+
<div>Dogecoin: $${data.dogecoin.usd}</div>
|
45 |
+
`;
|
46 |
+
});
|
47 |
+
</script>
|
48 |
+
</body>
|
49 |
+
</html>
|
50 |
+
'''
|
51 |
+
|
52 |
+
# Run the application
|
53 |
+
if __name__ == '__main__':
|
54 |
+
app.run(debug=True, host='0.0.0.0')
|