Skip to content

Commit 2d1ac03

Browse files
committed
Days 18 - Added calls to trivia API
1 parent 374db21 commit 2d1ac03

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ ENV/
9090
env.bak/
9191
venv.bak/
9292
*_venv/
93+
venv_*/
9394

9495
# Spyder project settings
9596
.spyderproject
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FLASK_APP=demo.py

days/017-020-flask-call-apis/code/program/routes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ def pokemon():
3535
return render_template('pokemon.html',
3636
pokemon=pokemon)
3737

38+
@app.route('/trivia')
39+
def trivia():
40+
question, answer = get_trivia_question()
41+
return render_template('trivia.html',
42+
question=question,
43+
answer=answer)
44+
45+
def get_trivia_question():
46+
r = requests.get('https://jservice.io/api/random')
47+
data = r.json()
48+
return data[0]['question'], data[0]['answer']
49+
3850

3951
def get_chuck_joke():
4052
r = requests.get('https://api.chucknorris.io/jokes/random')

days/017-020-flask-call-apis/code/program/templates/base.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<button class="mui-btn mui-btn--raised"><a href="/index">Home</a></button>
1414
<button class="mui-btn mui-btn--raised"><a href="/100Days">100 Days</a></button>
1515
<button class="mui-btn mui-btn--raised"><a href="/chuck">Chuck Quote</a></button>
16-
<button class="mui-btn mui-btn--raised"><a href="/pokemon">Pokemon Colour Picker</a></button>
16+
<button class="mui-btn mui-btn--raised"><a href="/pokemon">Pokemon Colour Picker</a></button>
17+
<button class="mui-btn mui-btn--raised"><a href="/trivia">Trivia Question</a></button>
18+
1719
</div>
1820
<hr>
1921
{% block content %}{% endblock %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<h1>Trivia Questions!</h1>
5+
<p>{{question}}</p>
6+
<hr>
7+
<p>{{answer}}</p>
8+
{% endblock %}

days/017-020-flask-call-apis/code/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
certifi==2018.8.24
22
chardet==3.0.4
33
Click==7.0
4+
python-dotenv==0.9.1
45
Flask==1.0.2
56
idna==2.7
67
itsdangerous==0.24

0 commit comments

Comments
 (0)