Last active
January 21, 2020 19:26
-
-
Save ryderdamen/2571cfd618dc681e8fca6090e5163b65 to your computer and use it in GitHub Desktop.
An example flask application for retrieving and displaying facts about cats.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, render_template | |
import requests | |
app = Flask(__name__) | |
def get_cat_fact(): | |
cat_facts_url = 'https://catfact.ninja/fact' | |
response = requests.get(cat_facts_url) | |
if response.status_code is 200: | |
return response.json()['fact'] | |
return 'Sorry, could not get cat fact!' | |
@app.route('/') | |
def index(): | |
"""Index page of the site""" | |
cat_fact_string = 'Hello there, here is your cat fact: {}'.format(get_cat_fact()) | |
return render_template('base.html', cat_fact=cat_fact_string) | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment