From bcc3fbf8db7ad3588870488672103375926340f8 Mon Sep 17 00:00:00 2001 From: Chris Ovrebo Date: Sat, 13 Feb 2021 11:33:33 -0600 Subject: [PATCH] PCC16 covrebo --- 16/covrebo/README.md | 2 ++ 16/covrebo/app.py | 11 +++++++++++ 16/covrebo/requirements.txt | 20 ++++++++++++++++++++ 16/covrebo/services.py | 11 +++++++++++ 16/covrebo/templates/index.html | 32 ++++++++++++++++++++++++++++++++ 16/covrebo/templates/layout.html | 28 ++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+) create mode 100644 16/covrebo/README.md create mode 100644 16/covrebo/app.py create mode 100644 16/covrebo/requirements.txt create mode 100644 16/covrebo/services.py create mode 100644 16/covrebo/templates/index.html create mode 100644 16/covrebo/templates/layout.html diff --git a/16/covrebo/README.md b/16/covrebo/README.md new file mode 100644 index 000000000..e7a5b2170 --- /dev/null +++ b/16/covrebo/README.md @@ -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). \ No newline at end of file diff --git a/16/covrebo/app.py b/16/covrebo/app.py new file mode 100644 index 000000000..2fee3a526 --- /dev/null +++ b/16/covrebo/app.py @@ -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() + return render_template('index.html', title='Funny', joke=joke) diff --git a/16/covrebo/requirements.txt b/16/covrebo/requirements.txt new file mode 100644 index 000000000..c2c1c6b00 --- /dev/null +++ b/16/covrebo/requirements.txt @@ -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 diff --git a/16/covrebo/services.py b/16/covrebo/services.py new file mode 100644 index 000000000..519676811 --- /dev/null +++ b/16/covrebo/services.py @@ -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'] diff --git a/16/covrebo/templates/index.html b/16/covrebo/templates/index.html new file mode 100644 index 000000000..9636b52c4 --- /dev/null +++ b/16/covrebo/templates/index.html @@ -0,0 +1,32 @@ +{% extends "layout.html" %} +{% block content %} +
+
+ + +
+ + + +
+{% endblock content %} \ No newline at end of file diff --git a/16/covrebo/templates/layout.html b/16/covrebo/templates/layout.html new file mode 100644 index 000000000..5547993dd --- /dev/null +++ b/16/covrebo/templates/layout.html @@ -0,0 +1,28 @@ + + + + + + + + + + + Hello, world! + + + + {% block content %}{% endblock %} + + + + + + + + + + \ No newline at end of file