mgbam commited on
Commit
5e698c6
·
verified ·
1 Parent(s): ad59053

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -1,26 +1,16 @@
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>
@@ -37,7 +27,7 @@ HTML_TEMPLATE = '''
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>
@@ -47,8 +37,18 @@ HTML_TEMPLATE = '''
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')
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
  import requests
4
+ import uvicorn
5
 
6
+ # Initialize FastAPI app
7
+ app = FastAPI()
8
 
9
  # API endpoint for getting cryptocurrency prices from CoinGecko
10
  API_URL = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,dogecoin&vs_currencies=usd'
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # HTML template with HTMX integration
13
+ HTML_TEMPLATE = """
14
  <!DOCTYPE html>
15
  <html lang="en">
16
  <head>
 
27
  <script>
28
  document.addEventListener('htmx:afterRequest', function(event) {
29
  const pricesDiv = document.getElementById('prices');
30
+ const data = JSON.parse(event.detail.xhr.responseText);
31
  pricesDiv.innerHTML = `
32
  <div>Bitcoin: $${data.bitcoin.usd}</div>
33
  <div>Ethereum: $${data.ethereum.usd}</div>
 
37
  </script>
38
  </body>
39
  </html>
40
+ """
41
+
42
+ @app.get("/", response_class=HTMLResponse)
43
+ def read_root():
44
+ return HTML_TEMPLATE
45
+
46
+ @app.get("/api/prices", response_class=JSONResponse)
47
+ def get_prices():
48
+ response = requests.get(API_URL)
49
+ response.raise_for_status()
50
+ return response.json()
51
 
52
+ if __name__ == "__main__":
53
+ # Run with: uvicorn this_file_name:app --host 0.0.0.0 --port 7860 --reload
54
+ uvicorn.run("this_file_name:app", host="0.0.0.0", port=7860, reload=True)