Skip to content

Commit bcc3fbf

Browse files
committed
PCC16 covrebo
1 parent a6c6c1b commit bcc3fbf

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

16/covrebo/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# CanHazDadJoke
2+
A single page [flask](https://flask.palletsprojects.com/en/1.1.x/) app that presents a button to press for a dad joke. A dad joke is received from the [icanhazdadjoke](https://icanhazdadjoke.com/) API as `json`. The `json` is then parsed and presented in a modal to the user. Page is styled using [Bootstrap](https://getbootstrap.com/) v5.0. A more detailed description of the project can be found [here](https://covrebo.com/retrieving-json-with-flask.html).

16/covrebo/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Flask, render_template
2+
import services
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route('/')
8+
def hello_world():
9+
# get dad joke
10+
joke = services.get_dad_joke()
11+
return render_template('index.html', title='Funny', joke=joke)

16/covrebo/requirements.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
attrs==19.3.0
2+
certifi==2020.6.20
3+
chardet==3.0.4
4+
click==7.1.2
5+
Flask==1.1.2
6+
idna==2.10
7+
itsdangerous==1.1.0
8+
Jinja2==2.11.2
9+
MarkupSafe==1.1.1
10+
more-itertools==8.4.0
11+
packaging==20.4
12+
pluggy==0.13.1
13+
py==1.9.0
14+
pyparsing==2.4.7
15+
pytest==5.4.3
16+
requests==2.24.0
17+
six==1.15.0
18+
urllib3==1.25.9
19+
wcwidth==0.2.5
20+
Werkzeug==1.0.1

16/covrebo/services.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import json
2+
import requests
3+
4+
5+
def get_dad_joke():
6+
url = "https://icanhazdadjoke.com/"
7+
headers = {'User-Agent': 'My Library (https://github.com/clark_griswold)',
8+
'Accept': 'application/json'}
9+
r = requests.get(url, headers=headers)
10+
joke = json.loads(r.text)
11+
return joke['joke']

16/covrebo/templates/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{% extends "layout.html" %}
2+
{% block content %}
3+
<main class="px-3">
4+
<div class="container mt-5">
5+
<!-- Button trigger modal -->
6+
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#dadJoke">
7+
Get Funny
8+
</button>
9+
</div>
10+
11+
<!-- Dad Joke Modal -->
12+
<div class="modal fade" id="dadJoke" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
13+
<div class="modal-dialog">
14+
<div class="modal-content">
15+
<div class="modal-header">
16+
<h5 class="modal-title" id="dadJoke">A dad joke:</h5>
17+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
18+
</div>
19+
<div class="modal-body">
20+
{{ joke }}
21+
</div>
22+
<div class="modal-footer">
23+
<button onClick="window.location.reload();" type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
24+
</div>
25+
<div class="about-modal-credit m-2">
26+
<span class="text-muted">Thanks <a href="https://icanhazdadjoke.com">icanhazdadjoke.com</a>.</span>
27+
</div>
28+
</div>
29+
</div>
30+
</div>
31+
</main>
32+
{% endblock content %}

16/covrebo/templates/layout.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
8+
<!-- Bootstrap CSS -->
9+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
10+
11+
<title>Hello, world!</title>
12+
</head>
13+
<body class="text-center">
14+
15+
{% block content %}{% endblock %}
16+
17+
<!-- Optional JavaScript; choose one of the two! -->
18+
19+
<!-- Option 1: Bootstrap Bundle with Popper -->
20+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
21+
22+
<!-- Option 2: Separate Popper and Bootstrap JS -->
23+
<!--
24+
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper.min.js" integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi" crossorigin="anonymous"></script>
25+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.min.js" integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG" crossorigin="anonymous"></script>
26+
-->
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)