-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
21 lines (17 loc) · 660 Bytes
/
Copy pathapp.py
File metadata and controls
21 lines (17 loc) · 660 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from flask import Flask, render_template
import requests
# Define API URL.
TRIVIA_URL = 'https://api.api-ninjas.com/v1/trivia'
# Initialize Flask.
app = Flask(__name__)
# Define routing.
@app.route('/')
def index():
# Make API Call - make sure to use a valid API key.
resp = requests.get(TRIVIA_URL, headers={'X-Api-Key': 'YOUR API KEY'}).json()
# Get first trivia result since the API returns a list of results.
trivia = resp[0]
# Render HTML using the trivia question and answer.
return render_template('index.html', question=trivia['question'], answer=trivia['answer'])
# Run the Flask app (127.0.0.1:5000 by default).
app.run()