Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 16/covrebo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# CanHazDadJoke
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).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool idea, didn't know about that API

11 changes: 11 additions & 0 deletions 16/covrebo/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask import Flask, render_template
import services

app = Flask(__name__)


@app.route('/')
def hello_world():
# get dad joke
joke = services.get_dad_joke()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice you added a service layer.

return render_template('index.html', title='Funny', joke=joke)
20 changes: 20 additions & 0 deletions 16/covrebo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
attrs==19.3.0
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
Flask==1.1.2
idna==2.10
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
more-itertools==8.4.0
packaging==20.4
pluggy==0.13.1
py==1.9.0
pyparsing==2.4.7
pytest==5.4.3
requests==2.24.0
six==1.15.0
urllib3==1.25.9
wcwidth==0.2.5
Werkzeug==1.0.1
11 changes: 11 additions & 0 deletions 16/covrebo/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import json
import requests


def get_dad_joke():
url = "https://icanhazdadjoke.com/"
headers = {'User-Agent': 'My Library (https://github.com/clark_griswold)',
'Accept': 'application/json'}
r = requests.get(url, headers=headers)
joke = json.loads(r.text)
return joke['joke']
32 changes: 32 additions & 0 deletions 16/covrebo/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "layout.html" %}
{% block content %}
<main class="px-3">
<div class="container mt-5">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#dadJoke">
Get Funny
</button>
</div>

<!-- Dad Joke Modal -->
<div class="modal fade" id="dadJoke" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="dadJoke">A dad joke:</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{ joke }}
</div>
<div class="modal-footer">
<button onClick="window.location.reload();" type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
<div class="about-modal-credit m-2">
<span class="text-muted">Thanks <a href="https://icanhazdadjoke.com">icanhazdadjoke.com</a>.</span>
</div>
</div>
</div>
</div>
</main>
{% endblock content %}
28 changes: 28 additions & 0 deletions 16/covrebo/templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Bootstrap CSS -->
<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">

<title>Hello, world!</title>
</head>
<body class="text-center">

{% block content %}{% endblock %}

<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: Bootstrap Bundle with Popper -->
<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>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<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>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.min.js" integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG" crossorigin="anonymous"></script>
-->
</body>
</html>